PS03 (v0.1)

Contents

PS03 (v0.1)#

DS256, Gettysburg College

Prof Eatai Roth

Due: Sep 26, 2025 5p

The questions are in no particular order in terms of difficulty, so if you feel stuck, skip the question and come back to it.

You’ll need to run the cell below to install the otter-grader package. You’ll only need to do this once, but there’s no harm in running the cell multiple times.

Your Name: …

Collaborators: …

By submitting this work, you attest that:

  • it reflects your own effort and understanding of the material

  • you did not ask for help from students outside the course, other than PLAs

  • you did not use a generative AI to produce any code used below.

# Initialize Otter
import otter
grader = otter.Notebook("ps03.ipynb")

1. Write a function factorize() that takes as input an integer and returns as output a list of factors of that number. If the number is prime, also print “That’s prime.”

The function range(start, stop, step) creates a sequence of numbers as a list.

def factorize(num):
...
# Is it doing what you expect it to do.
print(factorize(1)) # should be [1]
print(factorize(57)) # should be [1, 3, 19, 57]
print(factorize(59)) # should print "That's prime." and return [1, 59]
grader.check("q1")

2.

Write the function check_cyclic_equality() that takes two lists and checks if there is a rotation that makes them same.

A single rotation/cycling is defined as taking the zero-th element of the list and moving it to the end. For example, [‘dog’, 7, ‘chicken’, ‘rabbit’, 9] cycled by 2 would be [‘chicken’, ‘rabbit’, 9, ‘dog’, 7].

If the lists are cyclically equal, return the smallest number the second list has to be cycled to equal the first list. If not, return False.

def check_cyclic_equality(lista, listb):
...
# Lists are cyclically equal with 2 rotations
assert check_cyclic_equality([1, 2, 3], [3, 1, 2]) == 1

# Lists are not cyclically equal
assert check_cyclic_equality([1, 2, 3], [4, 5, 6]) == False
grader.check("q2")

3. Write the function extract_vowels() that takes in a string of text and returns how many of each vowel (a,e,i,o,u) and the total number of vowels in the string.

Hints:

  • You can consider a string a list of characters and loop over the characters.

  • ‘A’ and ‘a’ are different characters. It might be helpful (but not necessary) to convert all letters to lowercase first using .lower() (demo below).

hi = 'Hello!'
for letter in hi:
    print(letter)
    
'Gettysburg College'.lower()
...

# Assert is a kind of test statement. If the statement is True (correct), assert does nothing.
assert extract_vowels("Gettysburg College") == [0, 3, 0, 1, 1, 5]
grader.check("q3")

4.

Write a function, find_filetype() which will take as input a list of filenames and a file extension starting with a ‘.’ (e.g. ‘.pdf’, ‘.xlsx’, ‘.ipynb’) and return the sublist of only those files with the specified filetype.

...

file_list = ['hi.pdf', 'hello.doc', 'ps01.ipynb', 'goodbye.pdf', 'test.ipynb']

find_filetype(file_list, '.ipynb')
grader.check("q4")

5. Write the function remove_duplicates() that takes as an input a list and removes any duplicates from the list, returning a list of only unique values in the order they appear in the original list.

Hint: As you build your list of unique values, you’ll be checking whether new values are already in that list.

...

assert remove_duplicates([9, 1, 1, 'Emergency']) == [9, 1, 'Emergency']
assert remove_duplicates('Hello there!') == ['H', 'e', 'l', 'o', ' ', 't', 'h', 'r', '!']
grader.check("q5")

6. (Optional challenge) Write the function ‘convert_to_numeric()’ that takes as input a list of prices. Some prices are listed as numbers, others as strings. Strings may appear with or without a decimal (but guaranteed that only one decimal per string) and possibly with a ‘$’ or ‘USD’). Return a list of prices as floats. If there is a non-numeric entry (e.g. ‘Out of Stock’), skip it.

Hint:

  • In class we checked the type of a variable as type(var)==int. The function isinstance(var, [types]) can check whether a variable is of several types.

isinstance(12.43, [int, float]) –> True

...
# Some tests
assert convert_to_numeric([12, '15.50', '$20', '30 USD', 'Out of Stock']) == [12.0, 15.5, 20.0, 30.0]
assert convert_to_numeric(['5', 7.25, '$8.00', 'Not available']) == [5.0, 7.25, 8.0]
grader.check("q6")

Submission#

When you are ready to submit, uncomment (cmd+/ or ctrl+/) and run the cell below.

# !git add *
# !git commit -m 'ps03 submitted'
# !git push -f