puzzle

Execute periodic tasks using a timer with a different interval as intended

Hi, I am forced to execute a periodic task using a timer that is invoked at a different interval as the period I'd like to execute this task. Note that the timeout does not occur 100% accurately; i.e. timeout(javax.ejb.Timer timer) in the code below might be invoked at intervals 100, 98, 105ms etc. So, I came up with this, which is ...

Help with C puzzle

I found a site with some complicated C puzzles. Right now I'm dealing with this: The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work. #include <stdio.h> int main() { int i; int n = 20; for( i = 0; i < n; i-- ) printf("-"); return 0;...

Faster or more memory-efficient solution in Python for this Codejam problem.

I tried my hand at this Google Codejam Africa problem (the contest is already finished, I just did it to improve my programming skills). The Problem: You are hosting a party with G guests and notice that there is an odd number of guests! When planning the party you deliberately invited only couples and gave each couple a...

catDog string problem at Codingbat.com

Could anyone check my solution? I want to return true if the string "cat" and "dog" appear the same number of times in the given string. There are various strings with different numbers of "cat" and "dog". public boolean catDog(String str) { int catAnswer = 0; int dogAnswer = 0; int cat_Count = 0; int dog_Count = 0; for (...

cat file | ... vs ... <file

Is there a case of ... or context where cat file | ... behaves differently than ... <file? ...

How can I solve the Log Pile wooden puzzle with a computer program?

Can anyone suggest how to solve the Log Pile wooden puzzle using a computer program? See here to visualise the puzzle: http://www.puzzlethis.co.uk/products/madcow/the_log_pile.htm The picture only shows some of the pieces. The full set of 10 pieces are configured as follows with 1 representing a peg, -1 representing a hole and 0 repres...

Puzzle: find the minimum number of weights

I came across this question: say given two weights 1 and 3, u can weigh 1,2 (by 3-1),3,4 (by 3+1) (using both sides of balance). Now find the minimum number of weights so that you can measure 1 to 1000. So the answer was 1,3,9,27... I want to know how do you arrive at such a solution means powers of 3. What is the thought process? Sou...

Help me finish this Python 3.x self-challenge.

This is not homework. I saw this article praising Linq library and how great it is for doing combinatorics stuff, and I thought to myself: Python can do it in a more readable fashion. After half hour of dabbing with Python I failed. Please finish where I left off. Also, do it in the most Pythonic and efficient way possible please. fro...

How can I ensure that when I shuffle my puzzle I still end up with an even permutation?

I'm interested making an implementation of the 14-15 puzzle: I'm creating an array with the values 0 - 15 in increasing order: S = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } Now, what I want to do is shuffle them to create a new instance of the puzzle. However, I know that if I create a board with an "odd permutation" ...

What category of combinatorial problems appear on the logic games section of the LSAT?

EDIT: See http://stackoverflow.com/questions/318888/solving-who-owns-the-zebra-programmatically for a similar class of problem There's a category of logic problem on the LSAT that goes like this: Seven consecutive time slots for a broadcast, numbered in chronological order I through 7, will be filled by six song tapes-G, H, L, O, ...

Word Jumble Algorithm

Given a word jumble (i.e. ofbaor), what would be an approach to unscramble the letters to create a real word (i.e. foobar)? I could see this having a couple of approaches, and I think I know how I'd do it in .NET, but I curious to see what some other solutions look like (always happy to see if my solution is optimal or not). This isn't...

Is Odersky serious with "bills !*&^%~ code!" ?

In his book programming in scala (Chapter 5 Section 5.9 Pg 93) Odersky mentioned this expression "bills !*&^%~ code!" In the footnote on same page: "By now you should be able to figure out that given this code,the Scala compiler would invoke (bills.!*&^%~(code)).!()." That's a bit to cryptic for me, could someone explain what's go...

What's the scope of a Javascript variable declared in a for() loop?

Check out the following snippet of HTML/Javascript code: <html> <head> <script type="text/javascript"> var alerts = []; for(var i = 0; i < 3; i++) { alerts.push(function() { document.write(i + ', '); }); } for (var j = 0; j < 3; j++) { (alerts[j])(); } for (var i = 0; i < 3; i++) { (alerts[i])(); } </script> </head><body><...

what does this javascript code do?

Found this code in our code base the other day. Not sure what it is used for. Any guesses? function checkIntegerRange(x) { return ((x >= 0) && (x < 2020202020)) || (x == 2147483647) || (x == 4294967295); } ...

How would I find all sets of N single-digit, non-repeating numbers that add up to a given sum in PHP?

Let's say I want to find all sets of 5 single-digit, non-repeating numbers that add up to 30... I'd end up with [9,8,7,5,1], [9,8,7,4,2], [9,8,6,4,3], [9,8,6,5,2], [9,7,6,5,3], and [8,7,6,5,4]. Each of those sets contains 5 non-repeating digits that add up to 30, the given sum. Any help would be greatly appreciated. Even just a starting...

How to create a magic square in PHP?

I'd like to try my hand at creating a Magic Square in PHP (i.e. a grid of numbers that all add up to the same value), but I really don't know where to start. I know of the many methods that create magic square, such as starting "1" at a fixed position, then moving in a specific direction with each iteration. But that doesn't create a tru...

Finding subsets that can be completed to tuples without duplicates

We have a collection of sets A_1,..,A_n. The goal is to find new sets for each of the old sets. newA_i = {a_i in A_i such that there exist (a_1,..,a_n) in (A1,..,An) with no a_k = a_j for all k and j} So in words this says that we remove all the elements from A_i that can't be used to form a tuple (a_1,..a_n) from the sets (A_1,..,A_n...

Requiring clients to solve computational puzzles...

Not that I need it, but it was interesting to hear someone speak about their server and protecting it from DOS attack by having a puzzle that the client must solve before the server will do anything (it doesnt do allocations or make a session unless solved). The person also said puzzles can be made to take a quick amount of time or long...

Using search to solve the 15-puzzle

I'm trying to write a 4 x 4 grid using vertical bars and underscore. I have a class for the puzzle, but i want to know what fields and methods i can use to represent and manipulate a configuration for the puzzle. Reference: Fifteen puzzle ...

SQL: how to get all the distinct characters in a column, across all rows

Is there an elegant way in SQL Server to find all the distinct characters in a single varchar(50) column, across all rows? Bonus points if it can be done without cursors :) For example, say my data contains 3 rows: productname ----------- product1 widget2 nicknack3 The distinct inventory of characters would be "productwigenka123" ...