Skip to main content

Homework 10

Skills: 7, 8

Due

Sunday, Nov 29, Anywhere-on-earth

Submission

This HW, like all homeworks, is done on https://github.com and submitted on https://app.pawtograder.com. Go to Pawtograder to find the repository where the assignment should be done. Commits automatically make submissions.

While there may be automated feedback for parts of the "Programming Practice" section, ALL OF YOUR GRADE IS DETERMINED BY THE ASSIGNMENT CHAT THAT OCCURS DURING THE LAB FOLLOWING THE DEADLINE (see Assignment Chats).


Project

Project 3: A Text-based, Data Powered Tool in Python

Your third (and last) open-ended course projects begins this week. Like other homework, projects will be graded by assignment chats during lab (see Assignment Chats). Since projects are large, each week there will be checkpoints due, which are a part of the project.

This project gives you the following task:

Find a dataset that can be used to build an interactive tool -- i.e., one that prompts the user, and based on their input, provides a response using the data in question. Likely, this will mean providing some sort of search/filter on the data. The tool will be written in Python, using Pandas for data processing.

Project 3, Checkpoint 1, due this week, is:

  • Identify a dataset that you would like to use, and figure out what the interaction will be. You may want to explore the dataset a bit (either clicking around via whatever online data hub you are using, or using Pyret). You do not need to write any Python this week, but should write in English what your plan is for your tool.

Programming Practice

Problem 1

The weather is cooling down in Boston, but over the summer, you noticed a lot of kids selling lemonade at little stands. You noticed two that were next to each other that had different pricing and different materials. You were curious which strategy would work better, so in this problem you will model both stands.

Part A

The first stand was getting 16 fluid ounce bottles of lemonade from a shop right around the corner, for $2/each, and was selling 4 fluid ounce cups for $1/each. For simplicity, we'll ignore the cost of the cups, refridgeration, etc, and you can assume the corner shop never runs out of 16 fluid ounce bottles.

Design a function lemonade_stand_1 that takes a number of cups sold as input and returns the total profit. Be sure to take into account that the 16 fluid ounce bottles cannot be purchased in part, so there may be extra left over! Hint: the python operator % (which calculates the remainder of division) may be useful.

Part B

The second stand decided to buy the lemonade in bulk, getting a gigantic 5 gallon (640 fluid ounce) container for $20. To try to undercut the other stand, they decided to sell their 4 fluid ounce cups for $0.75 each. But, since they bought their lemonade from a store that is far from home, when they run out, they will stop being able to sell lemonade.

Design a function lemonade_stand_2 that takes a number of cups (attempted to be) sold as input and returns the total profit. Note that this input may be any positive integer, so if it exceeds their capacity, the function should simply return their profit before they closed the stand. Note that if they sell the entirety of their lemonade, there is no penalty for being unable to sell additional cups. As before, we aren't considering cost of cups, refridgeration, etc.

Part C

Now, you'd like to design a function which_stand_better that, given a number of cups sold, returns the number 1 if lemonade stand 1 would make more money at that number of cups, or 2 if lemonade stand 2 would make more money with that number of cups sold. If they are tied, return 0.

Problem 2

Translate your tick function from Homework 2 into Python.

Problem 3

Design a function add_prefix that takes a string (the prefix) and a list of strings and returns a list of strings where each element in the input has had the prefix added in front of it.

Problem 4

Design a function remove_odd_length_strings that takes a list of strings and returns a list that only includes those in the input whose length is even (evenly divisible by 2). len(str) returns the length of a string in Python, and n % m is the remainder operator.

Problem 5

Currently, this program prints 10. We'd like you to add one line that includes the global keyword to the program (do not change or remove any existing lines, and you may only add that one line) that causes it to print 50 instead.

x = 10

def f(x : int) -> int:
""" the f function """
x = x + 20
return x

def g(y : int) -> None:
""" the g function """
x = f(y)

g(30)

print(x)