Appendix L — Assignments
Table of contents
- M Assignment 1
- M.1 Question 1: Convert a string to a float
- M.2 Question 2: Convert a string to an int
- M.3 Question 3: Find the remainder of division
- M.4 Question 4: Find whole and decimal portion of a number
- M.5 Question 5: Find the type of a value and convert to string
- M.6 Question 6: Join two lists
- M.7 Question 7: Insert items into a dictionary
- M.8 Question 8: Remove a value from a list
- M.9 Question 9: Extract a sub-list from a list
- M.10 Question 10: Scan a list and process all the values
M Assignment 1
The following 10 questions should be answered by placing 10 function definitions in the same .py
file named A1.py
. They will be marked by importing each function from the file, running the function on a variety of random inputs, and checking for correctness. Each function must be named as A1Q1
, A1Q2
, and so on, where A1
refers to assignment 1, and Q1
refers to question 1.
To help you develop your answers, I have created a web-based app using Streamlit (the same tool you’ll use for the project later in this term). This app can be downloaded as a .py
here. The best way to get this file is to “right-click” on the link then select “save link as”
To use this app follow these steps:
- Open the
conda prompt
from the Anaconda Navigator interface - Install Streamlit by entering
pip install streamlit
at the prompt (Note you only need to do this once and it will be present for all future assignments) - Put the grader app and your
A1.py
file in the same directory (e.g.C:\documents\UW\NE111
) - Return to the
conda prompt
and change to the directory you chose in step 3. You can do this by enteringcd <path to directory>
at the prompt. - Run the app by entering
streamlit run A1_grader.py
. This will open a new tab in your web browser, and hopefully the usage is self-explanatory.
M.1 Question 1: Convert a string to a float
Write a function that accepts a string representation of any number and converts it to a float. E.g. ‘33’ -> 33.0
M.2 Question 2: Convert a string to an int
Write a function that accepts a string representation of any number and converts it to an int. E.g. ‘33.65’ -> 33
M.3 Question 3: Find the remainder of division
Write a function that returns the remainder of division between any two numbers. E.g. 4, 3 -> 1 or 3.4, 1.1 -> 0.1
M.4 Question 4: Find whole and decimal portion of a number
Write a function that returns the decimal and whole parts of a number as separate values. E.g. 1.3 -> 0.3, 1
M.5 Question 5: Find the type of a value and convert to string
Write a function that returns the type of a value and returns the result as a string. E.g. 1.0 -> "<class 'float'>"
M.6 Question 6: Join two lists
Write a function that accepts two lists and joins them into a single list, then returns the result. E.g. [1, 2, 3], [4, 5, 6] -> [1, 2, 3, 4, 5, 6]
M.7 Question 7: Insert items into a dictionary
Write a function that accepts a dictionary, a key and a value, then inserts the value into the location given by key, then returns the updated dictionary. E.g. d, k, v -> returns d, and print(d[k]) displays v.
M.8 Question 8: Remove a value from a list
Write a function that accepts a list and a value, deletes the first instance of that value from the list, and returns the updated list. E.g. [1, 2, 1, 3], 1 -> [2, 1, 3]
M.9 Question 9: Extract a sub-list from a list
Write a function that accepts a list and returns a list containing the first half of the values, rounding down if the list is an odd number of elements long. E.g. [1, 2, 3, 4, 5] -> [1, 2]
M.10 Question 10: Scan a list and process all the values
Write a function which accepts a list containing only numerical values, then returns the difference between the minimum and maximum values it contains. E.g. [1, 2, 3, 4, 5] -> 4