puzzle

Programming Riddle: How might you translate an Excel column name to a number?

I was recently asked in a job interview to resolve a programming puzzle that I thought it would be interesting to share. It's about translating Excel column letters to actual numbers, if you recall, Excel names its columns with letters from A to Z, and then the sequence goes AA, AB, AC... AZ, BA, BB, etc. You have to write a function th...

Programming Riddle: Counting down without subtracting.

Ok, goal by example : a command-line app that does this: Countdown.exe 7 prints 7 6 5 4 3 2 1 No form of subtracting (including use of the minus sign) or string reverse what so ever is allowed. waaaaay too easy apparently :-) An overview of the answers (the principles at least) By adding and recursion By using modulo By pushing an...

Riddle: The Square Puzzle

Last couple of days, I have refrained myself from master's studies and have been focusing on this (seemingly simple) puzzle: There is this 10*10 grid which constitutes a square of 100 available places to go. The aim is to start from a corner and traverse through all the places with respect to some simple "traverse rules" and reach n...

Online Puzzles and games for learning boolean algebra

I'm looking for some online game, where it tests our ability in boolean algebra. My cousin is young and has just learnt using logic gates. Is there any thing interesting available out there for him to practice online? ...

Linear Time Voting Algorithm. I don't get it.

As I was reading this (Find the most common entry in an array), the Boyer and Moore's Linear Time Voting Algorithm was suggested. If you follow the link to the site, there is a step by step explanation of how the algorithm works. For the given sequence, AAACCBBCCCBCC it presents the right solution. When we move the pointer forward...

Eric Lippert's challenge "comma-quibbling", best answer?

I wanted to bring this challenege to the attention of the stackoverflow community. The original problem and answers are here. BTW, if you did not follow it before, you should try to read Eric's blog, it is pure wisdom. Summary: Write a function that takes a non-null IEnumerable and returns a string with the following characteristics: ...

Partial, or wrapped multiplication - can anyone identify this function ?

I am hoping for insight into what looks like a partial multiplication. #define LOW(x) ((x)&0xffffffff) #define HIGH(x) ((x)>>32) unsigned long long NotMultiply(unsigned long long x, unsigned long long y) { return HIGH(x)*HIGH(y) + LOW(x)*LOW(y); } This function is iterated multiple times, as follows: unsigned long long DoBusy...

Getting a specific digit from a ratio expansion in any base (nth digit of x/y)

Is there an algorithm that can calculate the digits of a repeating-decimal ratio without starting at the beginning? I'm looking for a solution that doesn't use arbitrarily sized integers, since this should work for cases where the decimal expansion may be arbitrarily long. For example, 33/59 expands to a repeating decimal with 58 dig...

Solving Nonograms (Picross)

Hey, it's Friday afternoon, let's have a fun puzzle/algorithm problem to solve. One of my favorite Nintendo DS games is Picross DS. The game is quite simple, it involves solving puzzles called Nonograms. You can try a simple online Picross clone here: TylerK's Picross. Nonograms are a grid, with sequences of numbers defined for every r...

Code Golf: Automata

I made the ultimate laugh generator using these rules. Can you implement it in your favorite language in a clever manner? Rules: On every iteration, the following transformations occur. H -> AH A -> HA AA -> HA HH -> AH AAH -> HA HAA -> AH n = 0 | H n = 1 | AH n = 2 | HAAH n = 3 | AHAH n = 4 | HAAHHAAH n = 5 | AHAHHA n...

Check if a number is divisible by 3

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) out...

Implementing shuffle on the celestial jukebox

How would one implement shuffle for the "Celestial Jukebox"? More precisely, at each time t, return an uniform random number between 0..n(t), such that there are no repeats in the entire sequence, with n() increasing over time. For the concrete example, assume a flat-rate music service which allows playing any song in the catalog by ...

Fastest algorithm for circle shift N sized array for M position

What is the fastest algorithm for circle shifting array for m positions? For example [3 4 5 2 3 1 4] shift m = 2 positions should be [1 4 3 4 5 2 3] Thanks a lot ...

Pixies in the custard swamp puzzle

(With thanks to Rich Bradshaw) I'm looking for optimal strategies for the following puzzle. As the new fairy king, it is your duty to map the kingdom's custard swamp. The swamp is covered in an ethereal mist, with islands of custard scattered throughout. You can send your pixies across the swamp, with instructions to fly low or high...

Decimal Value Check if Zero

I am trying to write a division method, which accepts 2 parameters. public static decimal Divide(decimal divisor, decimal dividend) { return dividend / divisor; } Now, if divisor is 0, we get cannot divide by zero error, which is okay. What I would like to do is check if the divisor is 0 and if it is, convert it to 1. Is there wa...

How can this Java code compile?

A colleague came across some code that looked like this and couldn't understand how it could ever compile: class FooClass { public static void bar(String arg) { System.out.println("arg = " + arg); http://www.google.com System.out.println("Done!"); } } Basically, there was a random URL pasted in the middle of a metho...

Rounding issues with allocating dollar amounts across multiple people

What is the best way to solve this problem in code? The problem is that I have 2 dollar amounts (known as a pot), that need to be allocated to 3 people. Each person gets a specific amount that comes from both pots and the rates must be approximately the same. I keep coming across rounding issues where my allocations either add up to...

Good book for puzzle

I am great fan of book like "Java Puzzler". I want to know about other book titles which provides similar contents like "Java Puzzler" (Mostly for java). Same time looking for good general puzzle book also. Please suggest some titles. ...

KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm.

This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x <= maxval and x(1) + ... + x(ncells) = targetsum. Having tested several of the more promising answers, I'm going to award the answer-prize to Lennart Regebro, because: ...

Why won't it remove from the set?

This bug took me a while to find... Consider this method: public void foo(Set<Object> set) { Object obj=set.iterator().next(); set.remove(obj) } I invoke the method with a non-empty hash set, but no element will be removed! Why would that be? ...