WhiZTiM

simple_course_grading_good_code

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