puzzle

How do I compute the cartesian product of n sequences in F#?

有些话、适合烂在心里 提交于 2019-11-27 22:59:25
I was given a puzzle as a present. It consists of 4 cubes, arranged side by side. The faces of each cube are one of four colours. To solve the puzzle, the cubes must be orientated so that all four cubes' tops are different, all their fronts are different, all their backs are different and all their bottom's are different. The left and right sides do not matter. My pseudo-code solution was: Create a representation of each cube. Get all the possible orientations of each cube (there are 24 for each). Get all the possible combinations of orientations of each cube. Find the combination of

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

狂风中的少年 提交于 2019-11-27 22:56:52
why is 24 * 60 * 60 * 1000 * 1000 divided by 24 * 60 * 60 * 1000 not equal to 1000 in Java? Because the multiplication overflows 32 bit integers. In 64 bits it's okay: public class Test { public static void main(String[] args) { int intProduct = 24 * 60 * 60 * 1000 * 1000; long longProduct = 24L * 60 * 60 * 1000 * 1000; System.out.println(intProduct); // Prints 500654080 System.out.println(longProduct); // Prints 86400000000 } } Obviously after the multiplication has overflowed, the division isn't going to "undo" that overflow. You need to start with 24L * 60 * ... because the int overflows.

String Reduction - Programming Contest . Solution needed

流过昼夜 提交于 2019-11-27 20:35:40
问题 I have a question which asks us to reduce the string as follows. The input is a string having only A , B or C . Output must be length of the reduced string The string can be reduced by the following rules If any 2 different letters are adjacent, these two letters can be replaced by the third letter. Eg ABA -> CA -> B . So final answer is 1 (length of reduced string) Eg ABCCCCCCC This doesn't become CCCCCCCC , as it can be reduced alternatively by ABCCCCCCC -> AACCCCCC -> ABCCCCC -> AACCCC ->

Place random non-overlapping rectangles on a panel

只愿长相守 提交于 2019-11-27 20:13:57
I've a panel of size X by Y. I want to place up to N rectangles, sized randomly, upon this panel, but I don't want any of them to overlap. I need to know the X, Y positions for these rectangles. Algorithm, anyone? Edit : All the N rectangles are known at the outset and can be selected in any order. Does that change the procedure? You can model this by a set of "free" rectangles, starting with single one with coordinates of 0,0, size (x, y). Each time you need to add one more rectangle, choose one of remaining "free" rectangles, generate new rectangle (with top-left coordinate and size such

cat file | … vs … <file

坚强是说给别人听的谎言 提交于 2019-11-27 17:32:24
问题 Is there a case of ... or context where cat file | ... behaves differently than ... <file ? 回答1: When reading from a regular file, cat is in charge of reading the data, performs it as it pleases, and might constrain it in the way it writes it to the pipeline. Obviously, the contents themselves are preserved, but anything else could be tainted. For example: block size and data arrival timing. Additionally, the pipe in itself isn't always neutral: it serves as an additional buffer between the

hello world in C without semicolons and without IF/WHILE/FOR statements [closed]

强颜欢笑 提交于 2019-11-27 17:14:36
My friend says it's possible to write a C program that will print "hello world" without IF/WHILE/FOR and without semicolons. After minimal research I told her it was not possible. Is it possible? I've been trying to find a "portable" way of stealing a semicolon from an include file. This works under Linux: int main(int ac, char **av) { #define typedef #define uint8_t a[printf("hello world\n")] #include <stdint.h> } This causes the one typedef unsigned char uint8_t to become my printf. Another trick that worked was to #define away every standard stdint type such that stdint.h reduces to a bunch

How to print all possible balanced parentheses for an expression?

天大地大妈咪最大 提交于 2019-11-27 15:18:08
问题 For example, with elements a,b,c,d , there are 5 possible ways to take neighboring elements and reduce them into a single element, where exactly two elements must be combined at a time (below represented by parentheses): (((ab)c)d), ((a(bc))d), ((ab)(cd)), (a((bc)d)) and (a(b(cd))) The first example multiplies a*b , then multiplies that product by c , then multiplies that product by d . The second example first multiplies b*c , then multiplies that product by a , then multiplies that product

Solving a logic puzzle using Prolog

∥☆過路亽.° 提交于 2019-11-27 11:22:36
问题 The criminal is one of A, B, C and D. A says: "It's not me" B says: "It's D" C says: "It's B" D says: "It's not me" And we know that only one of them tells the truth. Who is the one? I want to solve it by using Prolog. It's an interview question. 回答1: One-liner solution ?- member(K,[a,b,c,d]),(K\=a->A=1;A=0),(K=d->B=1;B=0),(K=b->C=1;C=0),(K\=d->D=1;D=0),A+B+C+D=:=1. K = a, A = 0, B = 0, C = 0, D = 1 ; false. 回答2: Disclaimer : This is Xonix ' solution. If you like it vote him up. But as it

Multithreading Puzzles [closed]

眉间皱痕 提交于 2019-11-27 10:28:25
I'm trying to come up with some programming puzzles focused on multi-threading. Most of the problems I've been able to come up with, so far, have been pretty domain specific. Does anybody have any decent programming puzzles for developers attempting to learn the core concepts of multi-threading applications? There are a number of topics covered at this link. Multithreaded Programming with ThreadMentor : A Tutorial Edit: Here are some direct links to the problems listed at that link, along with their initial descriptions. ThreadMentor : The Dining Philosopher's Problem ThreadMentor : The Dining

Two marbles and a 100 story building

拟墨画扇 提交于 2019-11-27 09:13:07
问题 One of those classic programming interview questions... You are given two marbles, and told that they will break when dropped from some certain height (and presumably suffer no damage if dropped from below that height). You’re then taken to a 100 story building (presumably higher than the certain height), and asked to find the highest floor your can drop a marble from without breaking it as efficiently as possible. Extra info You must find the correct floor (not a possible range) The marbles