Antypas

Count and Range Count

Apr 14th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import random
  2.  
  3. # Add your count function here
  4. def count(target_grade,scores_list):
  5.     target_hits = 0
  6.     for score in scores_list:
  7.         if score == target_grade:
  8.             target_hits += 1
  9.     return target_hits, target_grade
  10.  
  11. # A counting function for a range of values (closed interval)
  12. def count_range(lower, upper, scores_list):
  13.     cumulative_count = 0
  14.     for x in range(lower, upper+1):
  15.         xcount, xvalue = count(x, scores_list)
  16.         print(xcount, " count of mark ", xvalue)
  17.         cumulative_count = cumulative_count + xcount
  18.     print("cumulative count in range [{0},{1}] > ".format(lower, upper),cumulative_count)
  19.     return cumulative_count
  20.        
  21. # "Create" a list of scores, randomly
  22. scores = []
  23.  
  24. for x in range (0, 30):
  25.   scores.append(random.randint(0, 10))
  26.  
  27. print("list of scores ", scores)
  28.  
  29. # Cumulative or range count function called here
  30. print("Cumulative or range count function called here:")
  31. cumulative_count = count_range(4,7, scores)
  32. print("Sanity check of returned value: ", cumulative_count)
  33.  
  34. # Count function called here
  35. print("Count function called here:")
  36. target_scorers, target_grade = count(7, scores)
  37.  
  38. #print(target_scorers)     #sanity check
  39. #print(target_grade)
  40. print("{0} learners got mark {1}".format(target_scorers, target_grade ))
Add Comment
Please, Sign In to add comment