There is a task (condition before the code). In theory, the program works correctly, but there are a couple of nuances in its work. At the same time I will ask a few questions that worry me no less
- What if we have several people have a maximum print speed?
- How to simplify the value in the hash (a random number is taken as close as possible to the real one)
- In your opinion, is it justified to use the class in this situation? And how would you generally change the structure.
- Why the
result.better.med_speed.low_speed(list)
construction does not work
PS, please do not swear if the written code caused you pain.
Condition
For a certain group of students (a total of 25 people in the group) data on the speed of text input from the keyboard (the number of characters entered per 10 minutes) is known. It is required to make a report in the following form: print the name and speed of entry of the most effective student; the average entry rate in this group, the names of those students whose entry rate is lower than the average.
class Speed @med=0 def better(list) #puts (list.sort_by{ |k,v| v}).to_s max=[] max << list.max_by { |k,v| v } puts max.to_s #puts list.to_a.to_s end def med_speed(list) @med=(list.values.inject(0){ |res,v| res+v})/25 puts "#{@med} -- средняя скорость печати" end def low_speed(list) bad_stud = list.find_all{|k,v| v<@med} puts "#Скорость печати ниже среднего имеют: #{bad_stud.to_s}" end end list = { Tim: (10 * (rand(150) + 100)), Tereza: (10 * (rand(150) + 100)), Otto: (10 * (rand(150) + 100)), Pavel: (10 * (rand(150) + 100)), Kitti: (10 * (rand(150) + 100)), George: (10 * (rand(150) + 100)), Sandra: (10 * (rand(150) + 100)), Patrik: (10 * (rand(150) + 100)), Nelson: (10 * (rand(150) + 100)), Fedel: (10 * (rand(150) + 100)), Hillary: (10 * (rand(150) + 100)), Marsel: (10 * (rand(150) + 100)), Boris: (10 * (rand(150) + 100)), Nikolay: (10 * (rand(150) + 100)), Juna: (10 * (rand(150) + 100)), Ivan: (10 * (rand(150) + 100)), Liza: (10 * (rand(150) + 100)), Karin: (10 * (rand(150) + 100)), Helga: (10 * (rand(150) + 100)), Josh: (10 * (rand(150) + 100)), Morris: (10 * (rand(150) + 100)), Wally: (10 * (rand(150) + 100)), Brigt: (10 * (rand(150) + 100)), Polina: (10 * (rand(150) + 100)), Markiz: (10 * (rand(150) + 100)) } #list.each{ |k,v| puts "#{k} : #{v}" } result = Speed.new result.better(list) result.med_speed(list) result.low_speed(list)