Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'ext.rb'
- require 'TestAufgabe1.rb'
- #Zeit in Minuten
- # specification
- #Purpose: converts years, days, hours and mins to mins
- #Contract: total_min(years, days, hours, mins): Natural x Natural x Natural x Natural -> Natural
- #Examples: (1, 2, 3, 4) -> 528664, (3, 247, 13, 23) -> 1933283
- def total_min(years, days, hours, mins)
- check_pre((nat?(years) and nat?(days) and nat?(hours) and nat?(mins)))
- hour_to_min(day_to_hour(year_to_day(years) + days) +hours) + mins
- end
- # specification
- #Purpose: converts years to days
- #Contract: year_to_day(a): Natural -> Natural
- #Examples: (1) -> 365, (2) -> 730
- def year_to_day(a)
- check_pre(nat?(a))
- a * days_per_year
- end
- # specification
- #Purpose: converts days to hours
- #Contract: day_to_hour(d): Natural -> Natural
- #Examples: (1) -> 24, (2) -> 48
- def day_to_hour(d)
- check_pre(nat?(d))
- d * hours_per_day
- end
- # specification
- #Purpose: converts hours to mins
- #Contract: hour_to_min(h): Natural -> Natural
- #Examples: (1) -> 60, (2) -> 120
- def hour_to_min(h)
- check_pre(nat?(h))
- h * mins_per_hour
- end
- def hours_per_day
- return 24
- end
- def mins_per_hour
- return 60
- end
- def days_per_year
- return 365
- end
- puts total_min(1, 67, 5, 59)
- #umgekehrter Weg
- # specification
- #Purpose: converts mins to full years, days, hours and mins
- #Contract: min_to_ydhm(zahl): Natural -> Natural x Natural x Natural x Natural
- #Examples: (123456789) -> 234a 323d 21h 9m, (2413795) -> 4a 216d 5h 55m
- def min_to_ydhm(zahl)
- check_pre(nat?(zahl))
- return "#{full_years(zahl)}a, #{full_days(zahl) % 365}d, #{full_hours(zahl) % 24}h, #{zahl % 60}m"
- end
- # specification
- #Purpose: converts mins to full years
- #Contract: full_years(mins): Natural -> Natural
- #Examples: (1362582) -> 2, (2849275) -> 5
- def full_years(mins)
- check_pre(nat?(mins))
- full_days(mins) / days_per_year
- end
- # specification
- #Purpose: converts mins to full days
- #Contract: full_days(mins): Natural -> Natural
- #Examples: (1362582) -> 946, (2849275) -> 1978
- def full_days(mins)
- check_pre(nat?(mins))
- full_hours(mins) / hours_per_day
- end
- # specification
- #Purpose: converts mins to full hours
- #Contract: full_hours(mins): Natural -> Natural
- #Examples: (3274) -> 54, (72632) -> 1210
- def full_hours(mins)
- check_pre(nat?(mins))
- mins / mins_per_hour
- end
- puts min_to_ydhm(2413795)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement