-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathta.rb
173 lines (144 loc) · 3.37 KB
/
ta.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
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
require 'csv'
require 'pry'
class Student
attr_accessor :name, :grades
def initialize
@name = name
@grades = grades
@grades = []
enter_name
enter_grades
end
def enter_name
puts 'Enter student\'s name:'
@name = gets.chomp
end
def enter_grades
puts 'Enter student\'s grades:'
grade = gets.chomp
while grade.length > 0
@grades.push(grade)
grade = ''
enter_grades
end
end
end
class StudentReport
attr_accessor :report
def initialize
@report = report
@report = []
add_students
end
def add_students
@student = Student.new
@report << @student
@formatted = @student.name.to_s + ',' + @student.grades.join(',')
to_csv
puts 'Add another student? (y/n)'
@add_student = gets.chomp
if @add_student == 'y'
add_students
end
end
def to_csv
File.open('ta.txt', 'a') do |f|
f.puts @formatted
end
end
end
class StudentReportAnalytics
def initialize
@students_array = []
@class_average_array = []
@all_grades = []
show_analytics
end
def proper_name
@student_name = @student_name.split.reverse.join(', ')
end
def find_average
@sum = 0.0
@average_array.each { |x| @sum = @sum + x }
@student_average = @sum / @average_array.length
end
def assign_grade
case @student_average
when 0..59
@student_grade = 'F'
when 60..69
@student_grade = 'D'
when 70..79
@student_grade = 'C'
when 80..89
@student_grade = 'B'
when 90..100
@student_grade = 'A'
end
end
def show_analytics
File.foreach('example.txt') do |l|
CSV.parse(l)
@average_array = l.chomp.split(',')
@student_name = @average_array.shift
@average_array.map! { |i| i.to_i }
@all_grades.push(@average_array)
proper_name
find_average
assign_grade
class_average
@report_style = "#{@student_name} #{@student_average} #{@student_grade}"
@students_array.push([@report_style])
end
# report_card
aggregate_info
end
def report_card
File.open('report.txt', 'a') do |f|
@students_array = @students_array.sort
for i in 0..(@students_array.length - 1)
f. puts @students_array[i][0]
end
end
end
def class_average
sum = 0.0
@class_average_array.push(@student_average)
@class_average_array.each { |x| sum = sum + x }
@class_average = sum / @class_average_array.length
end
def sample_variance
@numerator = 0
@find_extremes.each do |x|
@numerator = @numerator + (x - @class_average)*(x - @class_average)
end
@sample_variance = @numerator/(@find_extremes.length)
end
def standard_deviation
@strdev = Math.sqrt(@sample_variance)
end
def min_max
sum = 0
@find_extremes = []
for i in 0..(@all_grades.length - 1)
@all_grades[i].each { |x| @find_extremes.push(x) }
end
@min = @find_extremes.min
@max = @find_extremes.max
for i in 0..@find_extremes.length
sum = sum + i
end
@sum_for_strdev = sum
end
def aggregate_info
min_max
sample_variance
standard_deviation
puts "The average score is #{@class_average}."
puts "The min score is #{@min}."
puts "The max score is #{@max}."
puts "The standard deviation is #{@strdev}."
end
end
# report = StudentReport.new
analytics = StudentReportAnalytics.new