• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

[Java puzzle] I don't feel like myself

 
Ranch Hand
Posts: 135
4
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is certainly not one for seasoned professionals, just a quick little surprise.



A slightly misleading hint: 4 characters are enough, 2 of them being the same. Though now that I told you it's slightly misleading, perhaps it's no longer misleading at all.
Have fun (and maybe post your answers ROT13'd or just write 'done' or 'got it', so you don't spoil the fun for others).
 
Bartender
Posts: 15737
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was already aware of which value for x would print "You won!", but I will admit it took me a few minutes to trim the answer down to 4 characters.

I also like your misleading hint.
 
Marshal
Posts: 80497
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also know what value causes x != x to return true.
By four characters, Stephan, are you excluding whitespace? I think the JLS uses five printing characters.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I replaced the text "write something here" in Istvan's application with exactly 4 characters. None of those 4 characters is whitespace.
 
Marshal
Posts: 4759
591
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it.  Use the force hint.
 
Sheriff
Posts: 28384
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the answer, and interestingly Eclipse tells me that the "try again" line is dead code. So (theoretically) I didn't even have to run it to confirm that I was correct.
 
Campbell Ritchie
Marshal
Posts: 80497
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:. . . Use the force hint.

As long as you didn't need the brute force
 
Sheriff
Posts: 9008
652
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Done.

Another misleading hint: there are multiple answers to this.
 
Paul Clapham
Sheriff
Posts: 28384
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Another misleading hint: there are multiple answers to this.



Some of which have 4 characters which are all different.
 
Liutauras Vilda
Sheriff
Posts: 9008
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Some of which have 4 characters which are all different.


I'm certainly intrigued now.

Stephan van Hulst wrote:..it took me a few minutes to trim the answer down to 4 characters


I'm having hard times to trim up to that.
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:
'm having hard times to trim up to that.



