-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise7.rb
39 lines (34 loc) · 990 Bytes
/
exercise7.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#Hashes
students = {:cohort1 => 34 , :cohort2 => 42 , :cohort3 => 22}
new_students = {}
#displays the name and number of students
def displays (hash)
hash.map do |key , value|
puts "#{key}:#{value} students"
end
end
displays(students)
#add cohort 4
students[:cohort4] = 43
new_students = students
puts "new hash #{students}"
#use the keys method to output all of the cohort names
cohort_names = students.keys
puts "cohort names #{cohort_names}"
#another way of using the keys method
puts "cohort names #{students.keys}"
#increase each cohort size by 5% and display the new results
new_students.each do |key , value|
new_value = (value * 1.05).to_i
puts "#{key} = #{new_value}"
new_students[key] = new_value
end
#delete the 2nd cohort and redisplay the hash
new_students.delete(:cohort2)
puts "#{new_students}"
#Calculate the total number of students
total_students = 0
new_students.each do |key , value|
total_students = total_students + value
end
puts total_students