Skip to content

Instantly share code, notes, and snippets.

@leahnp
Created February 18, 2016 06:42
Show Gist options
  • Save leahnp/e15cf152f3d21759ed7f to your computer and use it in GitHub Desktop.
Save leahnp/e15cf152f3d21759ed7f to your computer and use it in GitHub Desktop.
HW 2-16 "Hours in a year"
#leah petersen
#homework 2-16-16 "hours-in-a-year"
#format number strings with US-style commas
def separate_comma(number)
whole, decimal = number.to_s.split(".")
whole_with_commas = whole.chars.to_a.reverse.each_slice(3).map(&:join).join(",").reverse
[whole_with_commas, decimal].compact.join(".")
end
#compute hours (24) in a year (365.25)
hours_in_year = 24 * 365.25
puts "There are " + separate_comma(hours_in_year) + " hours in a year."
#compute minutes (60 in hour) in a decade (10 years)
mins_in_year = hours_in_year * 60
mins_in_decade = mins_in_year * 10
puts "There are " + separate_comma(mins_in_decade) + " minutes in a decade."
#compute how many seconds old I am (31 years)
secs_in_year = mins_in_year * 60
age_in_seconds = secs_in_year * 31
puts "I am " + separate_comma(age_in_seconds) + " seconds old."
#compute Jeremy's age in years, he is 1,136,086,041.6 seconds old
jeremys_age = 1136086041.6 / secs_in_year
jeremys_age = jeremys_age.round
puts "Jeremy is " + separate_comma(jeremys_age) + " years old."
@theutahkate
Copy link

Awesome! Your code is very easy to read and functional - your comments are specific and concise, your variable names sound like what they are, and your code well-organized. That’s a clever/efficient method you wrote to add commas to larger numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment