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.

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