Appendix L — Assignments

Tip

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). You have to download the grader specific to each assignment.

To use this app follow these steps:

  1. Open the conda prompt from the Anaconda Navigator interface
  2. 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)
  3. Put the grader app and your A1.py file in the same directory (e.g. C:\documents\UW\NE111)
  4. Return to the conda prompt and change to the directory you chose in step 3. You can do this by entering cd <path to directory> at the prompt.
  5. 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.

L.1 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.

Tip

The Streamlit grader file for this assignment can be found here

Write a function that accepts a string representation of any number and converts it to a float. E.g. '33' \(\rightarrow\) 33.0

Write a function that accepts a string representation of any number and converts it to an int. E.g. '33.65' \(\rightarrow\) 33

Write a function that returns the remainder of division between any two numbers. E.g. (4, 3) -> 1 or (3.4, 1.1) \(\rightarrow\) 0.1

Write a function that returns the decimal and whole parts of a number as separate values. E.g. 1.3 \(\rightarrow\) (0.3, 1)

Write a function that returns the type of a value and returns the result as a string. E.g. 1.0 \(\rightarrow\) "<class 'float'>"

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]) \(\rightarrow\) [1, 2, 3, 4, 5, 6]

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) \(\rightarrow\) returns d, and print(d[k]) displays v.

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) \(\rightarrow\) [2, 1, 3]

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]) \(\rightarrow\) [1, 2]

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]) \(\rightarrow\) 4

L.2 Assignment 2

The following questions should be answered by placing function definitions in the same .py file named A2.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 AyQz where y refers to the assignment number (e.g. 2), and z refers to the question number (e.g. 3).

Tip

The Streamlit grader file for this assignment can be found here

Write a function that accepts three integer values, a, b and c, and returns True if a < b < c, and False otherwise. E.g (2, 3, 5) \(\rightarrow\) True; (4, 3, 6) \(\rightarrow\) False.

Write a function that accepts the temperature of water in degrees Celsius as an argument and returns True if the water will be liquid and False otherwise (assuming standard atmospheric pressure). Note that at 0 and 100 degrees multiple phases coexist.

Write a function that accept 2 values and the string representation of an arbitrary relational operator (e.g. ‘>’, ‘!=’), and execute the comparison. E.g. (1, 4, '==') \(\rightarrow\) False.

Write a function that uses a for loop to scan through a list and returns the number of even values in the list. Assume the list contains only numbers. E.g ([1, 2, 4, 11]) \(\rightarrow\) 2.

Write a function that uses the in keyword to determine if a given “key” is in a dictionary, and returns True or False accordingly. E.g ({'a': 1, 'b': 2}, 'a') \(\rightarrow\) True.

Write a function that accepts a single argument of any type, and returns True if that argument is a number and False otherwise. E.g. 1 \(\rightarrow\) True; True \(\rightarrow\) False.

Write a function that uses a for loop to determine whether or not a given list has any integer values in it. E.g. (['a', True, 20.0, 2]) \(\rightarrow\) True.

Write a function that accepts a list of numerical values and returns True if the list is ordered, and False otherwise. E.g. ([4, 3, 6]) \(\rightarrow\) False; (4, 9, 11) \(\rightarrow\) True.

L.3 Assignment 3

The following questions should be answered by placing function definitions in the same .py file named A3.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 AyQz where y refers to the assignment number (e.g. 3), and z refers to the question number (e.g. 3).

Tip

The Streamlit grader file for this assignment can be found here

Write a function that accepts a string and returns the number of letters it contains, upper or lower case. Eg. 'ab123C' \(\rightarrow\) 3.

Write a function that accepts a string and counts the number of characters in it, not including spaces. Eg. 'Mr. Bob Dobalina' \(\rightarrow\) 14

Write a function that validates a password by ensuring it contains at least 1 upper and lower case letter, number and the special character from the following: !@#$%^&. The function should return True for a valid password and False otherwise.

Write a function that checks an email address and returns True if it ends with a specified domain, where the desired domain is specified in the function call. E.g (bob@uwaterloo.ca, gmail.com) \(\rightarrow\) False, (bob@gmail.com, gmail.com) \(\rightarrow\) True

Write a function that accepts a list of strings, and returns a sub-list with strings that end in the specified file extension. E.g. (list_of_filenames, extension) \(\rightarrow\) sub_list.

Write a function that compares 2 strings a and b, and returns True if a is less than b. E.g. (a, b) \(\rightarrow\) True | False.

Write a function that accepts a list of strings and returns a single string with them all joined together. Eg. (["hello", "world"]) \(\rightarrow\) "helloworld"

Write a function which accepts a list of key-value pairs of the form "key:value" (as strings), separated by semicolons, and returns a dictionary containing the data. Keep the value as a string. E.g. "key1:value2;key2:value2" \(\rightarrow\) {'key1': 'value1', 'key2':'value2'}.

Write a function which encrypts and decrypts secret messages. The encryption scheme is such that it inserts a specified number of random letters between the real letters. For instance, ('test', 2) \(\rightarrow\) 'toierhsqwtww'. Include a mode argument so that the same function can both encrypt and decrypt a message. For example, the function definition should be def A3Q8(message, num, mode) where mode can be either "encrypt" or "decrypt", and num is the number of random letters between real letters.

L.4 Assignment 4

The following questions should be answered by placing function definitions in the same .py file named A4.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 AyQz where y refers to the assignment number (e.g. 4), and z refers to the question number (e.g. 2).

Tip

The Streamlit grader file for this assignment can be found here

To use this grader you also need to add the following files to the same directory as your code and the grader. Note that you can ‘right-click’ and select ‘save link as’ to download the files if your browser is opening it instead of downloading.

Write a function that accepts a file name, opens it, reads the contents, then returns the number of lines it contains. Assume the file is in the current working directory and is called 'file.txt'.

Write a function that accepts a file name, opens it, reads the contents, then counts the number of words it contains. Any escaped characters like \n and \t do not count as words. Assume the file is in the current working directory and is called 'file.txt'.

Write a function that accepts a filename, a string, and a number of repetitions, then writes the string to the file the given number of times, with each string on a new line. The function should return None. Store the file in the current working directory.

Write a function which accepts a filename and a dictionary containing columnar data and outputs it as a CSV file. Assume the computer running this function has pandas and openpyxl installed. Store the file in the current working directory. The user may or may not have included the file extension, so handle both cases. Assume the file is called 'data2.csv'.

Write a function which accepts the name of a CSV file, and returns the contents as a dictionary with each column in the CSV file as one entry in the dict. Assume the computer running this function has pandas and openpyxlinstalled. The user may or may not have included the file extension, so handle both cases. Assume the file is called 'data' (or 'data.csv').

Write a function which accepts a filename and a string, then appends the string to the end of the file, starting on a new line and preserving any data that may already be present in the file. Assume the file is called 'file.txt'.