Hmm... I know a solution with 3 characters, but that requires an additional line (don't want to spoil the fun for others). Did you achieve your 3-character solution without any additional modifications and additions?
 
Liutauras Vilda
Sheriff
Posts: 9008
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the thing, hard to discuss without spoiling. When you and others say 'character', I don't know what is actually being meant.

All I can say is, that I hope where it says "write something here" I can write anything I want, and so I did, and the solution is something what contains 3 characters (two of them being the same) in spoken language.

And no, I did  not modify any of the actual code.

It does get puzzling, doesn't it
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:
the solution is something what contains 3 characters (two of them being the same) in spoken language.

And no, I did  not modify any of the actual code.

It does get puzzling, doesn't it



Maybe ROT13, so if someone wants, they can read it, and those who don't want to see the solution don't have to?

Ebg13 qbrf abg raqbefr flzobyf, fb V'z tbvat gb jevgr gurz bhg.

Sybng be Qbhoyr qbg AnA jbhyq jbex. Jvgu n fgngvp vzcbeg, gung'f bayl 3 punenpgref. Mreb cre mreb jvgu na s be q gb sbepr sybngvat cbvag vf sbhe punenpgref.

Vs lbh sbhaq n qvssrerag guerr punenpgre fbyhgvba, V'yy unir pbagvahr guvaxvat.
 
Liutauras Vilda
Sheriff
Posts: 9008
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Istvan, that is indeed my solution!

I never heard of rot13 before, so it was fun to decrypt your message
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rot13.com takes the fun out of it.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your solution was the first I thought of Liutauras, but since the puzzle is to only change the code in the specified location, that solution is at least 9 characters, not 3. That's why I said I had to think for a little bit to arrive at the same value, but only using 4 characters.
 
Liutauras Vilda
Sheriff
Posts: 9008
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fair enough. You can have 3 characters there, but then yes, you got to do something way before that.

Earlier, I wrote:And no, I did  not modify any of the actual code.


Ok, I did not realise that counts as code modification in such scenario.
 
Campbell Ritchie
Marshal
Posts: 80497
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . I never heard of rot13 before . . .

It seems to be a Caesar cipher.
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Liutauras Vilda wrote:. . . I never heard of rot13 before . . .

It seems to be a Caesar cipher.



Gosh, it seems I'm too old. :-) It's been used on forums for decades.
https://en.wikipedia.org/wiki/ROT13
 
Campbell Ritchie
Marshal
Posts: 80497
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are other ways to produce the same result than what you said in the Rot13 post. But I think not shorter than four non‑whitespace keystrokes.
 
Paul Clapham
Sheriff
Posts: 28384
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
V qba'g unir n 3-punenpgre fbyhgvba rvgure ohg zl 4-punenpgre fbyhgvba jvgu ab ercrngrq punenpgre vf gjb zbq mreb sbeprq gb qbhoyr.

(I don't know what Caesar has to do with this, it looks more like Mongolian to me.)
 
Saloon Keeper
Posts: 5605
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
De fkppbu qdt de fhewhqccydw tyluhiyed, Y'c qvhqyt
Yv oek Weewbu veh 'n != n zqlq zbi'
qdt jxud iuqhsx veh 'xjjfi://tesi.ehqsbu.sec/ud/zqlq/zqlqiu/17/tesi/qfy/sedijqdj-lqbkui.xjcb#zqlq.bqdw.Tekrbu.DqD'
oek iuu jxu iebkjyed hywxj yd vhedj ev oek
rotfl1610
 
Campbell Ritchie
Marshal
Posts: 80497
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must be superstitious about using 13, Piet.
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You must be superstitious about using 13, Piet.



One could say his answer was rotten. ;-) (No offense, just an attempt at a pun.)
 
Piet Souris
Saloon Keeper
Posts: 5605
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You must be superstitious about using 13, Piet.


I'm never supersticious, for that brings bad luck

And Istvan,
you made me learn something new, about eh..well and rot13! Had me make a translation method, in which I used 16, only to realize that using again 16 did not recover the original.    


edit: rot13, rot17, ...: that somehow reminds me of Advent of Code 2022, day 11!
 
Liutauras Vilda
Sheriff
Posts: 9008
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:rot13, rot17, ...: that somehow reminds me of Advent of Code 2022, day 11!



Istvan, I don't know if you need help deciphering that, but Piet said, be ready on 1st Dec 2023.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Had me make a translation method, in which I used 16, only to realize that using again 16 did not recover the original.    


It will, but you have to expand the character set that it operates on to 32 characters.
 
Campbell Ritchie
Marshal
Posts: 80497
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do we think it is time we showed the answers “in clear”?
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need two pieces of info to solve this.

- One is the behaviour of the == and != operators, described here:
https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.21.1


If either operand is NaN, then the result of == is false but the result of != is true.

Indeed, the test x!=x is true if and only if the value of x is NaN.

The methods Float.isNaN and Double.isNaN may also be used to test whether a value is NaN.



- The other is, of course, what NaN means:
https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.3


Not-a-Number values (hereafter abbreviated NaN). A NaN value is used to represent the result of certain invalid operations such as dividing zero by zero. NaN constants of both float and double type are predefined as Float.NaN and Double.NaN.



Therefore, x = Double.NaN (or Float.NaN) is the right solution.
By adding a static import for either of those, one can get a solution with 3 characters. That is what Liutauras Vilda did, and the reason for the comment:

I'm having hard times to trim up to that.



The solution with 4 characters is of course:

A NaN value is used to represent the result of certain invalid operations such as dividing zero by zero



However, an integer division would lead to ArithmeticException; we have to use a floating-point operation. Any of these would work, but not all of them are 4 characters long:
x = 0/0d or x = 0/0f or x = 0/0D or x = 0/0F
x = (double) 0 / 0 or x = 0 / (double)0 (or with float)
x = 0.0/0 or x = 0/0.0 or x = 0.0/0.0

A 4-character version without repeating the 0 - contrary to C, Java allows % (remainder, modulo) for floating-point:
x=1%0d
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:you have to expand the character set that it operates on to 32 characters.



My mother-tongue uses a 40-letter native alphabet, a 44-letter extended alphabet, but only 32 characters in the 40-letter native alphabet. Some of those 32 characters are not part of the 40-letter native alphabet, only the 44-letter extended alphabet, though. :-D

Confusing?

I'm Hungarian. :-D

We have letters like Á, É, Í, Ó, Ö, Ő, Ú, Ü and Ű, which are in addition to the 26 letters used by English.
But we also have letters written using 2 characters: "SZ" represents the English "S" sound; the Hungarian "S" represents the English "SH". Also: "CS", "DZ" and "ZS". We even have a 3-character letter, "DZS", which represents the "J" of "jungle".
Our native alphabet does not contain Q, X, Y and W; those, as letters, are only used in foreign words and some historical family names.
However, some of our 2-character letters are written using a "Y" (not a letter of the native alphabet, just a character to form those 2-character letters): "LY" (which is pronounced the same as "J", and both stand for the "Y" of "yet"); "GY", "NY" and "TY", which are softened sounds with no clear English equivalents.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic