# Week 4: Problem Set ### Cohort Session --- ### Homework 1. Lists and loops: One may use an approximate formula to quickly converting Fahrenheit (F) to Celsius (C): C ≈ C˜ = (F − 30)/2. Write three functions, one named f_to_c that returns the exact value in degree Celsius given a temperature in degree Fahrenheit; the second named f_to_c_approx that returns the approximate value in degree Celsius given a temperature in degree Fahrenheit; and the third named get_conversion_table to build a table conversion (i.e. a nested list). You should use your f_to_c and f_to_c_approx functions in your get_conversion_table function. a. Use the get_conversion_table() function you have written to generate a conversion table for values of degree Fahrenheit. You need to convert for each of the following values: F=0, 10, 20, · · · , 100. The conversion table should be in the form of [[F, C, CApprox], [F, C, CApprox], · · · ], where F is the temperature in degree Fahrenheit; C is the corresponding temperature in degree Celsius to 1 decimal place, and CApprox is the approximate temperature in degree Celsius to 1 decimal place. ``` def f_to_c(f): return (f - 32) * (5 / 9) def f_to_c_approx(f): return (f - 30) / 2 def get_conversion_table_a(): # create a range of Fahrenheit values you want to convert # initialize an empty list to be returned # go through each Fahrenheit value in the range # use function to convert Fahrenheit to Celsius in 1 d.p # use function to approximately convert Fahrenheit to Celsius in 1 d.p # add the values of Fahrenheit, Celsius, Celsius_approx to temporary list # add the temporary list to the initialized list that is to be returned # return previously initialized list ``` b. Use get_conversion_table() function you have written to generate a conversion table for values of degree Fahrenheit. You need to do this for each of the following values: F=0, 10, 20, · · · , 100. The conversion table should be in the form of [[F, F, · · · ], [C, C, · · · ], [CApprox, CApprox, · · · ]]. ``` def f_to_c(f): return (f - 32) * (5 / 9) def f_to_c_approx(f): return (f - 30) / 2 def get_conversion_table_b(): # create a range of Fahrenheit values you want to convert # initialize 3 empty lists for the values of Fahrenheit, Celsius and Celsius_approx to be returned # go through each Fahrenheit value in the range # add the current value of Fahrenheit to the Fahrenheit list # add the current rounded value of Celsius to the Celsius list # add the current rounded value of Celsius_approx to the Celsius_approx list # return a list consisting the Fahrenheit list, Celsius list, Celsius_approx list ``` 2. Write a function named max_list which takes a **two-level nested list of integers, inlist, as an input**, and **outputs a list, outlist**, such that outlist[i] is the **maximum** of all numbers in inlist[i]. You can assume that inlist and all nested lists are never empty. ``` def max_list(inlist): # initialize an empty storage list to be returned # go through each sublist in inlist # initialize a variable to store the default maximum value to be compared # go through each item in a sublist # compare if item is larger than the maximum variable; if yes, make the item the maximum variable # add the maximum variable for the current sublist to the empty storage list # return the empty storage list that should contain the maximum values of each sublist ``` 3. Write a Python function named multiplication_table that takes a value n and returns an n by n multiplication table. For instance, if n is seven, your program will return a table as follows. ![](https://i.imgur.com/2BaXJOx.png) The first element of the nested list should be a list that represents the first row of the table, the second element represents the second row and so on. For `n < 1`, your function should return the value `None`. ``` def multiplication_table(n): # check if n is < 1; if yes, return None # initialize an empty temporary list to be returned # create a list of numbers from 1 to n (inclusive) # go through each number on the list # initialize a second empty temporary list for the products of one row to be appended to the list above # go through each number on the list again # multiply the outer loop number to the inner loop number # add the product to the second empty list # add the second empty list to the list that is to be returned # return the first empty list ``` 4. Dictionary: Write a function named most_frequent that takes in a list of integers and returns a list of the ones that have the most occurrences. If more than one number have the most number of occurrences, all of them should be reported. You can assume that the input list is never empty. You should also not sort the list beforehand. For example: ![](https://i.imgur.com/MpTmPyC.png) ##### Note: The following code is where the bird meme comes into play ``` def most_frequent(lst): # initialize an empty list (mode) to be returned # initialize an empty dictionary (to store the number's occurence count; key: number, value: number of times it appeared in lst) # initialize an empty list (to store unique values of lst) # [First:] Identify the unique numbers through a list, count its occurences and store it in a dictionary # go through each item in the input list, called lst # if item is in the unique value list, add one to the value where key is equal to item # else, add the item to the unique list, and add a key, i, with value 0 # [Next:] Find the largest number of occurence in the dictionary # initialize an integer, mode = 0 # go through the items in the dictionary e.g. for key, value in dict.items(): # if value >= mode, the value becomes the new mode (mode = value) # [Next:] Find the keys that has the mode as the value # go through the items in the dictionary e.g. for key, value in dict.items(): # if value is equal to mode, add the key to the list that is to be returned # return the list with the mode(s) ``` ![](https://i.imgur.com/Qy0TZaq.jpg) 5. Dictionary: A polynomial can be represented by a dictionary. Write a function diff for differentiating such polynomials. diff takes a polynomial as a dictionary argument and returns the dictionary representation of its the derivative. If p denotes the polynomial as a dictionary and dp a dictionary representing its derivative, we have |dp[j-1] = j∗p[j] for j running over all keys in p, except when j equals 0. Here is an example of the use of diff: ![](https://i.imgur.com/XAzO5BC.png) In the above example, the dictionary `p={0: -3, 3: 2, 5: -1}` means that the 0th coefficient is -3, the 3rd coefficient is 2, and the 5th coefficient is -1. This can be written as: ![](https://i.imgur.com/j4l67Er.png) ``` def diff(p): # [Note] keys in the dictionary are the power, values are the coefficients # initialize an empty dictionary that will be returned # get the list of keys from the input dictionary, called p e.g. dict.keys() returns a list of keys from the dictionary # [proceed to differentiate] # go through every key in the keys list # if key is 0, skip one iteration of the loop using 'continue' (power is 0, means it disappears after differentation) # new value: (power * coefficient) is the same as (key * p[key]) # new key: (power - 1) is the same as (key - 1) # add the new key and new value to the empty dictionary # return the dictionary ```