QUIZZES¶

Quiz 1/20/26¶

  1. What is the result of each of the following computations:

     1/2**2*2
     int(1.99)
  2. What possible values (in decimal notation) can a 3-bit integer represent? What if it was unsigned?

  1. Concisely explain what numbers can be represented by a float in python.
  1. What is wrong with the following code and how do you fix it?

     x = input("what is the square root of 9?")    
     print(x == 3) # gives false even for right answer

Homework 1/20/26 (due 1/27/26)¶

  1. (exercise from slides 03) Rewrite your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.

  2. (exercise from slides 03) Rewrite your pay program using try and except so that your program handles non-numeric input gracefully.

  1. Make a function that will takes a string as input and prints a message stating whether string contains an integer, a float, or neither. Demonstrate it works for the following example inputs.

    Example usage:

     myfunction("3")    # outputs 'integer'
     myfunction("3.14") # outputs 'float'
     myfunction("3.0")  # outputs 'float' (or int optionally)
     myfunction("hi")   # outputs 'just a string'
  2. Make a function with the following requirements (using only the python functionality we have covered thus far in class):

    • accepts multiple numbers (entered one at a time) as inputs, prompting for the next number after you enter each one.
    • Have it check whether each input is a valid number.
    • when you enter nothing as an input (just hitting return after typing nothing) it stops prompting for more
    • have it compute the mean of the inputs entered
    • have it print a message if the numbers have zero-mean (within appropriate numerical precision)
    • have it return the mean value

Quiz 1/27/26¶

Create functions that do each of the following using only conditional expressions and basic python:

  1. Compute $\frac{A+B}{A*B}$ for two inputs $A$ and $B$

  2. Print the min, max, and median of 3 inputs (FYI: there is no built-in median function)

  3. Accept an input and print whether it is a valid int

Make sure your functions handle bad input cases

Homework 1/27/26 (due 2/3/26 at 10:00 AM)¶

  1. Create a function that will print the min, max, and median of 3 inputs using only base python logic, no built-in functions or packages, and show it works with an example.

  2. Create a function that takes three inputs and returns True if exactly two of them are positive numbers (but not three) and show it works.

  3. Create a function that implements the following function and show it works by computing on several values: $$ f(x) = \begin{cases} x + 1 & x < -1 \\ 0 & -1 \le x < 1 \\ x - 1 & x \ge 1 \end{cases} $$

  4. Put your functions in an external .py file and demonstrate you can import it.

Quiz 2/3/26¶

  1. Create a python function that implements the following function (note: sin is in the math library):

    $$ f(\theta) = \begin{cases} sin(\theta), & -\frac{\pi}{2} \le \theta < \frac{\pi}{2} \\ 1, & \theta > \frac{\pi}{2} \\ -1, & \theta < -\frac{\pi}{2} \end{cases} $$
  1. Make a python function that computes $ y = x^k$ for inputs $x$ and $k$, which defaults to computing $y = x^2$ when $k$ is not given.

Homework 2/3/26 (due 2/10/26 at 10:00 AM)¶

  1. Make an efficient function using base python and the math library that simultaneously computes and prints the following norms for a list of floats, and demonstrate your function works with the given examples:

    1. 0-norm (number of nonzero elements) - 0-norm of [-1.5, 2, 0, -3] should be 3.
    2. 1-norm (sum of absolute values of elements), - 1-norm of [-1.5, 2, 0, -3] should be 6.5.
    3. 2-norm (square root of sum of squared elements)- 2-norm of [-1.5, 2, 0, -3] should be 3.905...
    4. infinity-norm (max absolute value of element) - infinity-norm of [-1.5, 2, 0, -3] should be 3.

  2. Update your function to check if each an element is a "?" wildcard, and if so, ignore it in the norm calculations, behaving like a zero.

    1. 0-norm of [-1.5, 2, "?", -3] should be 3.
    2. 1-norm of [-1.5, 2, "?", -3] should be 6.5.
    3. 2-norm of [-1.5, 2, "?", -3] should be 3.905...
    4. infinity-norm of [-1.5, 2, "?", -3] should be 3.

Quiz 2/10/26¶

Make a function that does the following:

  • prints numbers from 1 to 100.
  • If the number is divisible by 3, print "divisible by 3"
  • If the number is divisible by 5, print "divisible by 5"
  • If the number is divisible by both 3 and 5, print "divisible by both 3 and 5", do not print the above also.

Homework 2/10/26 (due 2/17/26 at 10:00 AM)¶

Use only base python for these. As always, do your best to interpret and fulfill the requirements as written.

  1. Create a vmult(x, alpha) function that:
  • takes a list input x and a scalar input alpha, and returns a list z which has elements z[i] = alpha*x[i].
  1. Create a vdot(x,y) function that:
  • takes two list inputs x and y, and returns a scalar beta which is the sum of x[i]*y[i].
  1. Create a vadd(x,y) function that:
  • takes two list inputs x and y, and returns a list z which has elements z[i] = x[i]+y[i].
  • works for lists containing numbers or strings, or adding strings directly.
  • If the inputs are not the same length, partially adds them as much as possible starting from element zero.

Quiz 2/17/26¶

Given a list of filenames in the following format:

    fnames[0] = 'JohnSmith2015-Texas.txt' 
    fnames[1] = 'MaryJane2016-Mississippi.txt' 

Make a python script which extracts three lists from the list of filenames: a list of names, a list of years, and a list of states, all in the same order as in the filenames. So, for example:

names[0] = 'JohnSmith'
years[0] = '2015'
states[0] = 'Texas'

Hint: use mystring.split() and slicing

Quiz 3/3/26¶

