Puzzle 8-12-08 (solutions, kindof)

BCS ao at pathlink.com
Tue Aug 12 14:39:24 PDT 2008


Reply to wyverex,

> 1)First is simple..
> 
> What's the  "condition" so that the following code snippet  prints
> both HelloWorld !
> 
> if  "condition"
> printf ("Hello");
> else
> printf("World");

cheats:

(printf("Hello") && false)

(scope _ = new class {~this(){printf("World"); } }) // this might work (not-tested)

I think there might be a way to trick the parser into doing something strange 
but I can't seem to make it work.

> 2)Next little data structure knowledge need
> 
> You are provided with two stacks, and pop() and push() functions for
> them. You have to implement queue i.e. enqueue() and dequeue() using
> the available operations.
> 

//Big Dumb solution
Enqueue(T t) { while(S2.NotEmpty) S1.Push = S2.Pop; S1.Push = t; }
Dequeue(T t) { while(S1.NotEmpty) S2.Push = S1.Pop; return S2.Pop; }

> 3) little string manipulation
> 
> How do you reverse the words in a string?
> 
> "My name is Amit Agarwal"
> to
> "Agarwal Amit is name My"
> **try without using the library!
> 

// assume word's are [^ ]
void worldRev(char[] str)
{
   str.reverse;
   int i = 0;
   for(int j = 1; j < str.length)
       if(str[j] == ' ')
       {
           str[i..j].reverse;
           i=j;
       }
}




More information about the Digitalmars-d-learn mailing list