Due: Wednesday November 17, 2021 at 9:00PM ET.
import pytest
at the top of your file!hw6_code.py
. Hand in your work on Gradescope.If you know how to solve any of these problems using techniques you've learned outside of 111, don't! Make sure to only use Python concepts we've talked about in class.
Create a new Python file called hw6_code.py
.
Task: Write import pytest
at the top of your hw6_code.py
file.
An election is happening in CS111's mission control. The team is trying to decide who should man their next mission to space, and Space Election Official Ryan needs help counting the votes.
For the following, you can assume a vote is just the name of a candidate (though capitalization might be inconsistent!)
Task: Write a function name_matches
that takes a vote
(string) and a name
(string) and returns a boolean indicating whether the vote is for the given name, regardless of how the vote and name have been capitalized (that is, ignore case).
Note: You can lowercase a string s
in Python by writing s.lower()
(where s
gets replaced by the string you want to lowercase). As in Pyret, you have Boolean operators and
, or
, and not
.
Task: Write a function any_votes_for
that takes a name
(string) and votes
(list of votes) and returns a boolean indicating whether any votes were for the given name (case insensitive).
Task: Write a function count_votes_for
that takes a name
(string) and a votes
, a list of votes, and returns the number of votes that were for the given name (case insensitive).
Task: Write a function got_more_votes
that takes two names, name_one
and name_two
, and votes
, a list of votes, and returns the name that got more votes (case insensitive). If there is a tie, return the string βTieβ
.
Task: Write a function record_vote
that takes in votes
, a list of votes, and new_vote
, a new vote, and adds the vote to the end of the list. This function should not return anything!
Task: Write a function percentage_votes
that takes a name and a list of votes and returns the percentage of votes that match (case insensitive) the given name. You should return the percent in decimal form (i.e. if the name matches 90% of the votes, return 0.9). Please round to the nearest tenth. Don't forget to consider the case where someone has no votes (or where there are no votes)!
You can round a number in Python using the round
function. The round
function has two arguments, the first being the number to round and the second representing how many decimal places to have. Here are a few examples:
Task: Write a function test_votes
to test your vote functions. In particular, think about how to test record_vote
. Run test_votes()
to make sure all your tests pass and your functions work as expected.
Note: Any time you are returning a name, it may be returned lowercased or uppercased. Our test suite is case insensitive, so returning "Kenny"
will be treated the same as "kenny"
and "KENNY"
. Same applies for functions that return lists. ["Ken","Kenny"]
will be treated the same as ["ken","KENNY"]
.
is
means in PythonNote: What is
means in Python, you can also use the keyword is
in lieu of ==
! is
checks whether two items are the same item in memory, not just if they have the same value. You can find an example here.
Here we have a lot of information to help with these questions. They'll be quite difficult to do without reading these first.
To do many of the following problems, you will need to do something called string slicing. String slicing is the Python equivalent of Pyret's string-substring
, but winds up being a lot more powerful.
Let's say you have the string str = "Beep!"
. Just like in Pyret, strings in Python are 0-indexed:
To access the characters at those indices, use the notation: str[index]
. For example,
If you want to slice a string into a substring of more than one character, use the notation: str[start:end]
where start
represents the first index of the substring (inclusive) and end
represents the last index of the substring (exclusive). For example,
That's all you need to know for this assignment, but Python has a few additional shortcuts for string slicing that might be useful in the long run:
If you leave off the start or end index, Python assumes you want to start at 0 and end at the end of the string:
Negative numbers let you index from the end of a string
Python has a useful and customizable sorting function that operates on lists. In this section, we will explore how to use it.
The simplest version of the function is sorted(lst: list)
, which takes in a list and sorts it in ascending order.
For example:
To sort a list in descending order, add the input reverse=True
to the function call. Here are the examples from above but sorted in reverse:
Note: Notation like reverse=True
is used for optional inputs to a function, a concept we did not see in Pyret. Since an optional input might not be provided, we need the <name>=
part to indicate which optional parameter is being used.
In most cases, default sorting is enough. However, what if you want to sort a list in a specific, custom way? To do so, add the input key=my_fun
where my_fun
represents a function that takes in a single list element. my_fun
is called on each element in the list, and its output determines the sort order.
Let's take a look at a concrete example. The following function takes in a string and returns its length:
With key_fun
defined, we can do the following to sort the list by string length:
(Note that since string length is an integer, sorted
defaults to sorting the elements in increasing order)
Just as the results of the election are about to go out, Space Election Official Ryan drops all of the ballots on the floor.
For all of the following functions, you can assume that all names have at least three letters. This means that none of your tests cases should have names less than three letters.
first_three_letters(name: str, votes: list)
that takes in a candidate name and all votes and returns a list of all votes for candidates with the same first three letters in their name as name
(regardless of capitalization).test_first_three
to test first_three_letters
.sort_a(votes: list)
that returns votes
sorted alphabetically by the first letter in candidate's names.test_a
to test sort_a
.sort_b(votes: list)
that returns votes
sorted alphabetically by the second letter in candidate's names. You can assume all strings in votes
will be at least two characters long.test_b
to test sort_b
.Weβve been discussing situations in which data can be collected (e.g., from your car) or interpreted (e.g., the meta-data discussion from lecture) in ways that can have consequences for entities about whom information is obtained. Breaking it down, impacts from data can arise from multiple dimensions, such as:
In addition, every scenario that involves data also has stakeholders, which are entities that can be impacted (positive or negatively) based on that data. For example, with a restaurant review application, stakeholders include diners, restaurant owners, and the local communities that might get more or less business based on reviews.
For this assignment, we want you to do a stakeholder and data impacts analysis around whichever dataset (CO2, bikeshare, grocery stores) you used for project 1. Specifically:
Review your collection of impacts: Which seem addressable by programmers? Which by policies or laws? Which are solely in the hands of users? The goal here is for you to start to develop some strategies for thinking about data impacts based on the different uses of data and potential interactions with programs.
Put your answers to these in a single multi-line string at the bottom of your code file, as shown below. Please include line breaks to help your graders.
Closing Time by Semisonic
Brown University CSCI 0111 (Fall 2021)
Do you have feedback? Fill out this form.