Halhelms
SIGN UP FOR MY NEWSLETTER

www.savorgold.com is top on wow gold and runescape gold and World of Warcraft gold provider list for trusted services. Their reputation seems to growing by the minute, which isn't surprising because they are one of the safest sellers of Gold. Delivery speed and customer service are very good. They aslo are giving some bonus items depending on the amount of gold you purchase.

 
 
Halhelms

Recent Comments

Recent Entries

RSS

A Little Code Puzzle

Here's a fun puzzle for you to try in the language of your choice. We have two boolean contrarians, A and B. (A contrarian might be too-simply defined as someone who opposes the conventional wisdom on a topic.) Since they're contrarians, we expect/want them to not agree on a topic. If they do, we're in trouble.

What's the shortest function that will return true if A is true AND B is true OR A is false AND B is false?

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Joe Zack's Gravatar XOR to the rescue!

def AreBothTrueOrBothFalse a, b
!(a ^ b)
end
# Posted By Joe Zack | 11/20/10 2:29 PM
Ron Stewart's Gravatar In JavaScript: function f(a, b) { return !(a ? !b : b); }
# Posted By Ron Stewart | 11/20/10 2:31 PM
Hal Helms's Gravatar OK, good. I thought of one other one...
# Posted By Hal Helms | 11/20/10 2:34 PM
Ron Stewart's Gravatar OK, how about this one, slightly shorter (also JavaScript): function g(a, b) { return !(a != b); }
# Posted By Ron Stewart | 11/20/10 2:39 PM
Danilo Celic's Gravatar It seems to me that this boils down to if a and b are equal. So here's a quick JavaScript attempt (removed unnecessary whitespace with attempt to meet the "shortest" function criteria from the post:
function e(a,b){return a==b}
alert(e(true, true)); // alerts true
alert(e(true, false)); // alerts false
alert(e(false, false)); // alerts true
alert(e(false, true)); // alerts false
# Posted By Danilo Celic | 11/20/10 2:57 PM
Ron Stewart's Gravatar (slaps himself in the forehead)
# Posted By Ron Stewart | 11/20/10 3:02 PM
Steve Bryant's Gravatar I must be missing something, because I thought of this:

return ( A == B);

Since they are boolean, they can each only be true or false.
# Posted By Steve Bryant | 11/20/10 3:05 PM
Hal Helms's Gravatar That's the one I was thinking of Danilo!

I like little exercises like this. We all get a tiny bit smarter.
# Posted By Hal Helms | 11/20/10 4:52 PM
Ron Stewart's Gravatar Interesting example of how the way a problem is presented can lead us -- particularly in rushing to a solution -- to try to answer it in a manner that, although aligned with the presentation, isn't necessarily the simplest...
# Posted By Ron Stewart | 11/20/10 5:00 PM
Hal Helms's Gravatar Very good point, Ron. I deliberately stated in a way to make it sound as complex as possible.
# Posted By Hal Helms | 11/20/10 6:06 PM
 
   
Clicky Web Analytics