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

The person that should be fired is the one that wrote the code.

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.

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.

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

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.

- katastrofa
**Posts:**9318**Joined:****Location:**Alpha Centauri

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

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 ?

Psychological question actually, check if person realizes y is for year and m for month.

- katastrofa
**Posts:**9318**Joined:****Location:**Alpha Centauri

I thought m was for mother.

- Cuchulainn
**Posts:**62369**Joined:****Location:**Amsterdam-
**Contact:**

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.

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

- Cuchulainn
**Posts:**62369**Joined:****Location:**Amsterdam-
**Contact:**

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.

- DevonFangs
**Posts:**3004**Joined:**

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)

GZIP: On