puzzle

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

若如初见. 提交于 2019-11-30 07:33:13
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. Range(int x, int lower, int upper){ //... some code here return retVal; } Obviously I understand the

Java Puzzle with reflection and String

喜欢而已 提交于 2019-11-30 06:52:08
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 happening actually? MD. Sahib Bin Mahboob This source code opens up some interesting techniques of java.

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

心已入冬 提交于 2019-11-30 06:28:59
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" 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 code range, then you can simply see if you have any of the characters 32-128, one by one. The following query

How can I ensure that when I shuffle my puzzle I still end up with an even permutation?

不想你离开。 提交于 2019-11-30 04:49:41
问题 I'm interested making an implementation of the 14-15 puzzle: I'm creating an array with the values 0 - 15 in increasing order: S = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } Now, what I want to do is shuffle them to create a new instance of the puzzle. However, I know that if I create a board with an "odd permutation" than it is unsolvable. Wikipedia says I need to create the puzzle with an even permutation. I believe this means that I simply have to do ensure I do an even

Generalised Two-Egg Puzzle

老子叫甜甜 提交于 2019-11-30 03:55:55
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 out that the first-floor windows break eggs, nor is it ruled out that the Nth-floor windows do not cause

Why is my simple comparator broken?

左心房为你撑大大i 提交于 2019-11-30 00:47:18
问题 I have a class, which I have simplified to this: final class Thing { private final int value; public Thing(int value) { this.value = value; } public int getValue() { return value; } @Override public String toString() { return Integer.toString(value); } } I want to sort an array of this thing. So I have created a simple copmarator: private static final Comparator<Thing> reverse = new Comparator<Thing>() { public int compare(Thing a, Thing b) { return a.getValue() - b.getValue(); } }; I then

Tape-Equilibrium Codility Training [closed]

空扰寡人 提交于 2019-11-29 23:04:53
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 given. Array A represents numbers on a tape. Any integer P, such that 0 < P < N, splits this tape into two non−empty parts: A[0], A[1], …, A[P − 1] and A[P], A[P + 1], …, A[N − 1]. The difference between the two parts is the value of: |(A[0] + A[1] + … + A[P − 1]) − (A[P] + A[P + 1] + … + A[N − 1])| In other words,

Set every cell in matrix to 0 if that row or column contains a 0

六月ゝ 毕业季﹏ 提交于 2019-11-29 18:31:35
Given a NxN matrix with 0s and 1s. Set every row that contains a 0 to all 0 s and set every column that contains a 0 to all 0 s. For example 1 0 1 1 0 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 results in 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 A Microsoft Engineer told me that there is a solution that involves no extra memory, just two boolean variables and one pass, so I'm looking for that answer. BTW, imagine it is a bit matrix, therefore just 1s and 0s are allow to be in the matrix. Ok, so I'm tired as it's 3AM here, but I have a first try inplace with exactly 2 passes on each

square puzzle solution

我与影子孤独终老i 提交于 2019-11-29 15:09:39
问题 Question: given an integer number n, print the numbers from 1 up to n 2 like this: n = 4 result is: 01 02 03 04 12 13 14 05 11 16 15 06 10 09 08 07 How do you solve it (apart from the solution provided in the link below)? http://www.programmersheaven.com/mb/CandCPP/81986/81986/problem-in-making-ap-c++-program/?S=B20000 I'm looking in another direction. So far, I'm trying to figure out if I could obtain the ordered list of positions I have to fill in. Here's what Im looking into: is there a

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

房东的猫 提交于 2019-11-29 12:21:38
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 instances of the letter A, 0 instances of the letter B, ... 2 instances of the letter L, etc. Then, go