WhiZTiM

simple_course_grading_short_code

Feb 2nd, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. #!/usr/bin/env python
  2. marks = [75, 45, 89, 55, 65, 50, 72, 70, 60, 80, 95, 34, 83, 71, 65, 53, 58, 79, 85, 21, 67, 94, 77]
  3.          
  4. def grade_single_score(score_mark):
  5.     grades = [['U', 0,  49], ['D',  50, 59], ['C',  60, 69], ['B',  70, 79], ['A',  80, 89], ['A*', 90, 100]]
  6.     for grade in grades:
  7.         if grade[1] <= score_mark <= grade[2]:
  8.             return grade[0]
  9.     return 'N/A'
  10.    
  11. def grade_and_print_scores(score_list):
  12.     for idx, score in enumerate(score_list): print("Mark [%d] - %d - %s" % (idx, score, grade_single_score(score)))
  13.  
  14. def print_grade_distribution(score_list, grades_distr = {}):
  15.     for score in score_list:
  16.     grade = grade_single_score(score)
  17.     grades_distr[grade] = 1 if not grades_distr.has_key(grade) else grades_distr[grade] + 1
  18.     for grade, frequency in sorted(grades_distr.items(), lambda lhs, rhs: -1 if lhs[0].endswith('*') else cmp(lhs, rhs)):
  19.     print("Grade %s - %i" % (grade, frequency))
  20.    
  21. grade_and_print_scores(marks)
  22. print("--------------------")
  23. print_grade_distribution(marks)
Add Comment
Please, Sign In to add comment