D
Dave Burt
Jan said:I might continue posting the solutions for the rest of the book if I
decide to keep on reading.
This way I won't develop any bad habits (because hopefully the
solutions will be scrutinized by at least one other person) and it'll
help anyone else reading the book who gets stuck. I doubt the author
of the book will ever get around to publishing the "official"
solutions.
I just looked at the book samples at pragprog and was surprised to see there
are no solutions. That probably means Chris doesn't intend to do official
solutions. Ah, well. Your points are all valid, posting solutions is a good
idea all 'round.
Now, scrutiny and no bad habits. You've only made one mistake, but I'll
point out a couple of things that could be done differently.
puts ''
That's the same as just "puts" by itself, with no parameters.
input = input.to_i - 1
You can avoid going backwards here by moving "input = input + 1" to the end
of the loop. It often makes things simpler to do the increment at the end
rather than the beginning of a loop.
if input%4 == 0 || input%400 == 0 && input%100 != 0
Here's the mistake.
1984 and 2004 are leap years. OK.
1800 and 1900 are not leap years. Not OK - this program lists them as leap
years.
1600 and 2000 are leap years. OK.
In case this info helps, && binds more closely than ||, so what you've got
is the same as:
if input%4 == 0 || (input%400 == 0 && input%100 != 0)
You should be able to rearrange it to give the correct result for the 1984,
2004, 1800, 1900, 1600 and 2000.
Cheers,
Dave