Skip to content

Commit 8696e72

Browse files
committed
pushing learn to program exercises
1 parent 36acf5e commit 8696e72

5 files changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Learn to Program - Second Edition
2+
# Exercise from Chapter 2 - Numbers
3+
# Sam Gerber
4+
5+
# Write a program that tells you how many hours are in a year
6+
7+
def hours_in_year(year)
8+
hours = 365 * 24
9+
hours += 24 if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
10+
hours
11+
end
12+
13+
puts hours_in_year 1900 # =>8760
14+
puts hours_in_year 2000 # =>8784
15+
puts hours_in_year 2001 # =>8760
16+
puts hours_in_year 2004 # =>8784
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Learn to Program - Second Edition
2+
# Exercise from Chapter 2 - Numbers
3+
# Sam Gerber
4+
5+
# Write a program that tells you how many minutes are in a decade
6+
7+
def minutes_in_decade(start_year = 1900)
8+
days = 0
9+
start_year.step.take(10).each do |year|
10+
days += 365
11+
days += 1 if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
12+
end
13+
days * 24 * 60 * 60
14+
end
15+
16+
puts minutes_in_decade 1897 # =>315446400
17+
puts minutes_in_decade 2000 # =>315619200
18+
puts minutes_in_decade 2001 # =>315532800
19+
puts minutes_in_decade 2004 # =>315619200
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Learn to Program - Second Edition
2+
# Exercise from Chapter 2 - Numbers
3+
# Sam Gerber
4+
5+
# Write a program that tells you your age in seconds
6+
7+
def age_in_seconds(day, month, year)
8+
birthday = Time.local(year, month, day)
9+
Time.new - birthday
10+
end
11+
12+
puts age_in_seconds(31, 12, 1987) # => 877_000_000
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Learn to Program - Second Edition
2+
# Exercise from Chapter 2 - Numbers
3+
# Sam Gerber
4+
5+
# If I am 1_160 million seconds old, how old am I?
6+
7+
def age_in_years(seconds)
8+
birthday = Time.new - seconds
9+
Time.new.year - birthday.year
10+
end
11+
12+
puts age_in_years(1_160_000_000) # => 877_000_000
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Learn to Program - Second Edition
2+
# Examples from book
3+
# Sam Gerber
4+
5+
# Chapter 2 - Numbers
6+
7+
puts 1 + 2 # => 3
8+
9+
# 2.4 Simple Arithmetic
10+
puts 1.0 + 2.0 # => 3.0
11+
puts 2.0 * 3.0 # => 6.0
12+
puts 5.0 - 8.0 # => -3.0
13+
puts 9.0 / 2.0 # => 4.5
14+
15+
puts 1 + 2 # => 3
16+
puts 2 * 3 # => 6
17+
puts 5 - 8 # => -3
18+
puts 9 / 2 # => 4
19+
20+
puts 5 * (12-8) + -15 # =>5
21+
puts 98 + (59872 / (13*8)) * -51 # =>-29227

0 commit comments

Comments
 (0)