puzzle

In C bits, multiply by 3 and divide by 16

走远了吗. 提交于 2019-11-29 10:28:40
问题 A buddy of mine had these puzzles and this is one that is eluding me. Here is the problem, you are given a number and you want to return that number times 3 and divided by 16 rounding towards 0. Should be easy. The catch? You can only use the ! ~ & ^ | + << >> operators and of them only a combination of 12. int mult(int x){ //some code here... return y; } My attempt at it has been: int hold = x + x + x; int hold1 = 8; hold1 = hold1 & hold; hold1 = hold1 >> 3; hold = hold >> 4; hold = hold +

Java Puzzle with reflection and String

好久不见. 提交于 2019-11-29 07:11:48
问题 This source outputs G'Day Mate. How this is happening ? public static void main(String args[]) { System.out.println("Hello World"); } static { try { Field value = String.class.getDeclaredField("value"); value.setAccessible(true); value.set("Hello World", value.get("G'Day Mate.")); } catch (Exception e) { throw new AssertionError(e); } } And if we change main functions "Hello World" to new String("Hello World") : System.out.println(new String("Hello World")); It outputs Hello world . What is

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

十年热恋 提交于 2019-11-29 06:13:00
问题 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" 回答1: Given that your column is varchar, it means it can only store characters from codes 0 to 255, on whatever code page you have. If you only use the 32-128 ASCII

cat file | … vs … <file

馋奶兔 提交于 2019-11-29 03:21:32
Is there a case of ... or context where cat file | ... behaves differently than ... <file ? 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 input and ... . Quick and easy way to make the block size issue apparent: $ cat large-file | pv >/dev/null 5

Tinyurl-style unique code: potential algorithm to prevent collisions

烈酒焚心 提交于 2019-11-29 02:19:12
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 starts being lots of collisions) It's not out of the question to fill up 10% of the combinations so there's

Tape-Equilibrium Codility Training [closed]

☆樱花仙子☆ 提交于 2019-11-28 20:54:08
问题 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 . I received a codility test the other day for a job, as such I've been practicing using some of the problems from their training page Link Unfortunately, I've only been able to get 83/100 on the Tape-Equilibrium question: A non-empty zero-indexed array A consisting of N integers is

Generalizing the algorithm for domino tiling?

老子叫甜甜 提交于 2019-11-28 20:22:43
In this earlier question the OP asked the following problem: Given a rectangular grid where some squares are empty and some are filled, what is the largest number of 2x1 dominoes that can be placed into the world such that no two dominos overlap and no domino is atop a filled square? The (quite beautiful!) answer to this problem recognized that this is equivalent to finding a maximum bipartite matching in a specially-constructed graph. In this graph, each empty square has a node that is linked to each of its neighbors by an edge. Each domino then corresponds to an edge in the graph such that

String Reduction - Programming Contest . Solution needed

*爱你&永不变心* 提交于 2019-11-28 19:54:18
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 -> ABCCC -> AACC -> ABC -> AA as here length is 2 < (length of CCCCCCCC ) How do you go about this problem

Solving a logic puzzle using Prolog

那年仲夏 提交于 2019-11-28 18:27:43
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. 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. Disclaimer : This is Xonix ' solution. If you like it vote him up. But as it took me some head-scratching to figure out what was going on, I thought I might just as well offer my comments so

C# Potential Interview Question…Too hard? [closed]

佐手、 提交于 2019-11-28 17:36:13
Without running this code, identify which Foo method will be called: class A { public void Foo( int n ) { Console.WriteLine( "A::Foo" ); } } class B : A { /* note that A::Foo and B::Foo are not related at all */ public void Foo( double n ) { Console.WriteLine( "B::Foo" ); } } static void Main( string[] args ) { B b = new B(); /* which Foo is chosen? */ b.Foo( 5 ); } Which method? And why? No cheating by running the code. I found this puzzle on the web; I like it and I think I'm going to use it as an interview question...Opinions? EDIT: I wouldn't judge a candidate on getting this wrong, I'd