Istvan Kovacs

Ranch Foreman
+ Follow
since May 06, 2010
Merit badge: grant badges
For More
Zürich, Switzerland
Cows and Likes
Cows
Total received
3
In last 30 days
2
Total given
0
Likes
Total received
11
Received in last 30 days
5
Total given
3
Given in last 30 days
1
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Istvan Kovacs

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.
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

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.)

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
rot13.com takes the fun out of it.

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 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?
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).
You don't need Strings to represent single characters. You can use char for that. char is a primitive type (as opposed to String, which is a reference ('object') type), so values can be compared using == (you will no longer be comparing equality of object references, in other words, identity).
See https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/String.html#toCharArray() and its friends:
  • https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/String.html#charAt(int)
  • https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/String.html#chars()
  • https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/String.html#getChars(int,int,char%5B%5D,int)


  • 1 week ago
    Hi,

    I've played around a bit during the weekend. The result is here: https://github.com/kofa73/LambdaTool. It's not intended as a serious tool or library, but as an experiment. Feel free to use it, play around, do whatever you like.
    Three implementations are provided:
    - using proxies, only supporting interfaces
    - CgLib
    - ByteBuddy

    (I didn't have a real problem to solve with it -- or actually, the problem I solved the same way (getting access to the path stored as an annotation on a method of an OpenApi/Swagger-generated REST controller interface) could be done using the simple proxy-based solution.)

    Check the readme, and open the unit tests for demonstration of usage.

    Kofa
    1 year ago
    To be honest, I didn't write this just to show some corner case. What inspired this was a piece of badly written code I had reviewed. My initial code was to confirm a suspicion, then I prettied it up a bit to turn it into a puzzle I intended to be funny, as well as maybe teach others something about Java. It was not my intention to have anyone killed by a lion.

    There was a unit test, which, along with the class under test, combined a rather bizarre set of annotations, and yet did not fail with an NPE. That was what got me to look closer. Here's what the original code (class + test class) looked like:



    Yes, you have to leave the provided code as it is. And of course the real question is how to avoid the NPE.
    I reviewed a junior colleague's code today, and found something that looked weird. That's what inspired the post. (No, we aren't working on a pet store application.  )
    Given the following code:


    Implement class Cat to produce output similar to the following:

    Message from org.kovacstelekes.techblog.catwithninelives.Cat@8efb846: I'm a Cat with 9 lives
    Killed the cat. Message from null: I'm a Cat with 8 lives
    Killed the cat. Message from null: I'm a Cat with 7 lives
    Killed the cat. Message from null: I'm a Cat with 6 lives
    Killed the cat. Message from null: I'm a Cat with 5 lives
    Killed the cat. Message from null: I'm a Cat with 4 lives
    Killed the cat. Message from null: I'm a Cat with 3 lives
    Killed the cat. Message from null: I'm a Cat with 2 lives
    Killed the cat. Message from null: I'm a Cat with 1 life
    Killed the cat. Message from null: I've run out of lives. Please resurrect me.
    Resurrected the cat. Message from org.kovacstelekes.techblog.catwithninelives.Cat@2a84aee7: I'm a Cat with 9 lives
    Ynl0ZS5jbGFzcywgQnl0ZS5jbGFzcw==