digitalmars.D.learn - Puzzle 8-12-08
- Wyverex (17/17) Aug 12 2008 1)First is simple..
- Mike Wey (33/65) Aug 12 2008 1. Don't know ;)
- BCS (21/45) Aug 12 2008 cheats:
- bearophile (4/5) Aug 12 2008 Oh, right, the reverse method works in place on slices too :-)
- bearophile (128/141) Aug 12 2008 import std.stdio: putr = writefln, format;
- bearophile (8/8) Aug 12 2008 With the queue puzzle I have even found another possible bug in DMD (18t...
- bearophile (11/11) Aug 13 2008 Well, with my libs too (xsplit is lazy):
- lurker (6/16) Aug 12 2008 Might work, don't know if the order is correct:
- Wyverex (7/26) Aug 12 2008 if the order is right then I like this answer!
- Wyverex (41/68) Aug 12 2008 !printf("Hello")
- Fazil (26/26) Apr 23 2009 /* Solution 1: */
- Fazil (26/26) Apr 23 2009 /* Solution 1: */
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!
Aug 12 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
Aug 12 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; } }
Aug 12 2008
BCS:str[i..j].reverse;Oh, right, the reverse method works in place on slices too :-) Bye, bearophile
Aug 12 2008
Wyverex Wrote:1)First is simple.. What's the "condition" so that the following code snippet prints both HelloWorld !I have no idea yet. Condition can be a function that prints things by itself, but that's cheating.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.import std.stdio: putr = writefln, format; class Stack(T) { T[] data; void push(T el) { this.data ~= el; } T pop() { T aux = this.data[$-1]; this.data.length = this.data.length - 1; return aux; } string toString() { return format("Stack(%s)", this.data); } int length() { return this.data.length; } void length(int n) { this.data.length = n; } void reverse() { if (this.data.length) this.data.reverse; } } class Queue(T) { // Modified from Cookbook Recipe 68436 Stack!(T) back, forward; this() { back = new typeof(back); forward = new typeof(forward); } T dequeue() { if (this.forward.length) return this.forward.pop(); else { this.back.reverse(); auto aux = this.forward; this.forward = this.back; this.back = aux; return this.forward.pop(); } } void enqueue(T el) { this.back.push(el); } } import std.stdio: put = writef, putr = writefln; // a bit of testing void main() { int n = 8; int m = 2; auto q = new Queue!(int); for (int i; i < n; i++) { q.enqueue(i); putr("enqueue: ", i); } putr(); for (int i; i < n; i++) putr("dequeue: ", q.dequeue()); for (int i; i < m; i++) { for (int j; j < 2; j++) { q.enqueue(j); putr("enqueue: ", j); } putr(); for (int j; j < 2; j++) putr("dequeue: ", q.dequeue()); } putr(); for (int i; i < m; i++) { for (int j; j < 12; j++) { q.enqueue(j); putr("enqueue: ", j); } putr(); for (int j; j < 12; j++) putr("dequeue: ", q.dequeue()); } }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!Python: from array import array def reverse(s, start, stop): n = stop - start + 1 for i in xrange(n // 2): s[start + i], s[stop - i] = s[stop - i], s[start + i] s = array("c", " My name is Amit Agarwal 2") reverse(s, 0, len(s)-1) start = -1 for pos, c in enumerate(s): if c in " \t\n\r": if start != -1: reverse(s, start, pos-1) start = -1 else: if start == -1: start = pos print s (using slices and string methods it's simpler) D: import std.stdio: putr = writefln; void reverse(T)(T[] s, int start, int stop) { int n = stop - start + 1; for (int i; i < n/2; i++) { // swap T aux = s[start + i]; s[start + i] = s[stop - i]; s[stop - i] = aux; } } void main() { string s = " My name is Amit Agarwal 0".dup; s.reverse; int start = -1; foreach (pos, c; s) if (c==' ' || c=='\t' || c=='\n' || c=='\r') { if (start != -1) { reverse(s, start, pos-1); start = -1; } } else if (start == -1) start = pos; putr('"', s, '"'); } (One bug found translating from Python to D: those brackets are necessary!) Most languages have a page with puzzles meant to be solved with that language, so I suggest you to create a Wiki page with such problems, plus the solutions shown by people too under a spoiler link. Bye, bearophile
Aug 12 2008
With the queue puzzle I have even found another possible bug in DMD (18th bug I have found in DMD so far), minimal code: :-) void main() { for (int i; i < 1; i++) static if (false) printf("%d\n", i); } Bye, bearophile
Aug 12 2008
Well, with my libs too (xsplit is lazy): import d.string, d.func; void main() { string s = " My name is Amit Agarwal 0".dup; s.reverse; foreach (part; s.xsplit()) part.reverse; putr(repr(s)); } Bye, bearophile
Aug 13 2008
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");Might work, don't know if the order is correct: if (!fork()) printf ("Hello"); else printf("World");
Aug 12 2008
lurker wrote:Wyverex Wrote:if the order is right then I like this answer! On the web the most accepted solution is the cheater solution if (!printf("Hello")) printf("Hello"); else print("World");1)First is simple.. What's the "condition" so that the following code snippet prints both HelloWorld ! if "condition" printf ("Hello"); else printf("World");Might work, don't know if the order is correct: if (!fork()) printf ("Hello"); else printf("World");
Aug 12 2008
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");!printf("Hello") opens a new possibility, run if on a list... if( [true, false] ) printf ("Hello"); else printf("World"); other possibility, hijack if... maybe messy Nop out the else jump in memory!!!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.didn't code up but basically Stack work, temp; enqueue( item ) try while(1) temp.push(work.pop); catch(Object o) {} //work will throw underflow, maybe overflow? work.push(item); try while(1) work.push(temp.pop); catch(Object o) {} //temp will throw underflow dequeue try return work.pop; catch(Object o) //underflow return null;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!//Yeah taking ML Programming was worth it!!! import std.stdio; char[] rev(char[] str) { if(str.length == 0) return ""; int index = -1; foreach(i, c; str) if(c == ' ') { index = i; break; } if(index == -1) return str; if(index == 0) return rev(str[1..$]); return rev(str[index+1..$]) ~ " " ~ str[0..index]; } void main() { char[] str = " My Name is Wyverex Cypher "; writefln("%s\n%s", str, rev(str)); }
Aug 12 2008
/* Solution 1: */ int main(int argc, char* argv[]) { if( argc == 2 || main( 2, NULL ) ) { printf("Hello "); } else { printf("World\n"); } return 0; } /* Solution 2 (Only for Unix and Linux): */ int main(int argc, char* argv[]) { if( !fork() ) { printf("Hello "); } else { printf("World\n"); } return 0; }
Apr 23 2009
/* Solution 1: */ int main(int argc, char* argv[]) { if( argc == 2 || main( 2, NULL ) ) { printf("Hello "); } else { printf("World\n"); } return 0; } /* Solution 2 (Only for Unix and Linux): */ int main(int argc, char* argv[]) { if( !fork() ) { printf("Hello "); } else { printf("World\n"); } return 0; }
Apr 23 2009