August 07, 2008 Re: Programing Puzzles (another bonus) (Spoiler) | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
>
> char a=0,b=0,c=0,d=0;
> while(a<5 || b<9 || c<5 || d<9)
> {
> writef("%d%d:%d%d\n", a,b,c,d);
>
> // add a single /expression/ here
> }
>
> make that count from "00:00" to "59:59" (minutes and seconds)
>
> I seem to remember I figured this out once, but I don't remember how I did it.
>
>
import std.stdio;
void main()
{
char a=0,b=0,c=0,d=0;
do{
writef("%d%d:%d%d\n", a,b,c,d);
( d == 9 ? d = 0 || (c == 5 ? c = 0 || ( b == 9 ? b = 0 || ++a : ++b ) : ++c) : ++d);
}while(a<6 || b<0 || c<0 || d<0)
}
|
August 07, 2008 Re: Programing Puzzles (another bonus) (Spoiler) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | Reply to wyverex,
> BCS wrote:
>
>> char a=0,b=0,c=0,d=0;
>> while(a<5 || b<9 || c<5 || d<9)
>> {
>> writef("%d%d:%d%d\n", a,b,c,d);
>> // add a single /expression/ here
>> }
>> make that count from "00:00" to "59:59" (minutes and seconds)
>>
>> I seem to remember I figured this out once, but I don't remember how
>> I did it.
>>
> import std.stdio;
>
> void main()
> {
> char a=0,b=0,c=0,d=0;
>
> do{
> writef("%d%d:%d%d\n", a,b,c,d);
> ( d == 9 ? d = 0 || (c == 5 ? c = 0 || ( b == 9 ? b = 0 || ++a :
> ++b ) : ++c) : ++d);
>
> }while(a<6 || b<0 || c<0 || d<0)
>
> }
>
yah, that looks right. I think I used a comma expression or some sutch.
|
August 07, 2008 Re: Programing Puzzles | ||||
---|---|---|---|---|
| ||||
Posted in reply to Koroskin Denis | Reply to Koroskin,
> On Thu, 07 Aug 2008 01:50:56 +0400, Wyverex <wyverex.cypher@gmail.com>
> wrote:
>
>> just some fun little programming puzzles I found around online...
>>
>> Write a "Hello World" program in 'C' without using a semicolon.
>> (Note: #include in C doesn't need a semicolon but import does)
>>
>> Problem #1 Write a "Hello World" program in D with only a semicolon
>> on import statement.
>>
>> Problem #2 Test if an int is even or odd without looping or if
>> statement (Cant use: do, while, for, foreach, if).
>>
>> Problem #3 Write a program without using any loop (if, for, while
>> etc) to print numbers from 1 to 100 and 100 to 1;
>>
>> Problem #4 Find if the given number is a power of 2.
>>
> // 0 is considered a power of two here
> bool isPowerOfTwo(int i) {
> return (i & (i-1) == 0);
> }
isPowerOfTwo(int.min);
0b10000000 == byte.min
0b01111111 == byte.min -1
0b00000000
|
August 07, 2008 Re: Programing Puzzles | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> Reply to Koroskin,
>
>> On Thu, 07 Aug 2008 01:50:56 +0400, Wyverex <wyverex.cypher@gmail.com>
>> wrote:
>>
>>> just some fun little programming puzzles I found around online...
>>>
>>> Write a "Hello World" program in 'C' without using a semicolon.
>>> (Note: #include in C doesn't need a semicolon but import does)
>>>
>>> Problem #1 Write a "Hello World" program in D with only a semicolon
>>> on import statement.
>>>
>>> Problem #2 Test if an int is even or odd without looping or if
>>> statement (Cant use: do, while, for, foreach, if).
>>>
>>> Problem #3 Write a program without using any loop (if, for, while
>>> etc) to print numbers from 1 to 100 and 100 to 1;
>>>
>>> Problem #4 Find if the given number is a power of 2.
>>>
>> // 0 is considered a power of two here
>> bool isPowerOfTwo(int i) {
>> return (i & (i-1) == 0);
>> }
>
> isPowerOfTwo(int.min);
>
>
> 0b10000000 == byte.min
> 0b01111111 == byte.min -1
>
> 0b00000000
>
>
What are you trying to say? I imagine your validating Koroskin solution.
10000000 == byte.min
01111111 == byte.min - 1
=
00000000 == is power of 2
-Joel
|
August 07, 2008 Re: Programing Puzzles | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | Wyverex wrote:
> just some fun little programming puzzles I found around online...
>
>
> Problem #2 Test if an int is even or odd without looping or if statement (Cant use: do, while, for, foreach, if).
>
> Problem #4 Find if the given number is a power of 2.
>
> Post Solutions to this root, comments to someones solution in that thread.
Some of these are pretty standard interview questions. Although I don't personally like to ask these sort of questions because they are often about knowing a "trick" which you an easily lookup. The can be fun to figure out though.
Here's another common one:
| Write a bitcount for a 32-bit number.
And a little more challenging:
| Write a bitcount for a 32-bit number that is less then 15 operations without using a lookup table.
| Can you do that in 12 or less?
-Joel
|
August 07, 2008 Re: Programing Puzzles | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | Wyverex wrote: > Sweet! Didn't know printf was accessible without an import! 10pts! Actually this is due to a bad decision early on, where printf was put into object.d. It is not part of Tango's object. -- Lars Ivar Igesund blog at http://larsivi.net DSource, #d.tango & #D: larsivi Dancing the Tango |
August 07, 2008 Re: Programing Puzzles | ||||
---|---|---|---|---|
| ||||
On Thu, 07 Aug 2008 10:50:25 +0400, JAnderson <ask@me.com> wrote:
> Wyverex wrote:
>> just some fun little programming puzzles I found around online...
>> Write a "Hello World" program in 'C' without using a semicolon.
>> (Note: #include in C doesn't need a semicolon but import does)
>> Problem #1 Write a "Hello World" program in D with only a semicolon on import statement.
>> Problem #2 Test if an int is even or odd without looping or if statement (Cant use: do, while, for, foreach, if).
>> Problem #3 Write a program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1;
>> Problem #4 Find if the given number is a power of 2.
>> Post Solutions to this root, comments to someones solution in that thread.
>
> These are pretty standard interview questions. Although I don't personally like to ask these sort of questions because they are often about knowing a "trick" which you an easily lookup. The can be fun to figure out though.
>
> Here's another common one:
>
> | Write a bitcount for a 32-bit number.
>
> And a little more challenging:
>
> | Write a bitcount for a 32-bit number that is less then 15 operations without using a lookup table.
>
> | Can you do that in 12 or less?
>
> -Joel
I know the solution for 15 operations, but it is impossible to come up with solution quickly. Besides, it requires 64 bit arithmetic support.
|
August 07, 2008 Re: Programing Puzzles | ||||
---|---|---|---|---|
| ||||
Posted in reply to Koroskin Denis | Koroskin Denis wrote:
> On Thu, 07 Aug 2008 10:50:25 +0400, JAnderson <ask@me.com> wrote:
>
>> Wyverex wrote:
>>> just some fun little programming puzzles I found around online...
>>> Write a "Hello World" program in 'C' without using a semicolon.
>>> (Note: #include in C doesn't need a semicolon but import does)
>>> Problem #1 Write a "Hello World" program in D with only a semicolon on import statement.
>>> Problem #2 Test if an int is even or odd without looping or if statement (Cant use: do, while, for, foreach, if).
>>> Problem #3 Write a program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1;
>>> Problem #4 Find if the given number is a power of 2.
>>> Post Solutions to this root, comments to someones solution in that thread.
>>
>> These are pretty standard interview questions. Although I don't personally like to ask these sort of questions because they are often about knowing a "trick" which you an easily lookup. The can be fun to figure out though.
>>
>> Here's another common one:
>>
>> | Write a bitcount for a 32-bit number.
>>
>> And a little more challenging:
>>
>> | Write a bitcount for a 32-bit number that is less then 15 operations without using a lookup table.
>>
>> | Can you do that in 12 or less?
>>
>> -Joel
>
> I know the solution for 15 operations, but it is impossible to come up with solution quickly. Besides, it requires 64 bit arithmetic support.
The 12 op solution doesn't require 64-bit. Your right though to come up with something quickly for a question like this is extremely difficult when its been worked on for years, unless you "know" it.
-Joel
|
August 07, 2008 Re: Programing Puzzles | ||||
---|---|---|---|---|
| ||||
Posted in reply to JAnderson | JAnderson wrote:
> Wyverex wrote:
>> just some fun little programming puzzles I found around online...
>>
>>
>> Problem #2 Test if an int is even or odd without looping or if statement (Cant use: do, while, for, foreach, if).
>>
>> Problem #4 Find if the given number is a power of 2.
>>
>> Post Solutions to this root, comments to someones solution in that thread.
>
>
> Some of these are pretty standard interview questions. Although I don't personally like to ask these sort of questions because they are often about knowing a "trick" which you an easily lookup. The can be fun to figure out though.
>
> Here's another common one:
>
> | Write a bitcount for a 32-bit number.
>
> And a little more challenging:
>
> | Write a bitcount for a 32-bit number that is less then 15 operations without using a lookup table.
>
> | Can you do that in 12 or less?
>
> -Joel
int count( in int i )
{
int c = (i & 1) + (i & 2 ? 1 : 0) + (i & 4 ? 1 : 0) + (i & 8 ? 1 : 0);
i >>= 4;
c += (i & 1) + (i & 2 ? 1 : 0) + (i & 4 ? 1 : 0) + (i & 8 ? 1 : 0);
i >>= 4;
c += (i & 1) + (i & 2 ? 1 : 0) + (i & 4 ? 1 : 0) + (i & 8 ? 1 : 0);
i >>= 4;
c += (i & 1) + (i & 2 ? 1 : 0) + (i & 4 ? 1 : 0) + (i & 8 ? 1 : 0);
return c;
}
int count2( in int i )
{
int c = (i & 1) + ((i >> 1) & 1) + ((i >> 2) & 1) + ((i >> 3) & 1);
i >>= 4;
c += (i & 1) + ((i >> 1) & 1) + ((i >> 2) & 1) + ((i >> 3) & 1);
i >>= 4;
c += (i & 1) + ((i >> 1) & 1) + ((i >> 2) & 1) + ((i >> 3) & 1);
i >>= 4;
c += (i & 1) + ((i >> 1) & 1) + ((i >> 2) & 1) + ((i >> 3) & 1);
return c;
}
|
August 07, 2008 Re: Programing Puzzles | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | On Thu, 07 Aug 2008 19:37:27 +0400, Wyverex <wyverex.cypher@gmail.com> wrote:
> JAnderson wrote:
>> Wyverex wrote:
>>> just some fun little programming puzzles I found around online...
>>>
>>>
>>> Problem #2 Test if an int is even or odd without looping or if statement (Cant use: do, while, for, foreach, if).
>>>
>>> Problem #4 Find if the given number is a power of 2.
>>>
>>> Post Solutions to this root, comments to someones solution in that thread.
>> Some of these are pretty standard interview questions. Although I don't personally like to ask these sort of questions because they are often about knowing a "trick" which you an easily lookup. The can be fun to figure out though.
>> Here's another common one:
>> | Write a bitcount for a 32-bit number.
>> And a little more challenging:
>> | Write a bitcount for a 32-bit number that is less then 15 operations without using a lookup table.
>> | Can you do that in 12 or less?
>> -Joel
>
> int count( in int i )
> {
> int c = (i & 1) + (i & 2 ? 1 : 0) + (i & 4 ? 1 : 0) + (i & 8 ? 1 : 0);
> i >>= 4;
> c += (i & 1) + (i & 2 ? 1 : 0) + (i & 4 ? 1 : 0) + (i & 8 ? 1 : 0);
> i >>= 4;
> c += (i & 1) + (i & 2 ? 1 : 0) + (i & 4 ? 1 : 0) + (i & 8 ? 1 : 0);
> i >>= 4;
> c += (i & 1) + (i & 2 ? 1 : 0) + (i & 4 ? 1 : 0) + (i & 8 ? 1 : 0);
>
> return c;
> }
>
> int count2( in int i )
> {
> int c = (i & 1) + ((i >> 1) & 1) + ((i >> 2) & 1) + ((i >> 3) & 1);
> i >>= 4;
> c += (i & 1) + ((i >> 1) & 1) + ((i >> 2) & 1) + ((i >> 3) & 1);
> i >>= 4;
> c += (i & 1) + ((i >> 1) & 1) + ((i >> 2) & 1) + ((i >> 3) & 1);
> i >>= 4;
> c += (i & 1) + ((i >> 1) & 1) + ((i >> 2) & 1) + ((i >> 3) & 1);
>
> return c;
> }
Much simpler:
int getBitCount32(int i) {
return getBitCount16(i) + getBitCount16(i >> 16);
}
int getBitCount16(int i) {
return getBitCount8(i) + getBitCount8(i >> 8);
}
int getBitCount8(int i) {
return getBitCount4(i) + getBitCount4(i >> 4);
}
int getBitCount4(int i) {
return getBitCount2(i) + getBitCount2(i >> 2);
}
int getBitCount2(int i) {
return (i & 2) + (i & 1);
}
|
Copyright © 1999-2021 by the D Language Foundation