Page 1 of 2

### Stupid brainteaser

Posted: 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));}

### Stupid brainteaser

Posted: April 22nd, 2015, 1:04 pm
The person that should be fired is the one that wrote the code.

### Stupid brainteaser

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

### Stupid brainteaser

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

### Stupid brainteaser

Posted: 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?)

### Stupid brainteaser

Posted: 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

### Stupid brainteaser

Posted: April 22nd, 2015, 6:50 pm
Thanks! Would never have worked that one out.

### Stupid brainteaser

Posted: 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

### Stupid brainteaser

Posted: 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 ?

### Stupid brainteaser

Posted: April 23rd, 2015, 7:18 am
Psychological question actually, check if person realizes y is for year and m for month.

### Stupid brainteaser

Posted: April 23rd, 2015, 8:36 am
I thought m was for mother.

### Stupid brainteaser

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

### Stupid brainteaser

Posted: 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

### Stupid brainteaser

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

### Stupid brainteaser

Posted: 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)