Given a list of filenames in the following format:

    fnames[0] = 'JohnSmith2015-Texas.txt' 
    fnames[1] = 'MaryJane2016-Mississippi.txt' 

Make a python dictionary with keys as the state names, and values as the number of filenames for that state. For example the above would result in:

{Missisippi':1, 'Texas':1}

Homework 3/03/26 (due 3/17/26 at 10:00 AM)¶

1. Build upon your vector functions from 2/10 homework¶

(a) Make a vector class and show that you can call the functions from within the class.

(b) Create an __init__ dunder to support initialization and an __add__ dunder to allow addition with "+" using members of your vector class in vector addition. E.g.,

    v1 = Vector([1, 2, 3])
    v2 = Vector([4, 5, 6])
    w = v1+v2  # should be Vector([5, 7, 9])


2. Consider the following DNA sequence¶

    dna = """
    ATGACCATGATTACGGATTCACTGGCCGTCGTTTTACAACGTCGTGACTGGGAAAACCCTGGCGTTACCC
    AACTTAATCGCCTTGCAGCACATCCCCCTTTCGCCAGCTGGCGTAATAGCGAAGAGGCCCGCACCGATCG
    CCCTTCCCAACAGTTGCGCAGCCTGAATGGCGAATGGCGCTTTGCCTGGTTTACGACGTTGTAAAACGAC
    """

(a) Use a dictionary to build a histogram of the base pair frequencies.

(b) Advanced: for each base-pair, create a histogram of subsequent base pair frequencies, as in the frequency of the immediately-following base-pair given each base-pair

$$ P(\text{base_pair } | \text{ previous_base_pair}) $$

This yields a markov model of DNA, equivalent to a bigram model.

3. Process the "Alice in Wonderland" text. You can get it via:¶

    import urllib.request
    url = "https://www.gutenberg.org/files/11/11-0.txt"  # Alice in Wonderland
    data = urllib.request.urlopen(url).read().decode("utf-8", errors="ignore")

(a) create a histogram of word frequencies

(b) Sort the word frequencies in decreasing order and compare to Zipf's law, where the sorted rank is inversely proportional to the word frequency (2nd word is 1/2 as frequent as first, third word is 1/3 as frequent, etc.)

(c) Advanced: sort the words according to their frequencies and list the most commonly-used words. This is relatively tricky in base python.

Quiz 3/17/26¶

Suppose Python supports an "and" operation &, analogous to the + which can be supported for classes via dunders. Give python code to implement such an operation in a class consisting of two lists, where you produce a new list with the shared elements.

Homework 3/17/26 (due 3/26/26 at 10:00 AM)¶

1. sets¶

For the following test data:

genes_a = {"BRCA1", "TP53", "EGFR", "MYC", "PTEN", "KRAS"}
genes_b = {"TP53", "PTEN", "ALK", "KRAS", "BRAF", "PIK3CA"}

Use python sets to programmatically:

  1. Find the genes shared by genes_a and genes_b.
  2. Find all genes that appear in either genes_a or genes_b.
  3. Find the genes that are in genes_a but not in genes_b.
  4. Compute the Jaccard similarity between genes_a and genes_b.

2. regex¶

  1. create a random string representing DNA with one million base pairs. (each character is randomly chosen from ('A','T','C','G'). This might take awhile. Print the first 100 elements to show it worked.

  2. create a RNA string by replacing every 'T' with a 'U'. Print the first 100 elements to show it worked.

  3. Use regex functions to approximately estimate the probability of a start codon (ATG) in your DNA by counting how many are present.

  4. Use regex functions to approximately estimate the probability of a stop codon (one of TAA, TAG, or TGA) in your DNA. Show two different regex patterns that can work.

Quiz 3/24/26¶

  1. Suppose you are give a python string S. Use sets to extract the vowels (a,e,i,o,u) contained in S. Results can be in any order and data structure.

  2. Use regex to find the locations of the vowels in S. Result can be a simply list of indices.

Homework 3/24/26 (due 3/31/26 at 10:00 AM)¶

  1. Implement the edit_distance function from the slides and see if you can identify the backtrace from the D matrix. (try putting some trivial cases and seeing what happens).

  2. Choose an interesting corpus from nltk.corpus and perform the zipf's law activity from the slides (all 4 parts).

Quiz 3/31/26¶

  1. Compute the Hamming distance and the edit distance between "slide" and "slicer".

  2. Your goal is to create a spell correction module. Describe the steps for a simple algorithm to perform automatic spelling correction for a document consisting of a single string of characters. Use one sentence of plain language to describe each step, which should be a straightforward string-processing task. Suppose you only have access to basic python string processing libraries, and lots of test data.

Homework for 3/31 (due 4/14 at 10:00 AM)¶

https://www.keithdillon.com/classes/UMMC/BDS754-01/Numpy_practice.html

Quiz 4/7/26¶

Give answers for the following in python code:

  1. How do I create a 2x2 array containing values 1,2,3,4 using numpy?
  1. How do select the i,j element $A_{ij}$ of a matrix when represented as a 2D numpy array?

  2. suppose I have two very long python lists u and v. How do I use numpy to efficiently compute a new list w with elements $w_i = \sqrt{u_i+v_i}$ for all elements in u and v?

  3. Suppose I have a M-by-N numpy array. How do I crop off the edges (first and last row and column) using numpy to get a (M-2)-by-(N-2) array?

Quiz 4/14/26¶

  1. List the different ways you can multiply a matrix and vector in numpy (give code and math for each). Include all possible variations from either code or math perspective.
  1. Compute this product using broadcasting
$$\begin{bmatrix} 2 & -6 \\ -1 & 4\\ \end{bmatrix} * \begin{bmatrix} 2 \\ -1 \\ \end{bmatrix} = ?$$