Software Development (CSC 321 2016S) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [Teaching & Learning]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Outlines]
Reference: [Slack] [Ruby@CodeCademy]
Related Courses: [Rails Tutorial] [CSC 321 2016S @ EdX] [CSC 321 2015S (Davis)] [CSC 321 2015F (Rebelsky)] [CSC 322 2016S (Rebelsky)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker]
Overview
class TimeSetter def self.convert(daysleft) year = 1980 # Keep moving into the next year until we are down # to a reasonable number of days while (daysleft > 365) do daysleft -= 365 # There's an extra day in a leap year! if (isleapyear year) daysleft -= 1 end year += 1 end return (year,daysleft) end def self.isleapyear(year) return (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0); end end
This is still wrong. Why?
daysleft
is a bad name for the parameter, since that's notFix?
class TimeSetter def self.convert(days_since_start_of_time) daysleft = days_since_start_of_time year = 1980 # Keep moving into the next year until we are down # to a reasonable number of days while (daysleft > 365) do daysleft -= 365 # There's an extra day in a leap year! if (isleapyear year) daysleft -= 1 end year += 1 end return (year,daysleft) end def self.isleapyear(year) return (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0); end end
Approach one: Adding the code
if daysleft > 0
++year
else if (is_leap_year year)
daysleft = 366
else
daysleft = 365
end
class TimeSetter def self.convert(days_since_start_of_time) daysleft = days_since_start_of_time year = 1980 # Keep moving into the next year until we are down # to a reasonable number of days while (daysleft > 365) do daysleft -= 365 # There's an extra day in a leap year! if (isleapyear year) daysleft -= 1 end if daysleft > 0 ++year else if (is_leap_year year) daysleft = 366 else daysleft = 365 end end return (year,daysleft) end def self.isleapyear(year) return (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0); end end
Figure out a loop invariant (a statement about the status of the system that you can guarantee is true at the end of the loop if it's true at the beginning of the loop, and that is helpful in achieving the goals of the program.)
Another solution: In the while loop, do a more careful test
class TimeSetter def self.convert(days_since_start_of_time) daysleft = days_since_start_of_time year = 1980 # Keep moving into the next year until we are down # to a reasonable number of days while (daysleft > 365) do if (is_leap_year year && daysleft >= 366) daysleft -= 366 year += 1 else if (is_leap_year year) break out of the loop; else daysleft -= 365 year += 1 end end return (year,daysleft) end def self.isleapyear(year) return (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0); end end
class TimeSetter def self.convert(days_since_start_of_time) daysleft = days_since_start_of_time year = 1980 # Keep moving into the next year until we are down # to a reasonable number of days while (daysleft > (daysinyear year)) daysleft -= (daysinyear year) year += 1 end return (year,daysleft) end def self.daysinyear(year) if (isleapyear year) return 366 else return 365 end end def self.isleapyear(year) return (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0); end end
Why do we have professional codes of ethics?