puzzle

Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7

What is simple solution What is effective solution to less minimum memory and(or) cpu speed? ...

How to find out which numbers occur the most in a given group of numbers?

Suppose we have a vector/array in C++ and we wish to count which of these N elements has maximum repetitive occurrences and output the highest count. Which algorithm is best suited for this job. example: int a = { 2, 456, 34, 3456, 2, 435, 2, 456, 2} the output is 4 because 2 occurs 4 times. That is the maximum number of times 2 occu...

Find the prefix substring which gives best compression

Problem: Given a list of strings, find the substring which, if subtracted from the beginning of all strings where it matches and replaced by an escape byte, gives the shortest total length. Example: "foo", "fool", "bar" The result is: "foo" as the base string with the strings "\0", "\0l", "bar" and a total length of 9 bytes. "\0" is ...

C# Potential Interview Question…Too hard?

Without running this code, identify which Foo method will be called: class A { public void Foo( int n ) { Console.WriteLine( "A::Foo" ); } } class B : A { /* note that A::Foo and B::Foo are not related at all */ public void Foo( double n ) { Console.WriteLine( "B::Foo" ); } } static void Main( string[]...

Django/Python - Grouping objects by common set from a many-to-many relationships

This is a part algorithm-logic question (how to do it), part implementation question (how to do it best!). I'm working with Django, so I thought I'd share with that. In Python, it's worth mentioning that the problem is somewhat related to how-do-i-use-pythons-itertoolsgroupby. Suppose you're given two Django Model-derived classes: fro...

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

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 a...

A Project Euler Puzzler (specifically in PHP)

There is another recent Project Euler question but I think this is a bit more specific (I'm only really interested in PHP based solutions) so I'm asking anyway. Question #5 tasks you with: "What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?" Now, I have solved it twice. Once very inefficiently and ...

Scheduling a worker with overlapping periodic tasks

With one worker, who can only perform one task at a time (but can switch between tasks instantly) Given a list of tasks, -- defined as "n seconds, every m seconds" (eg, 5 seconds every 3600 seconds) How could I find the best starting times and count for each task? If every task were "1 second, every 60 seconds", each would have an uni...

Puzzle: Overload a C++ function according to the return value

We all know that you can overload a function according to the parameters: int mul(int i, int j) { return i*j; } std::string mul(char c, int n) { return std::string(n, c); } Can you overload a function according to the return value? Define a function that returns different things according to how the return value is used: int n = mul(...

How do I programmatically return the max of two integers without...

... using any comparison operators... and without using if, else, etc. ...

ACM Problem: Coin-Flipping, help me identify the type of problem this is.

I'm practicing for the upcoming ACM programming competition in a week and I've gotten stumped on this programming problem. The problem is as follows: You have a puzzle consisting of a square grid of size 4. Each grid square holds a single coin; each coin is showing either heads (H) and tails (T). One such puzzle is shown here: H...

multiplication chains that result in a constant modulo a power of 2

Is there a practical algorithm that gives "multiplication chains" To clarify, the goal is to produce a multiplication change of an arbitrary and exact length Multiplication chains of length 1 are trivial. A "multiplication chain" would be defined as 2 numbers, {start} and {multiplier}, used in code: Given a pointer to array of size ...

Puzzle: Find the most common entry in an array

You are given a 32-bit unsigned integer array with length up to 232, with the property that more than half of the entries in the array are equal to N, for some 32-bit unsigned integer N. Find N looking at each number in the array only once and using at most 2 kB of memory. Your solution must be deterministic, and guaranteed to find N. ...

What is the output of this program, and what does it return to the OS ?

It's kind of a C puzzle. You have to tell if the program finish its execution, if so, how much time it takes to run and what it returns to the OS. static unsigned char buffer[256]; int main(void) { unsigned char *p, *q; q = (p = buffer) + sizeof(buffer); while (q - p) { p = buffer; while (!++*p++); } return...

Triangle problem

Hey!! I am trying to resolve Euler Problem 18 -> http://projecteuler.net/index.php?section=problems&amp;id=18 I am trying to do this with c++ (I am relearning it and euler problems make for good learning/searching material) #include <iostream> using namespace std; long long unsigned countNums(short,short,short array[][15],short,boo...

Calculate exact result of complex throw of two D30

Okay, this bugged me for several years, now. If you sucked in statistics and higher math at school, turn away, now. Too late. Okay. Take a deep breath. Here are the rules. Take two thirty sided dice (yes, they do exist) and roll them simultaneously. Add the two numbers If both dice show <= 5 or >= 26, throw again and add the result to...

Solving "Who owns the Zebra" programmatically?

Edit: this puzzle is also known as "Einstein's Riddle" The Who owns the Zebra is an example of a classic set of puzzles and I bet that most people on Stack Overflow can solve it with pen and paper. But what would a programmatic solution look like? Based on the clues listed below... There are five houses. Each house has its own unique...

Ordering a dictionary to maximize common letters between adjacent words

This is intended to be a more concrete, easily expressable form of my earlier question. Take a list of words from a dictionary with common letter length. How to reorder this list tto keep as many letters as possible common between adjacent words? Example 1: AGNI, CIVA, DEVA, DEWA, KAMA, RAMA, SIVA, VAYU reorders to: AGNI, CIVA, SI...

What are some good websites for programming puzzles?

Programing puzzles can be a great way to practice your skills and kill time between projects. What sources do you use for programing puzzles? ...

Automatic spell checking of words in a text

[EDIT]In Short: How would you write an automatic spell checker? The idea is that the checker builds a list of words from a known good source (a dictionary) and automatically adds new words when they are used often enough. Words which haven't been used a while should be phased out. So if I delete part of a scene which contains "Mungrohype...