Puzzle 8-12-08 (Spoiler)

Mike Wey mike-wey at example.org
Tue Aug 12 14:38:51 PDT 2008


On Tue, 2008-08-12 at 15:46 -0400, Wyverex wrote:
> 1)First is simple..
> 
> What's the  "condition" so that the following code
> snippet  prints both HelloWorld !
> 
> if  "condition"
> printf ("Hello");
> else
> printf("World");
> 
> 
> 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.
> 
> 
> 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!
> 
> 
> 
> 
> 

1. Don't know ;)

2. This depends on the stack implementation:

void enqueue(Object obj)
{
  while(Object tmp = stack2.pop())
    stack1.push(tmp);

  stack1.push(obj);
}

Object dequeue()
{
  while(Object tmp = stack1.pop())
    stack2.push(tmp);

  return stack2.pop();
}

3. 
char[] reverse(char[] text)
{
  char[][] words = [""];

  foreach(c; text)
  {
    if(c == ' ')
      words ~= "";
    else
      words[$-1] ~= c;
  }

  char[] retText;

  foreach_reverse(word; words)
    retText ~= word ~ " ";

  return retText[0 .. $-1];
}

-- 
Mike Wey



More information about the Digitalmars-d-learn mailing list