-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstudents.rb
More file actions
204 lines (178 loc) · 4.54 KB
/
students.rb
File metadata and controls
204 lines (178 loc) · 4.54 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
students = []
eye_colors = []
ages = []
heights = []
blood_types = []
File.open("student_data.csv").each do |line|
info = line.split(",")
students.push(info[0].strip)
eye_colors.push(info[1].strip)
ages.push(info[2].strip.to_i)
height = (info[3].strip.to_i * 12) + info[4].strip.to_i
heights.push(height)
blood_types.push(info[5].strip)
end
def brown_eyes(eye_colors)
num = 0
eye_colors.each do |color|
if color == "Brown"
num += 1
end
end
return num
end
puts "How many students have brown_eyes?"
puts brown_eyes(eye_colors)
def can_drive(students, ages)
who_can_drive = []
ages.each_with_index do |age, i|
if age > 15
who_can_drive.push(students[i].strip)
end
end
return who_can_drive
end
puts "Which students can drive?"
puts can_drive(students, ages)
def green_eyed_girls(students, eye_colors)
green_eyed_girls_array = []
eye_colors.each_with_index do |color, i|
if not i.odd?
if color == "Green"
green_eyed_girls_array.push(students[i].strip)
end
end
end
return green_eyed_girls_array
end
puts "Which girls have green eyes?"
puts green_eyed_girls(students, eye_colors)
def num_of_vowels(string)
vowels = 0
string.chars.each do |char|
if is_vowel?(char)
vowels += 1
end
end
return vowels
end
def is_vowel?(char)
return char.downcase == "a" || char.downcase == "e" || char.downcase == "i" || char.downcase == "o" || char.downcase == "u"
end
def sophomore_vowels(students, ages)
most_vowels = students[0]
max_vowels = 0
students.each_with_index do |current_name, i|
vowels = 0
if ages[i] == 15
vowels = num_of_vowels(current_name)
if vowels > max_vowels
max_vowels = vowels
most_vowels = current_name
end
end
end
return most_vowels
end
puts "Which sophomore has the most vowels in their name?"
puts sophomore_vowels(students, ages)
def average_height(heights)
sum = 0
heights.each do |height|
sum += height
end
return (sum.to_f / heights.length)
end
puts "What is the average height of the students in inches?"
puts average_height(heights)
def in_array(array, check_value)
array.each do |value|
if value == check_value
return true
end
end
return false
end
def blood_donation(students, blood_types, student_name)
student = 0
donors = []
student_type = ""
students.each_with_index do |student, i|
if student == student_name
student_type = blood_types[i]
end
end
avalible_types = []
if student_type == "A"
avalible_types = ["A", "AB"]
elsif student_type == "B"
avalible_types = ["B", "AB"]
elsif student_type == "O"
avalible_types = ["A", "B", "O", "AB"]
elsif student_type == "AB"
avalible_types = ["AB"]
end
blood_types.each_with_index do |type, i|
if in_array(avalible_types, type)
donors.push(students[i])
end
end
return donors
end
puts "Which people can donate blood to a secific student given their blood type?"
puts blood_donation(students, blood_types, "Wendy")
def most_blood(students, blood_types)
max_donors = 0
highest_donor_round_1 = ""
highest_donors = []
students.each_with_index do |student_name, i|
donors = blood_donation(students, blood_types, student_name)
if donors.length > max_donors
max_donors = donors.length
highest_donor_round_1 = student_name
end
end
highest_donors.push(highest_donor_round_1)
students.each_with_index do |student_name, i|
donors = blood_donation(students, blood_types, student_name)
if donors.length == max_donors
if student_name != highest_donor_round_1
highest_donors.push(student_name)
end
end
end
return highest_donors, max_donors
end
puts "Which student has the highest number of donors, and how many?"
puts most_blood(students, blood_types)
def average_green_eyed_girls(students, ages, eye_colors)
greenEyedGirls = []
sum = 0
eye_colors.each_with_index do |color, i|
if color == "Green"
greenEyedGirls.push(students[i])
sum += ages[i]
end
end
average = sum.to_f / (greenEyedGirls.length)
return average
end
puts "What is the average age of the green eyed students?"
puts average_green_eyed_girls(students, ages, eye_colors)
def closest_to_average_green_eyes(students, ages, eye_colors)
average = average_green_eyed_girls(students, ages, eye_colors)
distance_to_average = 150
student = ""
eye_colors.each_with_index do |color, i|
distance_to_age = (ages[i] - average).abs
if color == "Green"
if distance_to_age < distance_to_average
distance_to_average = distance_to_age
student = students[i]
end
end
end
return student
end
puts "Which green eyed student is closest to the average?"
puts closest_to_average_green_eyes(students, ages, eye_colors)