puzzle

Two marbles and a 100 story building

不问归期 提交于 2019-11-28 15:28:59
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 are both guaranteed to break at the same floor Assume it takes zero time for you to change floors -

The “guess the number” game for arbitrary rational numbers?

只谈情不闲聊 提交于 2019-11-28 15:01:13
I once got the following as an interview question: I'm thinking of a positive integer n. Come up with an algorithm that can guess it in O(lg n) queries. Each query is a number of your choosing, and I will answer either "lower," "higher," or "correct." This problem can be solved by a modified binary search, in which you listing powers of two until you find one that exceeds n, then run a standard binary search over that range. What I think is so cool about this is that you can search an infinite space for a particular number faster than just brute-force. The question I have, though, is a slight

How do I programmatically return the max of two integers without using any comparison operators and without using if, else, etc?

依然范特西╮ 提交于 2019-11-28 08:24:14
How do I programmatically return the maximum of two integers without using any comparison operators and without using if , else , etc? plinth max: // Will put MAX(a,b) into a a -= b; a &= (~a) >> 31; a += b; And: int a, b; min: // Will put MIN(a,b) into a a -= b; a &= a >> 31; a += b; from here . MSN http://www.graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax r = x - ((x - y) & -(x < y)); // max(x, y) You can have fun with arithmetically shifting (x - y) to saturate the sign bit, but this is usually enough. Or you can test the high bit, always fun. I think I've got it. int data[2]

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

蓝咒 提交于 2019-11-28 07:48:54
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 digits. If I wanted to verify that, how could I calculate the digits starting at the 58th place? Edited - with the ratio 2124679 / 2147483647, how to get the hundred digits in the 2147484600th through 2147484700th places. OK, 3rd try's a charm :) I can't believe I

Puzzle Solving: Finding All Words Within a Larger Word in PHP

最后都变了- 提交于 2019-11-28 05:38:40
问题 So I have a database of words between 3 and 20 characters long. I want to code something in PHP that finds all of the smaller words that are contained within a larger word. For example, in the word "inward" there are the words "rain", "win", "rid", etc. At first I thought about adding a field to the Words tables (Words3 through Words20, denoting the number of letters in the words), something like "LetterCount"... for example, "rally" would be represented as 10000000000200000100000010: 1

Custom image mask in iOS

╄→尐↘猪︶ㄣ 提交于 2019-11-28 05:09:11
I have a issue with masking images. I do game "puzzle" and have to make custom images. I found and tried 2 way of custom cropping: Using CALayer.mask property. Using UIImage.mask property. In first option i create my custom path, then assign it to CAShapeLayer.path property, then assign CAShapeLayer to CALayer.mask property. At the end i have custom cropped image. In second option i use firstly use CGImageMaskCreate() method (i use previously created black mask images of puzzle), then CGContextClipToMask() . In either options i have problem with performance (mostly when i crop image into 16

How to find repeating sequence of characters in a given array?

一曲冷凌霜 提交于 2019-11-28 04:54:34
My problem is to find the repeating sequence of characters in the given array. simply, to identify the pattern in which the characters are appearing. .---.---.---.---.---.---.---.---.---.---.---.---.---.---. 1: | J | A | M | E | S | O | N | J | A | M | E | S | O | N | '---'---'---'---'---'---'---'---'---'---'---'---'---'---' .---.---.---.---.---.---.---.---.---.---.---.---.---.---.---. 2: | R | O | N | R | O | N | R | O | N | R | O | N | R | O | N | '---'---'---'---'---'---'---'---'---'---'---'---'---'---'---' .---.---.---.---.---.---.---.---.---.---.---.---. 3: | S | H | A | M | I | L | S | H

Fixing a broken loop by changing exactly one character

送分小仙女□ 提交于 2019-11-28 04:32:19
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; } Well fixing the above code is straight-forward. To make the problem interesting, you have to fix the above code, by changing exactly one character. There are three known solutions. See if you can get all those three. I cannot figure out how to solve. I know that it can be fixed by

Linear Time Voting Algorithm. I don't get it

余生长醉 提交于 2019-11-28 04:06:38
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 over an element e: If the counter is 0, we set the current candidate to e and we set the counter to 1. If the counter is not 0, we increment or decrement the counter according to whether e is the current candidate. When we are done, the current candidate is the

How to find the exact set of operations for a specific number?

风流意气都作罢 提交于 2019-11-28 02:03:14
I am trying to implement a program that answers this problem : if you are given a specific number (for example : 268) and another 6 numbers (for example : 2,4,5,25,75,100) How can I find the operation that gives me the exact answer or the closest one to it? You can answer the previous example by using this operation : 75*4-25-5-2 = 268 Rules : you can use these arithmetic operations : +, -, *, / , (). when you use division the reminder must be equal to 0 (6/3 is ok but 6/4 is not ok!). you can not use the same number more than once. Also, you can avoid using a number (for example : 100 in our