aspose file tools*
The moose likes Programming Diversions and the fly likes Programming Puzzle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Programming Puzzle" Watch "Programming Puzzle" New topic
Author

Programming Puzzle

Sameer Jamal
Ranch Hand

Joined: Feb 16, 2001
Posts: 1870
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

Joined: Feb 28, 2005
Posts: 986
I read this more than 5 times.But didn't understand.Can you explain in simple words?


Namma Suvarna Karnataka
Sameer Jamal
Ranch Hand

Joined: Feb 16, 2001
Posts: 1870
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

Joined: Jan 30, 2000
Posts: 18671
"...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?


"I'm not back." - Bill Harding, Twister
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
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.


Reid - SCJP2 (April 2002)
Sameer Jamal
Ranch Hand

Joined: Feb 16, 2001
Posts: 1870
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

Joined: Mar 11, 2002
Posts: 1140
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


Mani
Quaerendo Invenietis
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Programming Puzzle