puzzle

Strange code in java.util.concurrent.LinkedBlockingQueue

心已入冬 提交于 2019-12-18 13:16:35
问题 All! I found strange code in LinkedBlockingQueue: private E dequeue() { // assert takeLock.isHeldByCurrentThread(); Node<E> h = head; Node<E> first = h.next; h.next = h; // help GC head = first; E x = first.item; first.item = null; return x; } Who can explain why do we need local variable h? How can it help for GC? 回答1: To better understand what happens let's see how the list looks like after executing the code. First consider an initial list: 1 -> 2 -> 3 Then h points to head and first to h

In c binary, testing to see if a number is in range

杀马特。学长 韩版系。学妹 提交于 2019-12-18 12:37:47
问题 This is part of a puzzle that I can't figure out. The function takes in three inputs. The first is an int, the second is the lower bound and the third is the upper bound. I need to test to see if that first number is within the lower and upper bound inclusive. If it is in range then return 1, else return 0. The catch is that I can only use ! ~ & ^ | + << >> operations and only a combination of 20 of them.. Also, only int variables may be used, and no if statements, loops or function calls.

Generalised Two-Egg Puzzle

只愿长相守 提交于 2019-12-18 11:32:40
问题 Here is the Problem Description : Suppose that we wish to know which stories in a N-story building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions: An egg that survives a fall can be used again. A broken egg must be discarded. The effect of a fall is the same for all eggs. If an egg breaks when dropped, then it would break if dropped from a higher window. If an egg survives a fall then it would survive a shorter fall. It is not ruled

Tinyurl-style unique code: potential algorithm to prevent collisions

狂风中的少年 提交于 2019-12-18 03:38:23
问题 I have a system that requires a unique 6-digit code to represent an object, and I'm trying to think of a good algorithm for generating them. Here are the pre-reqs: I'm using a base-20 system (no caps, numbers, vowels, or l to prevent confusion and naughty words) The base-20 allows 64 million combinations I'll be inserting potentially 5-10 thousand entries at once, so in theory I'd use bulk inserts, which means using a unique key probably won't be efficient or pretty (especially if there

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

风流意气都作罢 提交于 2019-12-17 18:33:42
问题 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

Custom image mask in iOS

雨燕双飞 提交于 2019-12-17 17:46:19
问题 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

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

一个人想着一个人 提交于 2019-12-17 16:28:23
问题 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

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

夙愿已清 提交于 2019-12-17 15:26:59
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . 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? 回答1: I've been trying to find a "portable" way of stealing a semicolon from an

Multithreading Puzzles [closed]

烈酒焚心 提交于 2019-12-17 10:22:08
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . 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,

Fastest algorithm for circle shift N sized array for M position

╄→尐↘猪︶ㄣ 提交于 2019-12-17 06:30:12
问题 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. 回答1: 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