SERVING THE QUANTITATIVE FINANCE COMMUNITY

 
User avatar
mbunea
Topic Author
Posts: 107
Joined: March 26th, 2015, 11:39 am

Stupid brainteaser

April 22nd, 2015, 6:03 am

I stared at this for a long time and couldn't figure out what it does. Seems like a good problem to have each employee take and if they don't solve it, should be fired.int what(int y, int m) { return m ^ 2 ? 31 - --m % 7 % 2 : 29 - (y & 3 || y & 15 && !(y % 25));}
 
User avatar
oas
Posts: 6
Joined: June 1st, 2012, 9:06 pm

Stupid brainteaser

April 22nd, 2015, 1:04 pm

The person that should be fired is the one that wrote the code.
 
User avatar
MHill
Posts: 488
Joined: February 26th, 2010, 11:32 pm

Stupid brainteaser

April 22nd, 2015, 2:39 pm

Looked to me like it was trying to get month-end dates, but as I've never used C, I had to find out what the operators do. I'm still not clear on bits of it.m ^ 2 ? ... : ...seems to be the equivalent of 'if m=2, do the stuff after the colon, else do the stuff before the colon.'So if m (month) isn't February, return 31 - --m % 7 % 2'--m % 7 % 2' returns 1 if the month doesn't have 31 days (every second month has <31 days until you get to August, then you need every second month from July. This is the % 7 % 2 bit).So we return 31 where there are 31 days in the month, 30 otherwise.If m is February, return '29 - (y & 3 || y & 15 && !(y % 25))'I don't understand the operators here. I get that 'y & 3' returns 0 if the year is a multple of 4 (ie a leap year). I don't get the rest of it.
 
User avatar
mbunea
Topic Author
Posts: 107
Joined: March 26th, 2015, 11:39 am

Stupid brainteaser

April 22nd, 2015, 4:50 pm

I'm joking about firing, in case it isn't clear. This is a problem given in a local computer science journal, for high school kids. A workmate of mine who is training internship students presented them the problem and no one figured out what it does so he asked several colleagues, including me, to try solve it, so he can get an idea on how difficult it is. No one figured it out. Then he told us what if y were for year and m for month. Suddenly the purpose of the code became obvious.Now it seems almost embarassing how we coulnd't make the connection. There's 31 and 29 there also, they now seem like clues.
 
User avatar
MHill
Posts: 488
Joined: February 26th, 2010, 11:32 pm

Stupid brainteaser

April 22nd, 2015, 5:38 pm

Yeah, it was the 'm' and 'y' that made me guess it was a date function, and the 31 and 29 that it was for month ends. I did wonder briefly if it was for finding the last working day / Friday / Wednesday of the month, which is why I looked up C operators.I still don't get what the last bit of the 'February' code does. 'Y & 15' must return zero if the year is a multiple of 16. I don't see what use this is.I don't understand what 'NOT (y modulo 25)' means.And so I don't get what happens when you combine the three elements.Unless it's something to do with not having leap years on the turn of the century / millennium?(And is that really how 'millennium' is spelled?)
 
User avatar
oas
Posts: 6
Joined: June 1st, 2012, 9:06 pm

Stupid brainteaser

April 22nd, 2015, 5:46 pm

A nice explanation of the leap year part of the algorithm is here: http://stackoverflow.com/questions/3220 ... cally-in-c
Last edited by oas on April 21st, 2015, 10:00 pm, edited 1 time in total.
 
User avatar
MHill
Posts: 488
Joined: February 26th, 2010, 11:32 pm

Stupid brainteaser

April 22nd, 2015, 6:50 pm

Thanks! Would never have worked that one out.
 
User avatar
katastrofa
Posts: 8495
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

Stupid brainteaser

April 23rd, 2015, 6:10 am

Quotem ^ 2 ? ... : ...seems to be the equivalent of 'if m=2, do the stuff after the colon, else do the stuff before the colon.'^ is bitwise XOR
 
User avatar
ExSan
Posts: 4548
Joined: April 12th, 2003, 10:40 am

Stupid brainteaser

April 23rd, 2015, 6:44 am

QuoteOriginally posted by: mbuneaI stared at this for a long time and couldn't figure out what it does. Seems like a good problem to have each employee take and if they don't solve it, should be fired.int what(int y, int m) { return m ^ 2 ? 31 - --m % 7 % 2 : 29 - (y & 3 || y & 15 && !(y % 25));}and the brainteaser is ?
 
User avatar
mbunea
Topic Author
Posts: 107
Joined: March 26th, 2015, 11:39 am

Stupid brainteaser

April 23rd, 2015, 7:18 am

Psychological question actually, check if person realizes y is for year and m for month.
 
User avatar
katastrofa
Posts: 8495
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

Stupid brainteaser

April 23rd, 2015, 8:36 am

I thought m was for mother.
 
User avatar
Cuchulainn
Posts: 60470
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Stupid brainteaser

April 23rd, 2015, 9:16 am

Quoteint what(int y, int m) { return m ^ 2 ? 31 - --m % 7 % 2 : 29 - (y & 3 || y & 15 && !(y % 25));}Too much whitespace.
Last edited by Cuchulainn on April 22nd, 2015, 10:00 pm, edited 1 time in total.
http://www.datasimfinancial.com
http://www.datasim.nl

Approach your problem from the right end and begin with the answers. Then one day, perhaps you will find the final question..
R. van Gulik
 
User avatar
daveangel
Posts: 17031
Joined: October 20th, 2003, 4:05 pm

Stupid brainteaser

April 23rd, 2015, 11:05 am

QuoteOriginally posted by: CuchulainnQuoteint what(int y, int m) { return m ^ 2 ? 31 - --m % 7 % 2 : 29 - (y & 3 || y & 15 && !(y % 25));}Too much whitespace.more obfuscation please and write it in FORTRAN
knowledge comes, wisdom lingers
 
User avatar
Cuchulainn
Posts: 60470
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

Stupid brainteaser

April 23rd, 2015, 11:53 am

QuoteOriginally posted by: daveangelQuoteOriginally posted by: CuchulainnQuoteint what(int y, int m) { return m ^ 2 ? 31 - --m % 7 % 2 : 29 - (y & 3 || y & 15 && !(y % 25));}Too much whitespace.more obfuscation please and write it in FORTRANFortran66 and punch cards, of course.
http://www.datasimfinancial.com
http://www.datasim.nl

Approach your problem from the right end and begin with the answers. Then one day, perhaps you will find the final question..
R. van Gulik
 
User avatar
DevonFangs
Posts: 3004
Joined: November 9th, 2009, 1:49 pm

Stupid brainteaser

April 29th, 2015, 8:20 am

Here's a global pattern matcher:main(int c,char**v){return!m(v[1],v[2]);}m(char*s,char*t){return*t-42?*s?63==*t|*s==*t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);}(hint)
ABOUT WILMOTT

PW by JB

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...


GZIP: On