algorithm,optimization,graph,puzzle

Choose the node with the lowest sum of incoming weights. O(V^2) time complexity, where V is the number of nodes. O(1) memory complexity. Pseudocode: min_dist = INF min_node = null for node in graph: // O(V) loops sum = 0 for neighbor in neighbors(node): // O(V) loops sum += dist(node,...

Your code is very hard to follow, so I wrote my own code to debug this and your program although obscure and hard to follow is completely correct for valid inputs, you are just not handling all the cases very well thus you are in a while loop forever. Not...

Just compute how many squares you need to fill up n, and how many to fill out m, and multiply. In your example, the small square is length 4, so you need 2 for m and 2 for n, so you need 2x2 = 4 squares. To get the number...

swift,recursion,puzzle,n-queens

Looks like you are having trouble grasping the concept of recursion. Recursion doesn't mean everything becomes a recursive call. In function dropQueen you are using recursive calls as if they were gotos. Especially this one: dropQueen(atRow: r-1, c: q.col+1) That's clearly an attempt to do backtracking. Make up your mind;...

First, change onclick="shiftPuzzlepieces();" to onclick="shiftPuzzlepieces(this);" This will delete the item when clicked, and adds it to the end: function shiftPuzzlepieces(el) { //Swap function var blank = document.getElementById("positionblank"); var temp = el.parentNode.insertBefore(document.createElement('a'), el); el.parentNode.insertBefore(el, blank); el.parentNode.insertBefore(blank, temp); el.parentNode.removeChild(temp); } Demo: //jscript: function load() { var puzzle = '<div id="slidingpuzzleContainer4x4">'; for (var...

algorithm,puzzle,heuristics,tiling

That's an Exact Cover problem, with a nice structure too, usually, depending on the pieces. I don't know about any heuristic algorithms, but there are several exact options that should work well. As usual with Exact Covers, you can use Dancing Links, a way to implement Algorithm X efficiently. Less...

java,math,calendar,switch-statement,puzzle

(year-1984)%10/2 equals (year-984)%10/2， (year-84)%10/2 ， (year-4)%10/2， (year+6)%10/2 and the year >0...

algorithm,math,dynamic-programming,puzzle

How about checking the number of coins as a function of K%9? Then you can translate it to the function of log(K). Let K%9 = r, i.e. there is an integer n such that K = 9n + r, and r is between 0 and 8 inclusive. r=0: we need...

android,algorithm,puzzle,sudoku

You can replace steps 1-6 by starting with a known valid filled grid, and then applying transformations to it that keep the constraints intact. For example, you can start off with this grid, which is easy to generate by setting grid[i][j] to ((i + j) % 5) + 1: 1...

Something simple to give you some idea: y_map = {'A':0, 'B':1, 'C':2, 'D':3, 'E':4, 'F':5} def get_coords(coord_str): x,y = coord_str.split() x -= 1 y = y_map[y] return (x,y) user_coords = "A 4" user_input = 1 user_coords = get_coords(user_coords) grid[user_coords[1]][user_coords[0]] = user_input Needs a bit of work, but I'm sure it's...

The number of ways for n candies is the sum of the n-1th row of pascal's triangle.

Your code has several problems. At the end of your swap_puzzle() function, inside the while loop, you call print_puzzle(). print_puzzle calls shuffle(). That's why everything is shuffled instead of having just a swap operation. To fix this you must decouple these 2 functions: printing is one thing, shuffling is another...

Swap range in for loops as: def chekbaord(board): ok = True for i in range (0, len(board)): # board[0] changed to board s = "" for j in range(0, len(board[0])): # board changed to board[0] s = s +board[i][j] if "111" in s or "000" in s: print("not possible") ...

Not much math involved. More like, manipulation of collections. Divide 81 cows into 9 lots of increasing size. [ 1, 2, 3, 4, 5, 6, 7, 8, 9] [10, 11, 12, 13, 14, 15, 16, 17, 18] [19, 20, 21, 22, 23, 24, 25, 26, 27] [28, 29, 30, 31,...

Here's a possible solution, in R, using recursion . It would be interesting to build a tree of all the possible paths # For every input number n (n < 10) # there is an output number m such that: # m's first digit is n # m is an...

javascript,jquery,html,jquery-ui,puzzle

I see you are using jQuery. You might want to check out the snap-puzzle, also have a look at their Github repository. I have used this plugin a while ago and it worked as advertised. This demo could be interesting for you if you want to go without an extra...

<?php function rollingdice($input1,$input2) { $a = array_count_values($input1); $b = array_count_values($input2); if(array_diff_assoc($a,$b) || array_diff_assoc($b,$a)) { return 'Unlucky';} return 'Lucky'; } ...

As always, there is a "hacky", easy way to do this, and then there is more elegant but one that requires significant changes to your code. Hacky way To accomplish this as fast and dirty as possible, I would go with parsing id-s of pieces to check if they are...

First: You can't have two or more elements with the same ID, an ID must be unique. But you may have data attached to your puzzle pieces: <img id="piece_01" .. /> <td id="place_01" data="piece_01" ... /> or just the other way round, attaching place ID to piece. Now you can...

python,combinations,combinatorics,puzzle,traveling-salesman

Problem is called maximum matching in bipartite graph or assignment problem. It is done by Hungarian algorithm. On Wikipedia page are implementations in different languages. There is python library munkres with an implementation....

Turns out I misunderstood the solution. Here's the complete solution: def firstWillWin(self, values): """ :param values: a list of integers :return: a boolean which equals to True if the first player will win """ n = len(values) if n%2 == 0 and self.firstWillWinEven(values): return True return self.firstWillWinNormalCase(values) If odd_s !=...

In shiftPuzzlePieces(el) function, you should first check if el is adjacent to blank piece. If so, then shift it. Here is the jsFiddle function shiftPuzzlepieces(el) { var elIndex=0; var child=el; while((child=child.previousSibling)!=null) elIndex++; //position of el is stored in elIndex var blankIndex=0; var blank = document.getElementById("positionblank"); child=blank; while((child=child.previousSibling)!=null) blankIndex++; //position of...

You are correct; this code is correct as written, and the Codewars guys are boneheads. They probably want you to switch the names around the other way. The OO metaphor for calling a function on an object and passing an argument is "tell the object to perform the action on...