Thread overview | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 06, 2008 Programing Puzzles | ||||
---|---|---|---|---|
| ||||
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. |
August 06, 2008 Re: Programing Puzzles | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | 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. Solutions, Don't Peek if you havn't tried them yet! Problem #1******************************************** import tango.io.Stdout; void main() { if(Stdout("Hello World")) {} } Problem #2********************************************* import tango.io.Stdout; void main() { int x = 2; char[][2] ans = ["Even", "Odd"]; Stdout( ans[ x & 1 ] ).newline; } Problem #3******************************************* import tango.io.Stdout; void countUP( int i )() { Stdout(i)(" "); countUP!(i+1); } void countUP( int i : 101 )() {} void countDN( int i )() { Stdout(i)(" "); countDN!(i-1); } void countDN( int i : 0 )() {} void main() { countUP!(1); Stdout.newline; countDN!(100); } Problem#4****************************************** import tango.io.Stdout; bool test( in uint i ) { int c = 0; while( i > 0 ) { c += i & 1; i = i >> 1; } return ( c == 1? true : false ); } void main() { for(uint x = 0; x < uint.max ; ++x) if(test(x)) Stdout(x).newline; } |
August 06, 2008 Re: Programing Puzzles (spoilers) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | Reply to wyverex, > 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). bool isEven(int i) {return !(i & 0x01);} > > Problem #3 Write a program without using any loop (if, for, while etc) > to print numbers from 1 to 100 and 100 to 1; void DoIt(int i = 1) { writef("%d\n", i); i==100 || DoIt(i+1); writef("%d\n", i); } > > Problem #4 Find if the given number is a power of 2. > bool isPow(int i) { if(!i) return true; while(!(i & 0x01)) i>>=1; return !(i ^ 0x01); } |
August 06, 2008 Re: Programing Puzzles | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | Wyverex wrote:
> Problem #1******************************************** import tango.io.Stdout;
>
> void main()
> {
> if(Stdout("Hello World")) {}
> }
this works, no semicolon needed with phobos:
void main()
{
if( printf("Hello World"), true )
{
}
}
|
August 06, 2008 Re: Programing Puzzles | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lutger | Sweet! Didn't know printf was accessible without an import!
10pts!
Lutger wrote:
> Wyverex wrote:
>
>> Problem #1********************************************
>> import tango.io.Stdout;
>>
>> void main()
>> {
>> if(Stdout("Hello World")) {}
>> }
>
> this works, no semicolon needed with phobos:
>
> void main()
> {
> if( printf("Hello World"), true )
> {
> }
> }
|
August 06, 2008 Bonus Puzzle | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | Through another one in.... (Multiply x by 7 without using multiplication (*) operator.) Bonus Problem! Multiply X by Y without using multiplication (*) operator.. Faster the better!!! |
August 06, 2008 Re: Bonus Puzzle (spoiler) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | Wyverex wrote:
> Through another one in....
>
>
>
> (Multiply x by 7 without using multiplication (*) operator.)
>
> Bonus Problem!
> Multiply X by Y without using multiplication (*) operator.. Faster the better!!!
import tango.io.Stdout;
uint mult ( in uint a, in uint b )
{
uint shift, ans;
while( b )
{
ans += ( b & 1 ? a << shift : 0 );
shift ++;
b >>= 1;
}
return ans;
}
void main()
{
uint x = 92;
uint y = 478;
Stdout( x * y ).newline; //here to prove value is right!!
Stdout( mult(x, y) ).newline;
}
|
August 07, 2008 Re: Bonus Puzzle | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | Reply to wyverex,
> Through another one in....
>
> (Multiply x by 7 without using multiplication (*) operator.)
>
> Bonus Problem!
> Multiply X by Y without using multiplication (*) operator.. Faster
> the
> better!!!
int Mul(int i, int j)
{
int ret = 0;
while(j)
{
if(j & 0x01) ret += i;
j >>= 1;
i <<= 1;
}
return ret;
}
now do div :)
|
August 07, 2008 Re: Programing Puzzles (another bonus) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | 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. |
August 07, 2008 Re: Programing Puzzles | ||||
---|---|---|---|---|
| ||||
Posted in reply to Wyverex | 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);
}
|
Copyright © 1999-2021 by the D Language Foundation