Quiz01 Practice


Introduction

These are some practice exercises for you to prepare for Tuesday’s quiz! Based off your feedback (thank you!), we have made these practice problems similar to the difficulty level and content that you will see on the quiz. The practice solutions video will be released Saturday so keep an eye out for that. Please try these problems yourself before looking for solutions, as this will be the best way to practice for the quiz!

Please limit yourself to only using append and pop when dealing with lists, since that is how it will likely be on the quiz!

Setup Practice Directory

Open the course workspace in VS Code (File > Open Recent > comp110-workspace-…) and open the File Explorer pane. Expand exercises.

Navigate to your practice folder.

Right click and create a new file called quiz01.py.

Practice Questions

1. orderPrice

Write a function named orderPrice that takes in a list of strings that represent an order, a list of available menu items, and a list of corresponding float prices for each menu option and returns a price for the entire order. Any order that has an item not on the menu will be given a default price of $2, for each item not on the menu.

Functional Requirements * Have a function with the following signature * Name: orderPrice * Parameters: List[str] order, List[str] menu, List[float] prices * Return Type: float * You can assume both the menu and prices lists will have the same length, with each index corresponding to each other (menu[1] corresponds to prices[1])

Examples

>>> orderPrice(["Burger", "Fries", "Milkshake", "Salad"], [“Burger”, “Fries”, “Salad”], [7.50, 5.0, 9.50]) 
                >>> 24
                >>> orderPrice([“Milkshake”, "Pasta", “Taco”, "Taco", "Milkshake", “Fries”], ["Milkshake", "Taco", "Fries"], [4.50, 5.70, 5.0]) 
                >>> 27.40
                >>> orderPrice([ ], [ "Cookout Tray" ], [ "6.99" ]) 
                >>> 0

2. sortScores

Write a function named sortScores that takes in a list of unordered test scores and returns the list of scores in ascending order.

Functional Requirements * Have a function with the following signature * Name: sortScores * Parameters: List[int] scores * Return type: List[int] * You can assume all of the scores will be unique integer values (no repeats) * If the list is empty return an empty list [ ] * Hint: it may be helpful to write a helper function to find the min of a list

Examples

>>> sortScores([20, 67, 89, 35]) 
                >>> [20, 35, 67, 89]
                >>> sortScores([]) 
                >>> []

3. scoreStats

Write a function named scoreStats that takes in a list of unordered test scores and returns a tuple containing the median and mean score.

Functional Requirements * Have a function with the following signature: * Name: scoreStats * Parameters: List[int] scores * Return type: Tuple[int, int] - median, mean * You can assume all of the scores will be unique integer values (no repeats) * You must use your prior sortScores function within your code * If the list is empty return a tuple = (0, 0) * Hint: use the python round( ) function to convert your median and mean to int. It will also be useful in determining what is the middle value.

Examples

>>> scoreStats([90, 88, 52]) 
                >>> (88, 77)
                >>> scoreStats([]) 
                >>> (0, 0)

4. noDupes

Write a function named noDupes that takes in a list of integers and returns the list without any duplicate values, without changing the order.

Functional Requirements * Have a function with the following signature: * Name: noDupes * Parameters: List[int] duped * Return type: List[int] * If the list is empty return an empty list

Examples

>>> noDupes([1, 1, 2, 4, 7, 7]) 
                >>> [1, 2, 4, 7]
                >>> noDupes([3, 5, 8]) 
                >>> [3, 5, 8]
                >>> noDupes([]) 
                >>> []

5. multiplyTable

Write a function named multiplyTable that takes in an integer n, integer step, and integer end. It returns a list of the given integer n’s multiplication table from 1 to the end integer with a step size of step.

Functional Requirements * Have a function with the following signature: * Name: multiplyTable * Parameters: int num, int step, int end * Return Type: List[int] * Must use range function AND for-in loop * If step size OR end is 0, return an empty list

Examples

>>> multiplyTable(2, 2, 9) 
                >>> [2, 6, 10, 14, 18]

In this first example, we are finding the mult table for 2, starting at a factor of 1, going up to a factor of 9, with a step size between factors of 2. [(2 x 1), (2 x 3), (2 x 5), (2 x 7), (2 x 9)]

>>> multiplyTable(5, 1, 3) 
                >>> [5, 10, 15]
                >>> multiplyTable(3, 0, 2) 
                >>> []
                >>> multiplyTable(2, 1, 0)
                >>> []

Testing your program

Feel free to either use print( ) or write actual test cases to test each function! This is a great time to practice testing, and the cases under “Examples” are great use/edge cases to test for each function.

To use the pytest module:

  1. Create another file in the same practice folder called quiz01_test.py

  2. Import the definitions of the functions you just made at the top of the file (if you set this file before actually implementing the functions, go ahead and write the skeletons!)

  3. Create your test definitions, trying at least 2 use cases and 1 edge case (can use what is listed above or what’s more encouraged is writing your own, which will be great practice for the quiz!)

Style and Documentation Requirements

While you are not submitting anything for this, it is always good practice to write your meaningful doc strings and have proper style in all your variable names, keeping in mind things such as types, magic numbers, etc.

Solutions

Below is the link for the solutions! We also try to go through the process behind thinking through how to tackle the quiz overall, along with detailed step by step reasoning through each of the questions. Time stamps are in the description for each problem!

Click here