PS02#
DS256, Gettysburg College
Prof Eatai Roth
Due: Sep 19, 2025 5p
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.
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.
# Initialize Otter
import otter
grader = otter.Notebook("ps02.ipynb")
1. Write a function called only_lowercase that takes as input text (as a string) and returns the same text with all spaces and punctuation (just !, ?, ., ‘, , (that’s a comma)) removed and all remaining letters set to lowercase.
Here are two functions you’ll find helpful:
.lower()converts all the alphabetic characters into lowercase..replace('a', 'b')replaces all occurences of character ‘a’ with ‘b’. If the second character is an empty string ‘’, replace will just remove the first character.
Here are two other tools you may find helpful for this problem.
a = 'BaNaNa for SALE!'
print(a.lower())
print(a.replace('a', 'o')) # notice, the 'A' in 'SALE' was not replaced because 'A' doesn't match 'a'
banana for sale!
BoNoNo for SALE!
def only_lowercase(text):
'''your code here'''
# Is it doing what you expect it to do.
print(only_lowercase("Prof Roth can't wait for Thanksgiving!"))
Cell In[9], line 2
'''your code here'''
^
IndentationError: expected an indented block after function definition on line 1
grader.check("q1")
2. 1.
A palindrome is a word or phrase that reads the same forwards and backwards. Write a function named check_palindrome that takes as input a string and returns a boolean value as to whether the string is a palindrome. Palindrome’s generally ignore punctuation and spaces. Some examples:
‘kayak’
‘No lemons! No melon!’
‘Go hang a salami, I’m a lasagna hog.’
For this problem, you’ll be combining knowledge of lists and strings. A string can be treated as a list of characters and indexed/sliced just like a list. For example:
'hello'[:3] == 'hel'
'banana'[::2] == 'bnn'
First apply only_lowercase (the function you wrote above) to remove all spaces and punctuation from the text. Then check if the remaining characters read the same backwards as forwards.
def check_palindrome(text):
'''your code here'''
grader.check("q2")
3. Write a function transfer_funds that takes three inputs:
sendera list of numbersrecipienta list of numbersamounta number representing the amount to transfer
The sender and recipient lists represent account balances after a transaction. The last value in the list is the current amount in their account. The function should return the two lists:
if the sender has enough in their account, transfer the money and add an entry to the end of each list with the new balance.
if after the transaction, the sender has less than 50 remaining as their balance, print ‘Low funds.’
if the sender does not have enough in their account, print ‘Transaction declined: insufficient funds.’ and return the lists unchanged.
'''your code here'''
# If you get long trailing decimals, that's okay.
Prof = [100.60, 80.60]
bookie = [50.10, 60.25]
Prof_new, bookie_new = transfer_funds(Prof, bookie, 25.21)
print("Sender history:", Prof_new)
print("Recipient history:", bookie_new)
grader.check("q3")
4. Write a function unzip_list that takes as an input a list and returns two lists, one containing only the even-positioned entries and the other containing all the odd-positioned entries. For example:
my_list = ['A', 'B', 'C', 'D', 'E']
unzip_list(my_list)
should return
['A', 'C', 'E'], ['B', 'D']
'''your code here'''
grader.check("q4")
5. Write the function middle that takes as input a list. If the length of the list is odd, return the middle value in the list. Otherwise, return the first value (0-position) of the list.
Tip: A list will not accept a float as an index. To change a float to an int, use int(num). This will also truncate any fraction, rounding down to the nearest integer.
int(4.75)
'''your code here'''
grader.check("q5")
6. Write the function in_range that takes as input arguments a list (rng) and another number (num). The list contains two numbers defining a range (e.g. [0, 10]).
if the number is below the range, return -2
if the number is equal to the lower boundary value, return -1
if the number is in the range, return 0
if the number is equal to the higher boundary value, return 1
if the number is above the range, return 2
Check to make sure the numbers in the list are in order, and if not, reverse their order. If the list has the same two numbers (no range) and the other number is also equal to that number, return 0.
'''your code here'''
grader.check("q6")
7. Write the function half_and_half that takes as its input a list. If the list has an even number of items, check whether the first half of the list is the same as the second half and return True if it is. If the halves don’t match or the list is odd-length, return False.
'''your code here'''
grader.check("q7")
8. Write the function donate that takes as arguments two lists of strings (donor, recipient) and another string (item).
if the item is not in the
donorlist, print ‘Item not available` and return both lists unchangedif the the recipient already has that item, print ‘No thank you.’ and return both lists unchanged
if the
donorhas the item and therecipientdoes not, move the item from the donor to the recipient, print ‘Thank you. You are most welcome.’ and return the updated lists.
Hint: .remove(item) removes an item from a list by name (as opposed to .pop() which removes an item by index). The item must be in the list, or else you will get an error.
'''your code here'''
grader.check("q8")
Submission#
Make sure you have run all cells in your notebook in order before running the cell below. The cell below should commit and push your work to github.
Check github that your work was received.
!git add *
!git commit -m 'ps02 submitted'
!git push