puzzle

How to generate Sudoku boards with unique solutions

吃可爱长大的小学妹 提交于 2019-11-26 15:08:15
How do you generate a Sudoku board with a unique solution? What I thought was to initialize a random board and then remove some numbers. But my question is how do I maintain the uniqueness of a solution? TMS Easy: Find all solutions with an efficient backtracking algorithm. If there is just one solution, you are done. Otherwise if you have more than one solution, find a position at which most of the solutions differ. Add the number at this position. Go to 1. I doubt you can find a solution that would be much faster than this. Here is the way my own SuDoKu program does it: Start with a complete

Algorithm to generate anagrams

落花浮王杯 提交于 2019-11-26 12:03:55
What would be the best strategy to generate anagrams. An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; ex. Eleven plus two is anagram of Twelve plus one A decimal point is anagram of I'm a dot in place Astronomers is anagram of Moon starers At first it looks straightforwardly simple, just to jumble the letters and generate all possible combinations. But what would be the efficient approach to generate only the words in dictionary. I came across this page, Solving anagrams

Finding a single number in a list [duplicate]

霸气de小男生 提交于 2019-11-26 11:12:28
This question already has an answer here: How to find the only number in an array that doesn't occur twice [duplicate] 5 answers What would be the best algorithm for finding a number that occurs only once in a list which has all other numbers occurring exactly twice. So, in the list of integers (lets take it as an array) each integer repeats exactly twice, except one. To find that one, what is the best algorithm. The fastest (O(n)) and most memory efficient (O(1)) way is with the XOR operation. In C: int arr[] = {3, 2, 5, 2, 1, 5, 3}; int num = 0, i; for (i=0; i < 7; i++) num ^= arr[i]; printf

Check if a number is divisible by 3 [closed]

泪湿孤枕 提交于 2019-11-26 09:33:52
问题 Write code to determine if a number is divisible by 3. The input to the function is a single bit, 0 or 1, and the output should be 1 if the number received so far is the binary representation of a number divisible by 3, otherwise zero. Examples: input \"0\": (0) output 1 inputs \"1,0,0\": (4) output 0 inputs \"1,1,0,0\": (6) output 1 This is based on an interview question. I ask for a drawing of logic gates but since this is stackoverflow I\'ll accept any coding language. Bonus points for a

Compile time sizeof_array without using a macro

扶醉桌前 提交于 2019-11-26 09:00:44
问题 This is just something that has bothered me for the last couple of days, I don\'t think it\'s possible to solve but I\'ve seen template magic before. Here goes: To get the number of elements in a standard C++ array I could use either a macro (1), or a typesafe inline function (2): (1) #define sizeof_array(ARRAY) (sizeof(ARRAY)/sizeof(ARRAY[0])) (2) template <typename T> size_t sizeof_array(const T& ARRAY){ return (sizeof(ARRAY)/sizeof(ARRAY[0])); } As you can see, the first one has the

Efficiently reverse the order of the words (not characters) in an array of characters

那年仲夏 提交于 2019-11-26 08:02:08
问题 Given an array of characters which forms a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. Example input and output: >>> reverse_words(\"this is a string\") \'string a is this\' It should be O(N) time and O(1) space ( split() and pushing on / popping off the stack are not allowed). The puzzle is taken from here. 回答1: A solution in C/C++: void swap(char* str, int i, int j){ char t = str[i]; str[i] = str[j]; str[j] = t; } void reverse

Algorithm to determine if array contains n…n+m?

大城市里の小女人 提交于 2019-11-26 07:54:55
问题 I saw this question on Reddit, and there were no positive solutions presented, and I thought it would be a perfect question to ask here. This was in a thread about interview questions: Write a method that takes an int array of size m, and returns (True/False) if the array consists of the numbers n...n+m-1, all numbers in that range and only numbers in that range. The array is not guaranteed to be sorted. (For instance, {2,3,4} would return true. {1,3,1} would return false, {1,2,4} would

How to find all combinations of coins when given some dollar value

橙三吉。 提交于 2019-11-26 05:49:53
问题 I found a piece of code that I was writing for interview prep few months ago. According to the comment I had, it was trying to solve this problem: Given some dollar value in cents (e.g. 200 = 2 dollars, 1000 = 10 dollars), find all the combinations of coins that make up the dollar value. There are only pennies (1¢), nickels (5¢), dimes (10¢), and quarters (25¢) allowed. For example, if 100 was given, the answer should be: 4 quarter(s) 0 dime(s) 0 nickel(s) 0 pennies 3 quarter(s) 1 dime(s) 0

How to test randomness (case in point - Shuffling)

放肆的年华 提交于 2019-11-26 05:27:42
问题 First off, this question is ripped out from this question. I did it because I think this part is bigger than a sub-part of a longer question. If it offends, please pardon me. Assume that you have a algorithm that generates randomness. Now how do you test it? Or to be more direct - Assume you have an algorithm that shuffles a deck of cards, how do you test that it\'s a perfectly random algorithm? To add some theory to the problem - A deck of cards can be shuffled in 52! (52 factorial)

How to generate Sudoku boards with unique solutions

拟墨画扇 提交于 2019-11-26 04:09:20
问题 How do you generate a Sudoku board with a unique solution? What I thought was to initialize a random board and then remove some numbers. But my question is how do I maintain the uniqueness of a solution? 回答1: Easy: Find all solutions with an efficient backtracking algorithm. If there is just one solution, you are done. Otherwise if you have more than one solution, find a position at which most of the solutions differ. Add the number at this position. Go to 1. I doubt you can find a solution