I hear python is easier to write and runs faster.Quick question: php or python for a login database
I hear python is easier to write and runs faster.Quick question: php or python for a login database
Quick question: php or python for a login database
bump
I think we should start some programming contests up in here
yeah it's quite a hurdle at first but it really isn't that complicated actuallyI would love to try it but I don't understand the input and output formats, despite me being familiar with the game.
5 1
6 2 4 3 _ _
1
2
3
4
5
6 _ _ _ _ _
4
5 1
6 2 _ 3 _ _
4
5
6 2 1 3 _ _
3
4
5
6 2 1 _ _ _
2
3
4
5
6 _ 1 _ _ _
1
2
3
4
5
6 _ _ _ _ _
aight, I start, something simpleI'm up for doing programming exercises, but we should start with something simpler
Got it. I'm almost certain I've seen this problem years ago however I don't think it was described this way.yeah it's quite a hurdle at first but it really isn't that complicated actually
just understand that the sequence of numbers is the sequence of pegs holding the disc whose radius is equal to the position of the disc in the sequence (with the count starting from 1)
in other words, theses lines:
...
Got it now ?
aight, I start, something simple
1 - write a function that take 2 integers and return the greater of the two
2 - write a function that take 3 integers and return the greater of the three...using the function written in question 1
function greater(a, b)
{
if(a != b)
return a > b ? a : b;
else
return 'there are equivalent values';
}
function greater3(a, b, c)
{
var holder = greater(a, b);
if(typeof holder != 'string')
return greater(holder, c);
else
return 'there are equivalent values';
}
greater3(4,8,3);
def greater2(a,b)
if a != b
a > b ? a : b
else
puts 'There are equivalent values'
return false
end
end
def greater3(a, b, c)
if greater2(a, b) != false
holder = greater2(a,b)
holder2 = greater2(holder, c)
end
end
greater3(1, 4, 4)
yeah it's quite a hurdle at first but it really isn't that complicated actually
just understand that the sequence of numbers is the sequence of pegs holding the disc whose radius is equal to the position of the disc in the sequence (with the count starting from 1)
in other words, theses lines:
6 4
4 2 4 3 1 1
1 1 1 1 1 1
match this configuration "visually":
starting config:
5 1
6 2 4 3 _ _
final config:
1
2
3
4
5
6 _ _ _ _ _
where _ represents an empty peg
so the problem is to write a program that can give you the minimal number of moves needed to go from the starting config to the final one and enumerates those moves following this pattern
X Y means move disc on top of peg number X to the top of peg number Y
So for this particular configuration a minimal solution has the following moves (visually again):
move #1
4
5 1
6 2 _ 3 _ _
move #2
4
5
6 2 1 3 _ _
move #3
3
4
5
6 2 1 _ _ _
move #4
2
3
4
5
6 _ 1 _ _ _
and .... move #5
1
2
3
4
5
6 _ _ _ _ _
Got it now ?
The first time I read this problem a little over 2 years ago now, the mere fact that you had to figure out the input and output syntax made me want to solve it.
I have a solution that's not quite optimal yet. I used recursion but the algorithm is dumb, basically trying every configuration possible starting from the first peg and checking each time if the final config has been found and if its "path" is shorter than previous final config found in a previous iteration/recursive call. yea, real ugly. the only intelligent thing it does is to avoid moving the same disk in two successive moves, also moving a disk over a disk of smaller radius, obviously.
Right now I want to add more intelligence to that code to solve it like how I do when I go about it manually, like trying first to put in its final position the disc of greater radius among the discs that get moved between starting and final config. THAT alone should make my code spare thousands and thousands of unnecessary iterations/recursive calls.
And when I'm done with that, if I'm satisfied with its performance - let's say...if it's able to find a solution for a non trivial config of 8 discs and 5 pegs - then I'd like to translate my code from java to C,
yea I'm quite a masochist.
Anyone here does any Machine Learning? Trying to figure out the best way to jump in. Usually don't program outside of work (AngularJS/Java mostly) but it seems Machine Learning is the future.
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
class Greeter2 extends Greeter {}
var greeter = new Greeter("world");
var button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function() {
alert(greeter.greet());
}
document.body.appendChild(button);
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var Greeter = (function () {
function Greeter(message) {
this.greeting = message;
}
Greeter.prototype.greet = function () {
return "Hello, " + this.greeting;
};
return Greeter;
})();
var Greeter2 = (function (_super) {
__extends(Greeter2, _super);
function Greeter2() {
_super.apply(this, arguments);
}
return Greeter2;
})(Greeter);
var greeter = new Greeter("world");
var button = document.createElement('button');
button.textContent = "Say Hello";
button.onclick = function () {
alert(greeter.greet());
};
document.body.appendChild(button);
I didn't make a mistake, it's just that the board formatting deleted the spaces between discs 5 and 1I think you may have made a mistake though,
For the first example, disc 1 and 3 should be on peg 4.
yesJavascript:
Code:function greater(a, b) { if(a != b) return a > b ? a : b; else return 'there are equivalent values'; } function greater3(a, b, c) { var holder = greater(a, b); if(typeof holder != 'string') return greater(holder, c); else return 'there are equivalent values'; } greater3(4,8,3);
Ruby
Code:def greater2(a,b) if a != b a > b ? a : b else puts 'There are equivalent values' return false end end def greater3(a, b, c) if greater2(a, b) != false holder = greater2(a,b) holder2 = greater2(holder, c) end end greater3(1, 4, 4)
most of my training comes from university. no go-to book really. I just have a basic understanding of some key concepts and data structures.What book did you use to learn algorithms? I really want to spend more time on data structures and algorithms, and I will once I get a few other things out of the way.
I didn't make a mistake, it's just that the board formatting deleted the spaces between discs 5 and 1
as you can see in your own msg
don't know if wrapping it up between code tags will correct it. edit: it does
yes
but I wouldn't have bothered testing if the numbers are equal though. just return their value if that's the case.
so javascript and ruby let you write functions that return different types ?
I don't really know either language but I gathered that from your code
most of my training comes from university. no go-to book really. I just have a basic understanding of some key concepts and data structures.
what I find funny about recursion is that it lets you write in a more simplier way what you'll have a hard time expressing iteratively, simplier code yes but most times far from optimal. So learning to tweak code to make it run faster is something I find quite rewarding.
about data structures, learning java probably helped me most, the API is very rich and you cannot really do without it. trees and hashsets are my favorite to work with.
edit: taking a course on/reading about compilers helped me a lot too
function a()
{
return function() {console.log("testing")}
}
var a = a(); /* you call the outer function, it is evaluated and since you didn't call the inner function it is assigned to variable a */
a(); // prints "testing"
/* Write your custom functions here */
static int diameterOfTree(Node root) {
/* For your reference
class Node {
Node left, right;
int data;
Node(int newData) {
left = right = null;
data = newData;
}
}
*/
}
/* Write your custom functions here */
int diameterOfTree(node * root) {
/* For your reference
struct node {
struct node *left,*right;
int val;
};
*/
}
1
6
40
30
65
78
22
38
5
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
* @author LivArmandNoumi_(l-a-n)
*
* Facebook hiring sample test
*
* There are K pegs. Each peg can hold discs in decreasing order of
* radius when looked from bottom to top of the peg. There are N discs
* which have radius 1 to N; Given the initial configuration of the pegs
* and the final configuration of the pegs, output the moves required to
* transform from the initial to final configuration. You are required
* to do the transformations in minimal number of moves.
*
* A move consists of picking the topmost disc of any one of the pegs
* and placing it on top of anyother peg. At anypoint of time, the
* decreasing radius property of all the pegs must be maintained.
*
* Constraints: 1<= N<=8 3<= K<=5
*
* Input Format: N K 2nd line contains N integers. Each integer in the
* second line is in the range 1 to K where the i-th integer denotes the
* peg to which disc of radius i is present in the initial
* configuration. 3rd line denotes the final configuration in a format
* similar to the initial configuration.
*
* Output Format: The first line contains M - The minimal number of
* moves required to complete the transformation. The following M lines
* describe a move, by a peg number to pick from and a peg number to
* place on. If there are more than one solutions, it's sufficient to
* output any one of them. You can assume, there is always a solution
* with less than 7 moves and the initial confirguration will not be
* same as the final one.
*
* Sample Input #00:
*
* 2 3
* 1 1
* 2 2
*
* Sample Output #00:
*
* 3
* 1 3
* 1 2
* 3 2
*
* Sample Input #01:
*
* 6 4
* 4 2 4 3 1 1
* 1 1 1 1 1 1
*
* Sample Output #01:
*
* 5
* 3 1
* 4 3
* 4 1
* 2 1
* 3 1
*
* Sample Input #02
* 6 4
* 1 2 1 2 3 4
* 2 2 1 1 3 4
*
* Sample Output #02
* 7
* 1 3
* 1 4
* 2 4
* 2 1
* 4 2
* 3 2
* 4 1
*
* NOTE: You need to write the full code taking all inputs are from
* stdin and outputs to stdout If you are using "Java", the classname is
* "Solution"
*
*/
/* Represents a move in a path candidate solution by its start and end pegs
and its position or depth in said path which is of type ArrayList<Node> */
class Node {
int from;
int to;
int depth;
public Node(int from, int to, int depth) {
this.from = from;
this.to = to;
this.depth = depth;
}
public int getFrom() {
return from;
}
public int getTo() {
return to;
}
public int getDepth() {
return depth;
}
}
/* The main class*/
public class Solution {
private int discs; /* the number of discs, each one being represented by its radius of type integer between 1 and the value of discs */
private int pegs; /* the number of pegs, each one being represented by an integer between 1 and the value of pegs */
public static List<Node> path; /* will contain the succession of moves solving the problem if there's one for the specified max number of moves*/
private int[] in; /* represents the initial configuration */
private int[] out; /* represents the final configuration */
private int[] tops; /* contains for each peg the radius (hence the number) of its topmost disc */
private int[] notAllowed; /* if notAllowed[p] == r then disc of radius r (r>0) CANNOT be put on top of peg p and if notAllowed[p] == -1 then any disc of radius lower than
getTop(conf,p) can be put atop peg p */
private boolean limitReached; /* set at true whenever the maximum number of moves have been reached for the path that is being built at that moment */
public final int MAX_MOVES; /* the desired maximum number of moves for the solution */
/* Constructor. Initializes what needs to be */
public Solution(int discs, int pegs, int maxMoves) {
this.discs = discs;
this.pegs = pegs;
MAX_MOVES = maxMoves;
init();
}
/* Initialization */
private void init() {
path = new ArrayList<Node>(MAX_MOVES);
limitReached = false; /* not really necessary according to the Java language spec but still */
in = new int[discs];
out = new int[discs];
tops = new int[pegs];
notAllowed = new int[pegs];
for (int i=0; i < pegs; i++) {
notAllowed[i] = -1;
}
}
/* Used for filling up an array of integers with integers */
private void fillTab(int [] tab, int elt, int idx) {
if ((idx >= 0) && (idx < tab.length))
tab[idx] = elt;
}
/* Used for filling up the in array */
public void fillIn(int elt, int idx) {
fillTab(in, elt, idx);
}
/* Used for filling up the out array */
public void fillOut(int elt, int idx) {
fillTab(out, elt, idx);
}
/* Fills up the tops array */
public void fillTops() {
int i;
for (i=0; i < pegs; i++) {
tops[i] = getTop(in, i);
}
}
/* Returns the number of the topmost disc of a given peg for a given configuration */
public int getTop(int [] tab, int peg) {
int i, result = -1;
for(i=0; i < tab.length; i++) {
if (tab[i] == peg + 1) {
result = i;
break;
}
}
return result;
}
/* Performs a move from peg1 to peg2 */
public void move(int [] tabIn, int [] tabTops, int[] tabNotAllowed, int peg1, int peg2) {
int moved_disk = tabTops[peg1];
tabIn[moved_disk] = peg2 + 1;
tabTops[peg1] = getTop(tabIn, peg1);
tabTops[peg2] = moved_disk;
tabNotAllowed[peg1] = moved_disk;
if (tabNotAllowed[peg2] != -1) tabNotAllowed[peg2] = -1;
}
/* The procedure doing the main job.
For a given temporary path being built and the current configuration of
the discs and pegs, attempts to perform a move from peg1 to peg2 if the
limit of moves hasn't been reached yet. If the move is possible, it is
performed and a recursive call is made only if neither the subsequent path
matches the final configuration nor the limit of moves is reached */
private void step(List<Node> l, int [] tabIn, int [] tabTops, int [] tabNotAllowed, int peg1, int peg2) {
List<Node> tmp = new ArrayList<Node>(l), tmp_;
int [] tmpIn = Arrays.copyOf(tabIn, tabIn.length), tmpIn_;
int [] tmpTops = Arrays.copyOf(tabTops, tabTops.length), tmpTops_;
int [] tmpNotAllowed = Arrays.copyOf(tabNotAllowed, tabNotAllowed.length), tmpNotAllowed_;
int top1, top2;
if (tmp.size() >= MAX_MOVES)
limitReached = true;
else if (limitReached)
limitReached = false; /* This is useful for making sure we backtrack for just one step when a recurvive call returns */
if (!limitReached) {
top1 = getTop(tmpIn, peg1);
top2 = getTop(tmpIn, peg2);
if ((top1 != tmpNotAllowed[peg2]) && (top1 >= 0) && ((top1 < top2) || (top2 == -1))) {
move(tmpIn, tmpTops, tmpNotAllowed, peg1, peg2);
tmp.add(new Node(peg1 + 1, peg2 + 1, tmp.size() + 1));
if (tmp.size() == MAX_MOVES) {
limitReached = true;
}
if (Arrays.equals(tmpIn,out)) {
if (path.isEmpty() || (path.size() > tmp.size())) path = tmp;
return;
} else {
if (limitReached)
return;
int i, j;
for (i=0; i < pegs; i++) {
tmp_ = new ArrayList<Node>(tmp);
tmpIn_ = Arrays.copyOf(tmpIn, tmpIn.length);
tmpTops_ = Arrays.copyOf(tmpTops, tmpTops.length);
tmpNotAllowed_ = Arrays.copyOf(tmpNotAllowed, tmpNotAllowed.length);
if (i != peg2) {
for (j=0; j < pegs; j++) {
if (j != i) step(tmp_, tmpIn_, tmpTops_, tmpNotAllowed_, i, j);
}
}
}
}
}
else return;
}
}
// Path builder
public void buildPath() {
List<Node> path_ = new ArrayList<Node>(MAX_MOVES);
int i, j;
double startTime = System.nanoTime();
for (i=0; i < pegs; i++) {
for (j=0; j < pegs; j++) {
if (i != j) step(path_, in, tops, notAllowed, i, j);
path_.clear();
}
}
if (!path.isEmpty()) {
System.out.println("Solution found in " + (System.nanoTime() - startTime) / 1000000000 + " second(s):");
this.display(path);
}
}
/* If a solution was found, displays its moves */
public void display(List<Node> l) {
if (!l.isEmpty()) {
System.out.println(l.get(l.size() - 1).getDepth());
for (Node n: l) {
System.out.println(n.getFrom() + " " + n.getTo());
}
}
}
/* Reads the input data, calculates a solution and eventually displays it */
public static void main(String[] args) {
int i, discs, pegs, maxMoves;
maxMoves = Integer.valueOf(args[0]).intValue();
if (maxMoves > 18) {
System.err.println("Number of moves must not be greater than 18");
System.exit(-1);
}
Scanner scanner = new Scanner(System.in);
discs = scanner.nextInt();
pegs = scanner.nextInt();
if (discs * pegs <= 0) {
System.err.println("Number of discs/pegs must be greater than 0");
System.exit(-2);
}
else {
Solution solution = new Solution(discs, pegs, maxMoves);
for (i=0; i<discs; i++) {
solution.fillIn(scanner.nextInt(), i);
}
for (i=0; i<discs; i++) {
solution.fillOut(scanner.nextInt(), i);
}
solution.fillTops();
solution.buildPath();
}
}
}