puzzle

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

时间秒杀一切 提交于 2019-11-27 08:58:09
问题 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

How to mix two ARGB pixels?

牧云@^-^@ 提交于 2019-11-27 07:41:21
How can I mix two ARGB pixels ? Example Here A is (Red with Alpha) and B is ( Blue with Alpha ). Mark Ransom Taken from the same Wikipedia article where you got the image: Translating to values which range from 0 to 255: rOut = (rA * aA / 255) + (rB * aB * (255 - aA) / (255*255)) gOut = (gA * aA / 255) + (gB * aB * (255 - aA) / (255*255)) bOut = (bA * aA / 255) + (bB * aB * (255 - aA) / (255*255)) aOut = aA + (aB * (255 - aA) / 255) It seems like this is what you want: http://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending , but I'm a little confused by your notation since wikipedia

Eric Lippert's challenge “comma-quibbling”, best answer?

不打扰是莪最后的温柔 提交于 2019-11-27 07:27:20
I wanted to bring this challenge to the attention of the stackoverflow community. The original problem and answers are here . BTW, if you did not follow it before, you should try to read Eric's blog, it is pure wisdom. Summary: Write a function that takes a non-null IEnumerable and returns a string with the following characteristics: If the sequence is empty the resulting string is "{}". If the sequence is a single item "ABC" then the resulting string is "{ABC}". If the sequence is the two item sequence "ABC", "DEF" then the resulting string is "{ABC and DEF}". If the sequence has more than

Fixing a broken loop by changing exactly one character

允我心安 提交于 2019-11-27 05:22:15
问题 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

Programming Riddle: How might you translate an Excel column name to a number?

谁都会走 提交于 2019-11-27 03:11:20
I was recently asked in a job interview to resolve a programming puzzle that I thought it would be interesting to share. It's about translating Excel column letters to actual numbers, if you recall, Excel names its columns with letters from A to Z, and then the sequence goes AA, AB, AC... AZ, BA, BB, etc. You have to write a function that accepts a string as a parameter (like "AABCCE") and returns the actual column number. The solution can be in any language. clorz I wrote this ages ago for some Python script: def index_to_int(index): s = 0 pow = 1 for letter in index[::-1]: d = int(letter,36)

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

戏子无情 提交于 2019-11-27 02:11:24
问题 How do I programmatically return the maximum of two integers without using any comparison operators and without using if , else , etc? 回答1: 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. 回答2: 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,

Check if a number is divisible by 3 [closed]

人盡茶涼 提交于 2019-11-27 01:04:04
Write code to determine if a number is divisible by 3. The input to the function is a single bit, 0 or 1, and the output should be 1 if the number received so far is the binary representation of a number divisible by 3, otherwise zero. Examples: input "0": (0) output 1 inputs "1,0,0": (4) output 0 inputs "1,1,0,0": (6) output 1 This is based on an interview question. I ask for a drawing of logic gates but since this is stackoverflow I'll accept any coding language. Bonus points for a hardware implementation (verilog etc). Part a (easy): First input is the MSB. Part b (a little harder): First

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

…衆ロ難τιáo~ 提交于 2019-11-27 00:34:33
问题 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 | '---'---'---'---'---'---'---'---'---'---'--

Fastest algorithm for circle shift N sized array for M position

谁都会走 提交于 2019-11-27 00:27:52
What is the fastest algorithm for circle shifting array for M positions? For example, [3 4 5 2 3 1 4] shift M = 2 positions should be [1 4 3 4 5 2 3] . Thanks a lot. If you want O(n) time and no extra memory usage (since array was specified), use the algorithm from Jon Bentley's book, "Programming Pearls 2nd Edition". It swaps all the elements twice. Not as fast as using linked lists but uses less memory and is conceptually simple. shiftArray( theArray, M ): size = len( theArray ) assert( size > M ) reverseArray( theArray, 0, size - 1 ) reverseArray( theArray, 0, M - 1 ) reverseArray( theArray

Linear Time Voting Algorithm. I don't get it

情到浓时终转凉″ 提交于 2019-11-27 00:17:00
问题 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