Advertisement
AquaBlitz11

Untitled

Jul 4th, 2021
1,199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.11 KB | None | 0 0
  1. """
  2. Instructions:
  3.  
  4. 1.  Look at the example database below.
  5.    Each person is represented by a dictionary that maps attribute names to their values.
  6.    For example, to get the first person's first name, you access via this expression:
  7.  
  8.        >>> example_db[0]['first_name']
  9.        'Thanadol'
  10.    
  11.    Test this on your interactive shell. (>>> means what you type. The line after means the result you should see.)
  12.  
  13. 2.  Try accessing non-existent attributes, like this:
  14.  
  15.        >>> example_db[0]['department']
  16.        Traceback (most recent call last):
  17.          ...
  18.        KeyError: 'department'
  19.    
  20.    Observe there is a KeyError. When solving problems, know that you can check whether a key exists by the `in` operator:
  21.  
  22.        >>> 'salary' in example_db[0]
  23.        True
  24.        >>> 'salary' in example_db[1]
  25.        False
  26.        >>> 'department' in example_db[0]
  27.        False
  28.    
  29.    This operator will be useful when solving the problems.
  30.  
  31. 3.  Now, let's look at the problems. There are four (4) problems for you to solve.
  32.    In each problem, you will receive the database (and any additional information) through function arguments.
  33.    Therefore, to test your solutions, you need to create an example database and pass it into the function.
  34.    (I do not recommend using `input` or `print` functions in this homework.)
  35.  
  36.    See the example test cases below. I've already created an example database called `example_db` for you.
  37.    If you wish to create a new `gunn_db` for your own testing, that's okay.
  38.    I think my test cases are sufficient for preliminary testing though.
  39.  
  40.    As with any problems we've seen so far, your solution should work for *any* possible database, not just the example.
  41.    I should be able to give you any database via the function argument, and your function should return the correct answer.
  42.  
  43.    Once you are done solving a problem, uncomment that problem's test cases and run the program.
  44.    If there's no error, that means the test cases are satisfied.
  45.  
  46.    Each problem is solvable in 10 lines or fewer. Good luck!
  47.  
  48. """
  49.  
  50. # see example database and test cases below
  51. # a database is a list of people
  52.  
  53. def count_people(db):
  54.     """
  55.    Given a database, return how many people there are in the database.
  56.    """
  57.  
  58.     # hint: should be a one-liner
  59.     return 0
  60.  
  61. def get_mean_salary(db):
  62.     """
  63.    Given a database, find the mean of people's salaries in the database.
  64.    Note that the salary field might be missing from some people. In that case, simply ignore that person.
  65.    """
  66.  
  67.     # hint: you want to find
  68.     # - the sum of the salaries (for people with salary information)
  69.     # - the number of people who have salary information
  70.     # this will require a loop with running sum and counter variables.
  71.     # then finally you just divide the sum by the number of people and return.
  72.     return 0
  73.  
  74. def find_people_with_last_name(db, last_name):
  75.     """
  76.    Given a database and the desired last name, return a list of people objects corresponding to the given last name.
  77.    (It's guaranteed that everyone in the database has name info.)
  78.    """
  79.  
  80.     # hint: start off with a new empty list. go through each person in the database;
  81.     # if they have the last name you want, just append that person (not just the name) to your empty list.
  82.     # return the list.
  83.     return []
  84.  
  85. def sort_by_birth_months(db):
  86.     """
  87.    Given a database, construct a new dictionary mapping birth month (1-12) to list of people's names with that birth month.
  88.    (It's guaranteed that everyone in the database has name and date of birth info.)
  89.    """
  90.  
  91.     # hint: start with a new dictionary like this:
  92.     birth_month_to_people = {
  93.         1: [],
  94.         2: [],
  95.         3: []
  96.         # ...
  97.     }
  98.  
  99.     # then, go through each person in the database:
  100.     # - get their full name by concatenating the attributes first_name and last_name
  101.     # - get their birth month by looking at date_of_birth, index 1
  102.     # then do something like:
  103.     # birth_month_of_people[found_birth_month].append(found_full_name)
  104.  
  105.     return birth_month_to_people
  106.  
  107. # -------------------------------
  108. # Test cases
  109. # -------------------------------
  110.  
  111. # example database
  112. example_db = [
  113.     {
  114.         'first_name': 'Thanadol',
  115.         'last_name': 'Chomphoochan',
  116.         'aliases': ['Pleng', 'Aqua'],
  117.         'date_of_birth': (2021, 11, 27),
  118.         'salary': 84000,
  119.         'position': 'Lecturer'
  120.     },
  121.     {
  122.         'first_name': 'Apichaya',
  123.         'last_name': 'Chomphoochan',
  124.         'aliases': ['Piano'],
  125.         'date_of_birth': (2023, 8, 21),
  126.         # salary missing
  127.         'position': 'Student'
  128.     },
  129.     {
  130.         'first_name': 'Siraphob',
  131.         'last_name': 'Wanavejkul',
  132.         'aliases': ['Gunn'],
  133.         'date_of_birth': (2000, 9, 3),
  134.         'salary': 65000,
  135.         'position': 'Affiliated Artist'
  136.     },
  137.     {
  138.         'first_name': 'Imaginary',
  139.         'last_name': 'Person',
  140.         'aliases': [],
  141.         'date_of_birth': (2000, 11, 3),
  142.         'salary': 50000,
  143.         'position': 'Filler'
  144.     },
  145. ]
  146.  
  147. # # problem 1
  148. # assert count_people(example_db) == 4
  149.  
  150. # # problem 2
  151. # assert get_mean_salary(example_db) == (84000+65000+50000)/3 # salary is missing for Apichaya Comphoochan
  152.  
  153. # # problem 3
  154. # assert find_people_with_last_name(example_db, 'Chomphoochan') == [
  155. #     {
  156. #         'first_name': 'Thanadol',
  157. #         'last_name': 'Chomphoochan',
  158. #         'aliases': ['Pleng', 'Aqua'],
  159. #         'date_of_birth': (2021, 11, 27),
  160. #         'salary': 84000,
  161. #         'position': 'Lecturer'
  162. #     },
  163. #     {
  164. #         'first_name': 'Apichaya',
  165. #         'last_name': 'Chomphoochan',
  166. #         'aliases': ['Piano'],
  167. #         'date_of_birth': (2023, 8, 21),
  168. #         # salary missing
  169. #         'position': 'Student'
  170. #     }
  171. # ]
  172.  
  173. # # problem 4
  174. # assert sort_by_birth_months(example_db) == {
  175. #     1: [],
  176. #     2: [],
  177. #     3: [],
  178. #     4: [],
  179. #     5: [],
  180. #     6: [],
  181. #     7: [],
  182. #     8: ['Apichaya Chomphoochan'],
  183. #     9: ['Siraphob Wanavejkul'],
  184. #     10: [],
  185. #     11: ['Thanadol Chomphoochan', 'Imaginary Person'],
  186. #     12: []
  187. # }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement