• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Programming Puzzle

 
Sameer Jamal
Ranch Hand
Posts: 1870
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exhibit a simple context into which either of the phrases (1 + 2) or (2 +
1) can be substituted so that in essentially all programming languages the resulting substitutions yield different results.
 
Arjunkumar Shastry
Ranch Hand
Posts: 986
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read this more than 5 times.But didn't understand.Can you explain in simple words?
 
Sameer Jamal
Ranch Hand
Posts: 1870
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose there are two phrases (1+2) or (2+1) which can be substituted, we have to show a programming statement(algorithm) so that if we substitute one phrase with other it will give the different output in different programming languages.
[ February 21, 2006: Message edited by: Sameer Jamal ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"...in essentially all programming languages" - errr, that's pretty open-ended. My examples will be Java-specific, though similar equivalents exist in other languages.

----

Trivial solution:

System.out.println("(1+2)");

has different output than

System.out.println("(2+1)");

----

Slightly more interesting:

String[] parts = str.split("(1+2)");

Matches differently than

String[] parts = str.split("(2+1)");

----

Is that the sort of thing you mean?
 
Reid M. Pinchback
Ranch Hand
Posts: 775
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For any (programming or mathematical) language that allows you to define "+" you can make this trivially true, just define "+" to be some non-commutative operation. (1+2)==(1+2) is assuming commutativity, so all you need to do is pick contexts where commutativity doesn't hold.
 
Sameer Jamal
Ranch Hand
Posts: 1870
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
"...in essentially all programming languages" - errr, that's pretty open-ended. My examples will be Java-specific, though similar equivalents exist in other languages.

----

Trivial solution:

System.out.println("(1+2)");

has different output than

System.out.println("(2+1)");

----

Slightly more interesting:

String[] parts = str.split("(1+2)");

Matches differently than

String[] parts = str.split("(2+1)");

----

Is that the sort of thing you mean?


Nobody can beat Jim, wait for my next puzzle.
[ February 21, 2006: Message edited by: Sameer Jamal ]
 
Mani Ram
Ranch Hand
Posts: 1140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sameer Jamal:


Nobody can beat Jim, wait for my next puzzle.

[ February 21, 2006: Message edited by: Sameer Jamal ]


I thought exactly the same thing (the System.out.println solution, not the split one), but never posted the solution, because it looked too too stupid to me
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic