Skip to main content
edited tags
Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Tweeted twitter.com/StackMma/status/1045689999197392902
Source Link
bobthechemist
  • 19.8k
  • 4
  • 56
  • 140

Better way to sort course grades

I use the following code to create a histogram of grades. Grades are provided as a List of strings such as "A-", "D+", "E" (don't ask), etc. The +/- substitution hack is necessary to sort the grades in my desired order.

f[s_String] := 
  If[StringLength@s == 1, s <> "b", 
   StringReplace[s, {"+" -> "a", "-" -> "c"}]];
g[s_String] := StringReplace[s, {"a" -> "+", "b" -> "", "c" -> "-"}];
Reverse@Sort@
  Tally@(f /@ {"C", "B-", "B", "B+", "B", "C-", "E", "B", "D", "D+", 
      "C-", "B", "C", "B-", "C+", "E", "B-", "B-", "C-", "A", "C", 
      "B-", "B", "C+", "B", "C", "B+", "C+", "D-", "C", "A", "E", "B",
       "B+", "C", "C", "D+", "D", "C", "C-", "C", "B", "D", "B", "B", 
      "E", "B", "W", "W"})
BarChart[Last /@ %, ChartLabels -> g /@ First /@ %]

(* {{"Wb", 2}, {"Eb", 4}, {"Dc", 1}, {"Db", 3}, {"Da", 2}, {"Cc", 
  4}, {"Cb", 9}, {"Ca", 3}, {"Bc", 5}, {"Bb", 11}, {"Ba", 3}, {"Ab", 
  2}} *)

enter image description here

I'm looking for a less hackish way to do this. Any suggestions?