puzzle

why is 24 * 60 * 60 * 1000 * 1000 divided by 24 * 60 * 60 * 1000 not equal to 1000 in Java?

why is 24 * 60 * 60 * 1000 * 1000 divided by 24 * 60 * 60 * 1000 not equal to 1000 in Java? ...

Solution to classic "Blockbuster" problem

In the UK throughout the 80's and 90's (70's too I believe!) there was a classic TV program called "Blockbuster", which had a display of hexagons in a honeycomb grid, like this (sorry for blurry pic!): As you can see, there are 5 columns of letters and four rows. 1 person or team is trying to travel horizontally, one is trying to tra...

Routing "paths" through a rectangular array

I'm trying to create my own implementation of a puzzle game. To create my game board, I need to traverse each square in my array once and only once. The traversal needs to be linked to an adjacent neighbor (horizontal, vertical or diagonal). I'm using an array structure of the form: board[n,m] = byte Each bit of the byte represe...

Finding patterns in Puzzle games.

I was wondering, which are the most commonly used algorithms applied to finding patterns in puzzle games conformed by grids of cells. I know that depends of many factors, like the kind of patterns You want to detect, or the rules of the game...but I wanted to know which are the most commonly used algorithms in that kind of problems... ...

Main class block in '{}' never executes

Consider the following code:- class Name { {System.out.println("hi");} public static void main(String[] args) { System.out.println(waffle()); } static boolean waffle() { try { return true; } finally { return false; } } } This never outputs "hi" . Why is thi...

Java metaprogramming conundrum: get all annotations that are itself annotated by a given annotation A

Do you think you are a java wizard? That you are well versed in the secrets of the reflection API? public @interface @a {} public @interface @b {} @Mark public @interface @c {} @Mark public @interface @d {} public @interface @e {} public Class C { @a @b @c @d @e public void x(); } public class Solver { public Annotation[]...

Coding brain teaser to update an array (language agnostic)

All, I need a clever way to implement this algorithm (for work) as quickly and cleanly as possible: I think I've removed all the language specific issues and boiled it down to this: I have two arrays: A and B. A has a list of names in it {Apple, Apple, Banana, Banana, Banana, Carrot, ...} each i-th value has no upper limit on the num...

What can be the efficient approach to solve the 8 puzzle problem?

The 8-puzzle is a square board with 9 positions, filled by 8 numbered tiles and one gap. At any point, a tile adjacent to the gap can be moved into the gap, creating a new gap position. In other words the gap can be swapped with an adjacent (horizontally and vertically) tile. The objective in the game is to begin with an arbitrary config...

Code Golf - Word Scrambler

Please answer with the shortest possible source code for a program that converts an arbitrary plaintext to its corresponding ciphertext, following the sample input and output I have given below. Bonus points* for the least CPU time or the least amount of memory used. Example 1: Plaintext: The quick brown fox jumps over the lazy dog. ...

How to optimally solve the flood fill puzzle?

I like playing the puzzle game Flood-It, which can be played online at: http://floodit.appspot.com/ It's also available as an iGoogle gadget. The aim is to fill the whole board with the least number of successive flood-fills. I'm trying to write a program which can solve this puzzle optimally. What's the best way to approach this pr...

Compile time sizeof_array without using a macro

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(AR...

Java Puzzler - Can anyone explain this behavior ?

abstract class AbstractBase { abstract void print(); AbstractBase() { // Note that this call will get mapped to the most derived class's method print(); } } class DerivedClass extends AbstractBase { int value = 1; @Override void print() { System.out.println("Value in DerivedClass: " + va...

duplicacy problems while creating a sudoku puzzle

I am trying to create my own normal 9x9 sudoku puzzle. I divided the problem into two parts - creating a fully filled sudoku, and removing unnecessary numbers from the grid Right now, I am stuck with the first part. This is the algorithm I use in brief: a) first of all I choose a number (say 1), generate a random cell position,...

Help understanding Sieve of Eratosthenes implementation

This is boring, I know, but I need a little help understanding an implementation of the Sieve of Eratosthenes. It's the solution to this Programming Praxis problem. (define (primes n) (let* ((max-index (quotient (- n 3) 2)) (v (make-vector (+ 1 max-index) #t))) (let loop ((i 0) (ps '(2))) (let ((p (+ i i 3)) (startj...

Ideas on what to do/lecture on for Programming Club in college?

Hey guys, This semester I started a programming club at my university since we did not have an existing similar group with the idea that it would be a place to hang out for computer science majors and partake in activities such as lectures on interesting topics, social events and group projects. However as this is the first semester st...

Link list algorithm to find pairs adding up to 10

Can you suggest an algorithm that find all pairs of nodes in a link list that add up to 10. I came up with the following. Algorithm: Compare each node, starting with the second node, with each node starting from the head node till the previous node (previous to the current node being compared) and report all such pairs. I think thi...

Simple Java Map puzzle

What is the best implementation for this general-purpose library method? public static <K, V> boolean containsEntry( Map<K, V> map, K key, V value) {} Criteria for judging this puzzle, as with most coding puzzles, are in this order: Completeness Correctness Performance Beauty Receipt of PayPal contribution EDIT: Well, since i...

C Puzzle - play with types

Please check the below program. #include <stdio.h> struct st { int a ; } fn () { struct st obj ; obj.a = 10 ; return obj ; } int main() { struct st obj = fn() ; printf ("%d", obj.a) ; } Following are the questions What is the output of the program? Where is ';' terminating the declaration of 'struct st'? By ISO IE...

Binary search in C with recursive function accepting only length

I am solving "Programming Pearls" exercises. 4.11 say: Write and prove the correctness of a recursive binary search function in C or C++ with this declaration: int binarysearch(DataType x[], int n); Use this function alone; do not call any other recursive function. I came up with: int bsearch_rec_array_only(int key...

Prolog - member predicate one-liner

Interview question! This is how you normally define the member relation in Prolog: member(X, [X|_]). % member(X, [Head|Tail]) is true if X = Head % that is, if X is the head of the list member(X, [_|Tail]) :- % or if X is a member of Tail, member(X, Tail). % ie. if member(X, Tail) is true. D...