Instantly share code, notes, and snippets.

@dbohdan /grades.tcl forked from smls/grades.p6
Last active Mar 9, 2018

Embed
What would you like to do?
Data munging task from the Perl 6 advent calendar
#!/usr/bin/env tclsh
package require sqlite3
package require fileutil
sqlite3 db :memory:
db eval {CREATE TABLE grades(name TEXT PRIMARY KEY, grade TEXT)}
foreach {name grade} [::fileutil::cat grades.txt] {
if {![regexp {[A-F][+-]?} $grade]} {
puts "Can't parse pair '$name $grade'"
exit 1
}
db eval {INSERT INTO grades VALUES ($name, $grade)}
}
puts "Zsófia's grade: [db eval {SELECT grade FROM grades WHERE name='Zsófia'}]"
puts "List of students with a failing grade:"
puts " [join [
db eval {SELECT name FROM grades WHERE grade >= 'E' ORDER BY grade}
] {, }]"
set grades [db eval {
SELECT DISTINCT trim(grade, '+-') FROM grades ORDER BY grade
}]
puts "Distribution of grades by letter:"
foreach grade $grades {
puts -nonewline " $grade: "
set count [db eval {
SELECT count(name) FROM grades WHERE trim(grade, '+-')=$grade
}]
puts -nonewline "$count student"
if {$count > 1} { puts -nonewline s }
puts {}
}
Peter B
Celine A-
Zsófia B+
João F
Maryam B+
秀英 B-
Finn D+
Aarav A
Emma F
Omar B
Zsófia's grade: B+
List of students with a failing grade:
João, Emma
Distribution of grades by letter:
A: 2 students
B: 5 students
D: 1 student
F: 2 students
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment