• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

[Java puzzle] Dysfunctional

 
Ranch Hand
Posts: 135
4
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
Another puzzle. Sorry about the use of a raw type.

 
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
What is the puzzle supposed to do? Find two values that are neither equal to each other nor unequal?
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Number of slices to send:
    Optional 'thank-you' note:
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.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
Please don't “correct” posts like that. It doesn't add clarity, but makes it uncertain what I replied to. Please post any changes in a new post.
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Number of slices to send:
    Optional 'thank-you' note:
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.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:

Istvan Kovacs wrote:. . .

I am finding this question difficult to understand. I would have thought you are supposed to find two values that will cause the program to print, “Success.” Needless to say, that code can be relied on to crash with an exception.
If you aren't allowed ifs, why would you be allowed a switch‑case?
I also think it is dubious practise to throw an Error of any kind. Maybe an assertion is allowed to throw an AssertionError, but your code doesn't contain any assertions.
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Number of slices to send:
    Optional 'thank-you' note:
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.


 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
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?
 
Marshal
Posts: 8880
638
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:You have shown several examples of code which appear to fail


Well, probably for a purpose not to spoil the correct answer that gets to "Success!";

Campbell Ritchie wrote:how are you going to do that?


I believe Istvan left this exercise for a reader Yesterday I had a quick think, but then I was too busy with advent of code, so no luck

 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
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!
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Number of slices to send:
    Optional 'thank-you' note:

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.
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Number of slices to send:
    Optional 'thank-you' note:

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.
 
Master Rancher
Posts: 4905
74
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Number of slices to send:
    Optional 'thank-you' note:

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.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
I tried 0.0 and -0.0 myself, but my function only forces boxing by casting to (Double): x -> (Double)x
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
You can create a similar puzzle with NaNs, but all the Boolean tests would have to be reversed.
 
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:
Thanks for posting this, Istvan. Your puzzles are always welcome.
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Number of slices to send:
    Optional 'thank-you' note:

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. http://www.pmsas.pr.gov.br/wp-content/?id=coderanch-1z0-809&exam=t/768833/Java-puzzle-don-feel
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
Yes, I remember that discussion now I have seen your link.
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Number of slices to send:
    Optional 'thank-you' note:
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.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
Once that values have been boxed, there is no need to do anything else; they return false from equals().
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Number of slices to send:
    Optional 'thank-you' note:
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.
 
Istvan Kovacs
Ranch Hand
Posts: 135
4
  • Number of slices to send:
    Optional 'thank-you' note:

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |