#!/usr/bin/env python # coding: utf-8 # # Lecture 6, Exercises B # In[5]: def check(actual, expected): if expected != actual: print( f"Function should return the value {expected}, it is returning the value {actual}") else: print(f"Congratulations, the test case passed!") # ## Question 1 # ### 1.1 # # The 10th grade exam for this year just ended. Help prepare result certificates for students by making a list of lists (called matric) containing the grades obtained in the nine subjects. Each list should contain a subject as the first index and the corresponding grade as the second index. Initialize all grades to 100. # In[ ]: matric = 0 # ### 1.2 # # Define a function to determine if all grades in the matric matrix are equal to 100. If all grades are 100, print True. If not, print False. (Use range.) # In[ ]: def hundred(lst): return False # In[ ]: L = [['Amharic', 90], ['English', 90], ['Maths', 100], ['Physics', 100], ['Biology', 100], ['Chemistry', 100], ['History', 100], ['Civics', 100], ['Geography', 100]] check(hundred(L), False) M = [['English', 100], ['Maths', 100], ['Physics', 100], ['Biology', 100], [ 'Chemistry', 100], ['History', 100], ['Civics', 100], ['Geography', 100]] check(hundred(M), 'You do not have the right number of subjects.') # ### 1.3 # # Now change the biology grade in matric to 75 without completely redefining the list. Print the matric list to confirm you altered the list as expected. Then, re-run your function from 1.2 to determine if you obtain the expected result. # In[ ]: # ### 1.4 # # Convert the grades in the matric matrix to letter grades. For this exercise, 90-100 is an A, 80-90 is a B, 70-80 is a C, and other scores should return an error that one of the above exercises was not properly completed. (You may pick whether to use range or not.) # In[ ]: def convert(lst): return False # Check that your matrix is correct by testing it on the matric list # In[ ]: L = [['Amharic', 100], ['English', 95], ['Maths', 91], ['Physics', 85], ['Biology', 75], ['Chemistry', 90], ['History', 75], ['Civics', 72], ['Geography', 84]] check(convert(L), [['Amharic', 'A'], ['English', 'A'], ['Maths', 'A'], ['Physics', 'B'], ['Biology', 'C'], ['Chemistry', 'B'], ['History', 'C'], ['Civics', 'C'], ['Geography', 'B']]) # ### 1.5 # # Write a function to print the level of distinction a student should receive if they obtain the scores in the matric matrix. Use loops to write a function that can accomplish this. (Do not use range.) 4 As = Distinction, 5 As = Great Distinction, and 6-9 As = Very Great Distinction. # In[13]: a = {} a["b"] = 5 a # In[25]: def distinction(lst): #memory = {} '''for i in lst: if i[1] in memory: memory[i] += 1 if i[1] not in memory: memory[i[1]] = 1 ''' a = 0 for i in lst: if i[1] == 'A': a+=1 if a >= 6: return "Very Great Distinction" elif a>= 5: return "Great Distinction" elif a == 4: return "Distinction" return "No distinction" # Test your function on the matric list # In[26]: L = [['Amharic', 'A'], ['English', 'A'], ['Maths', 'A'], ['Physics', 'B'], ['Biology', 'C'], ['Chemistry', 'B'], ['History', 'C'], ['Civics', 'C'], ['Geography', 'B']] check(distinction(L), 'No distinction') M = [['Amharic', 'A'], ['English', 'A'], ['Maths', 'A'], ['Physics', 'A'], ['Biology', 'C'], ['Chemistry', 'B'], ['History', 'C'], ['Civics', 'C'], ['Geography', 'B']] check(distinction(M), 'Distinction') N = [['Amharic', 'A'], ['English', 'A'], ['Maths', 'A'], ['Physics', 'A'], ['Biology', 'A'], ['Chemistry', 'B'], ['History', 'C'], ['Civics', 'C'], ['Geography', 'B']] check(distinction(N), 'Great Distinction') O = [['Amharic', 'A'], ['English', 'A'], ['Maths', 'A'], ['Physics', 'A'], ['Biology', 'A'], ['Chemistry', 'A'], ['History', 'C'], ['Civics', 'C'], ['Geography', 'B']] check(distinction(O), 'Very Great Distinction') # ## Question 2 # ### 2.1 # # Assume that all grades (A, B, C, D, F) are equally likely for all students (with probability equal to 0.2) on the matric exam. Define a list of lists (called probs) where each list has the letter grade at the first index and the probability of obtaining the corresponding letter grade at the second index. # In[ ]: probs = 0 # ### 2.2 # # Write a function that uses a for loop to find the total probability of obtaining any of the grades (A, B, C, D, F). If the probabilities sum to 1, then print possible. If the probabilities sum to more or less than 1, print impossible. Make a helper function to sum values in lists. # In[ ]: def summer1(lst): return False def check_probs(num): return False # Test your function on the probs list you made in exercise 2.1 # In[ ]: L = [['A', 0.5], ['B', 0.1], ['C', 0.1], ['D', 0.1], ['F', 0.2]] check(check_probs(summer1(L)), 'Possible') M = [['A', 0.5], ['B', 0.1], ['C', 0.1], ['D', 0.1], ['F', 0.3]] check(check_probs(summer1(M)), 'Impossible') N = [['A', 0.5], ['B', 0.3], ['C', 0.1], ['D', 0.1], ['F', 0.3], ['Z', 0.2]] check(check_probs(summer1(N)), 'Your list does not have the correct number of possible grades.') # ### 2.3 # # Answer question 2.2 using a while loop instead of a for loop. You may use the same check_probs function from exercise 2.2. # In[ ]: def summer2(lst): return False check_probs(probs) # In[ ]: L = [['A', 0.5], ['B', 0.1], ['C', 0.1], ['D', 0.1], ['F', 0.2]] check(check_probs(summer2(L)), 'Possible') M = [['A', 0.5], ['B', 0.1], ['C', 0.1], ['D', 0.1], ['F', 0.3]] check(check_probs(summer2(M)), 'Impossible') N = [['A', 0.5], ['B', 0.3], ['C', 0.1], ['D', 0.1], ['F', 0.3]] check(check_probs(summer2(N)), 'Impossible') # ### 2.4 # # Define a function to change the probabilities of the grades in the probs list. Set the probability of F to 0.3 and decrease the probability by 0.05 for each rise in letter grade. Use a while loop to accomplish this, and print the new_probs list to confirm that you have the proper elements. (You could define a new `list = [['A',0.1],['B',0.15],['C',0.2],['D',0.25],['F',0.3]]`, but try to alter the existing list for this problem.) # In[ ]: def change(lst): return False new_probs = 0 # ### 2.5 # # Define a function that takes in the matric list from exercise 1 and returns a list that contains the number of subjects in which each grade was obtained. The returned list should be arranged as follows: [a_count,b_count,c_count,d_count,f_count]. Define matric_count as a list containing the grade counts from the matric list. # In[ ]: def counts(lst): return False matric_count = 0 # In[ ]: L = [['Amharic', 'A'], ['English', 'A'], ['Maths', 'A'], ['Physics', 'B'], ['Biology', 'C'], ['Chemistry', 'B'], ['History', 'C'], ['Civics', 'C'], ['Geography', 'B']] check(counts(L), [3, 3, 3, 0, 0]) M = [['Amharic', 'A'], ['English', 'A'], ['Maths', 'A'], ['Physics', 'A'], ['Biology', 'C'], ['Chemistry', 'B'], ['History', 'C'], ['Civics', 'C'], ['Geography', 'B']] check(counts(M), [4, 2, 3, 0, 0]) N = [['Amharic', 'A'], ['English', 'A'], ['Maths', 'A'], ['Physics', 'A'], ['Biology', 'A'], ['Chemistry', 'B'], ['History', 'C'], ['Civics', 'D'], ['Geography', 'F']] check(counts(N), [5, 1, 1, 1, 1]) # ### 2.6 # # Define a function that takes in the grade count list from exercise 2.5 as lst1 and the adjusted probability list from exercise 2.4 as lst2. This function should determine the probability of obtaining the given matric result using a helper function for multiplication. Do the math to confirm your answer is right. # In[64]: def multiplication(lst): val = 0 # You need to redefine val return round(val, 5) def likelihood(lst1, lst2): return False # Test your function using the matric_count and new_probs lists # In[ ]: L1 = [3, 3, 3, 0, 0] L2 = [['A', 0.], ['B', 0.3], ['C', 0.2], ['D', 0.2], ['F', 0.2]] check(multiplication(likelihood(L1, L2)), 0.) M1 = [3, 3, 3, 0, 0] M2 = [['A', 0.2], ['B', 0.2], ['C', 0.2], ['D', 0.2], ['F', 0.2]] M3 = (0.2 ** 9) check(multiplication(likelihood(M1, M2)), round(M3, 5)) N1 = [9, 0, 0, 0, 0] N2 = [['A', 1.0], ['B', 0.], ['C', 0.], ['D', 0.], ['F', 0.]] N3 = (1.0 ** 9) check(multiplication(likelihood(N1, N2)), N3) # ## Question 3 - CHALLENGE # ### 3.1 # # A math exam exam is coming up. You want to prepare, but you misplaced your calculator, and the computer your using only has jupyter notebook available. Normally you could use arithmetic operators, but your input is given as a string (ex. '1 + 2 * 8 ÷ 2'). Make a calculator capable of completing addition, subtraction, multiplication, and division. (Do not take order of operations into account. Perform functions from left to right.) # In[ ]: def calc1(string): return False # Test your function on this equation '10 + 20 * 80 ÷ 20' # In[ ]: check(calc1('1+2+3÷2*12'), 36) check(calc1('1+2+3÷2*0'), 0) check(calc1('100+2+3÷5*12'), 252) # ### 3.2 # # Now complete exercise 3.1 taking into acount the order of operations. Again, you only need to add functionality for addition, subtraction, multiplication, and division. # In[ ]: def calc2(string): return False # Test your function on the equation '10 + 20 * 80 ÷ 20' # In[ ]: check(calc2('1+2+3÷2*12'), 21) check(calc2('1+2+3÷2*0'), 3) check(calc2('100+2+3÷5*12'), 109.2)