Istvan Kovacs

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

Recent posts by Istvan Kovacs

Campbell Ritchie wrote:Here is the simplest version of f() applied to your original code, along with actual type parameters given to the declaration of function:


It should be possible to apply a method reference in line 34 instead: Function::identity.



Yes. That's why I wrote in a previous comment:


I just wanted to ask if your solution would still work if I added a check asserting that !Objects.equals(item1, function.apply(item1)) and the same for item2, but then Mike provided the exact solution I had in mind.



I also wanted to draw attention to the behaviour of +0 and -0 leading to +infinity and -infinity for x -> 1/x; however, even adding the non-identity restriction mentioned above would not prevent people from using x -> -x, so I failed in that regard.
3 months ago
So, the solution has been revealed, but not yet explained:
- in the floating-point standard IEEE 754 (this is not just about Java), 0 is signed. https://en.wikipedia.org/wiki/IEEE_754#Signed_zero
- these values compare as ==. See https://en.wikipedia.org/wiki/IEEE_754#Comparison_predicates.
- however, they are are represented by (boxed into) different Double values, which are not equal according to equals();
- Objects.equals() was used to hide the boxing operation (you cannot call primitiveDouble.equals()).
- 1/+0 and 1/-0 result in +infinity and -infinity (but one could have used doubleValue -> -doubleValue as well as the mapping function). This is also described at https://en.wikipedia.org/wiki/IEEE_754#Signed_zero.
3 months ago

Campbell Ritchie wrote:You can create a similar puzzle with NaNs, but all the Boolean tests would have to be reversed.



I did something like that in January. https://coderanch.com/t/768833/Java-puzzle-don-feel
3 months ago

Campbell Ritchie wrote:I can think of a few things where the usual == and != operators seem to be at odds with equals(), I am keeping quiet about them, too.

It would appear one of my examples where == and != operators seem to be at odds with equals() caused it to print, “Success.” What a surprise!



I just wanted to ask if your solution would still work if I added a check asserting that !Objects.equals(item1, function.apply(item1)) and the same for item2, but then Mike provided the exact solution I had in mind.
3 months ago

Campbell Ritchie wrote:I can think of a few things where the usual == and != operators seem to be at odds with equals(), I am keeping quiet about them, too.

It would appear one of my examples where == and != operators seem to be at odds with equals() caused it to print, “Success.” What a surprise!



OK, reading your comment I now see that I probably did not create the puzzle to be specific enough; even with the additional restrictions, which I thought would help, there is a whole different family of solutions than what I originally had in mind. We'll see if someone finds my original idea.
3 months ago

Campbell Ritchie wrote:And?
You have shown several examples of code which appear to fail. If the idea behind the puzzle is to get it to print, “success,” how are you going to do that?



I'm sorry, I don't understand what I did wrong. I have posted puzzles a few times, and they were well received. So yes, when I came across something at work that was somewhat surprising, I turned it into a puzzle. I thought it was evident from the beginning that the aim was to get to the line that prints a message confirming success.
I could have simply posted a solution, but then that's not a puzzle. I could have simply posted the thing I discovered at work saying "Hey, did you know that skdjfh weiru sdfn? I didn't; I just learned that at work today", but then again, it's not fun. These puzzles are intended to provide a fun, and hopefully more effective way of learning something than linking a section of the specs.
I'm sorry if I failed to make my intention clear. I'm sorry the style with the AssertionErrors was sub-par (even though I think I was making assertions, so an AssertionError was a logical choice; it was like a test, without involving a framework).

If there is anything else I should do, please let me know.
If you think the topic is useless or hopeless, then delete it, please.
3 months ago
I can add assert statements, but then we depend on people calling the code with -ea.
The randomisation is there in case people get tricky in the additional code allowed by this instruction:
free to add anything after line 54, or add any number of types to the package dysfunctional, and use them on those lines

But let's try this, without exceptions.

You are only allowed to modify lines 11, 12, 44 and 46. You are, however, free to add anything after line 47, or add any number of types to the package dysfunctional, and use them on those lines.


3 months ago
OK, sorry.
Meanwhile, I realised more solutions exist (I wonder how many hackish loopholes people will find in the original one). So, here is a 'level 2' version of the puzzle, if that is acceptable:



You are only allowed to modify lines 11, 12, 51 and 53. You are, however, free to add anything after line 54, or add any number of types to the package dysfunctional, and use them on those lines.
3 months ago
Find two values x, y and a function F such that the two values are == equal, yet applying the same (pure, stateless) function, which does not contain any conditional statements or the like, produces different results for those values.
So,
x == y,
but F(x) != F(y).

I used capital F above, because our function is not implemented in method f(), that one is merely a factory method to return the actual function.
Ideally, f() is like:
return x -> (a simple expression that maps x to a different value)
For example (this one is obviously not the right solution):
return (String s) -> s.toUpperCase();

I used var and raw types to hide the types of item1, item2, the argument and the return value of function.
3 months ago
Another puzzle. Sorry about the use of a raw type.

3 months ago

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.