-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep8.rb
69 lines (60 loc) · 1.89 KB
/
step8.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# First we print the list of students
# students = [
# {name: "Dr. Hannibal Lecter", cohort: :november},
# {name: "Darth Vader", cohort: :november},
# {name: "Nurse Ratched", cohort: :november},
# {name: "Michael Corleone", cohort: :november},
# {name: "Alex DeLarge", cohort: :november},
# {name: "The Wicked Witch of the West", cohort: :november},
# {name: "Terminator", cohort: :november},
# {name: "Freddy Krueger", cohort: :november},
# {name: "The Joker", cohort: :november},
# {name: "Joffrey Baratheon", cohort: :november},
# {name: "Norman Bates", cohort: :november}
# ]
def input_students
puts 'Please enter the names of the students and then their cohort'
puts 'To finish, just hit return twice'
students = []
# While name is not empty repeat this code.
loop do
name = gets.chomp
break if name.empty?
cohort = gets.chomp.to_sym
students << {name: name, cohort: cohort}
puts students.length != 1 ? "Now we have #{students.count} students" : "Now we have #{students.count} student"
end
students
end
def print_header
puts "The students of Villains Academy"
puts "-------------"
end
def print(students)
students.each_with_index do |student, index|
puts "#{index + 1}. #{student[:name]} (#{student[:cohort]} cohort)"
end
end
def print_footer(names)
puts "Overall, we have #{names.count} great students"
end
def organise_by_cohort(students)
# Create a list of cohorts
cohort_list = students.map do |student|
student[:cohort]
end
# Removes duplicates from that list
compact_list = cohort_list.uniq
students_organised = []
compact_list.each do |value|
students.each do |student|
if student[:cohort] == value then students_organised.push(student) end
end
end
students_organised
end
students = input_students
organised_students = organise_by_cohort(students)
print_header
print(organised_students)
print_footer(students)