首页 > > 详细

讲解structure、辅导Python,Java,C++编程语言、CSV file辅导辅导留学生Prolog|辅导Python程序

0The following problems are about Piazza polls with file structure and data definitions adapted from the
exam preparation material. Changes are noted in bold italics below.
Information about Piazza questions is available in a CSV file in a format like the following:
Note: Compared to the prep material, the table has one extra row, Sanders's Email has changed,
and Warren's Choice has changed.
Each row has:
a Name value, the respondent's name (a non-empty string, which may not contain an @)
an Email value, the respondent's e-mail address (a non-empty string, which must contain an @;
if it begins with @ like @mochi, then the actual e-mail address that should be stored as
data is at tweetbook.com like mochi@tweetbook.com)
a timestamp (an integer greater than zero, where larger numbers mean later times)
a poll choice (which contains only letters, numbers, and spaces and must start with at least one
letter) or :NO VOTE: to indicate that the person intentionally made no choice at all
Note: a voter may select multiple choices and so appear in multiple rows, unless they selected
:NO VOTE:, in which case that must be their only row in the file.
We are interested in the name, e-mail address, and choice. We represent them with these data
definitions:
In [ ]: Choice = Optional[str] # NEW DATA DEFINITION
# interp. a poll choice containing only letters, numbers, and space
s and starting
# with a letter, or None to indicate intentionally making no choice
at all.
O0 = None
O1 = "unity"
O2 = "Est 1"
@typecheck # template based on optional
def fn_for_choice(c: Choice) -> ...:
if c is None:
return ...
else:
return ...(c)

Vote = NamedTuple('Vote', [('name', str),
('email', str),
('choice', Choice)]) # CHANGED TYPE
# interp. a poll response, with the respondent's name (non-empty wi
th no
# "@"), e-mail address (email, must have an "@" in it), and choice.
V1 = Vote("Joe Biden", "jb@gojoe.com", "Est 1")
V2 = Vote("Liz Warren", "ew@senate.gov", "unity")
# template based on compound (3 fields)
@typecheck
def fn_for_vote(v: Vote) -> ...:
return ...(v.name,
v.email,
v.choice)
# List[Vote]
# interp. a list of votes
LV1 = []
LV2 = [V1, V2]
# template based on arbitrary-sized and reference rule (Vote)
@typecheck
def fn_for_lov(lov: List[Vote]) -> ...:
# description of accumulator
acc = ... # type: ...

for v in lov:
acc = ...(acc, fn_for_vote(v))

return ...(acc)
Problem 1 (worth 1 mark)
We changed the type of the attribute choice in the Vote data type definition from str to Choice.
Neatly edit the code above to change everything else in the Vote data definition that should also change
as a result.
Problem 2 (worth 11 marks)
Now, neatly edit the read template below and add tests to complete a function design to read csv
files in this format into a List[Vote]. Complete all design steps, including fully designing any
required helper functions!
Be careful as the file's format and column ordering does not always nicely match our data definitions!
Assume the table above is available in a file named test_given.csv. If you need additional test files:
1. double-click this markdown cell,
2. add them as they would be in a csv file with triple-backquotes around them, and
3. indicate their file names.
For example, here is a blank test file you can also use:
File test_empty.csv contains:
Name,Email,Timestamp,Choice
Put any additional test files below:
In [ ]: @typecheck
def read(filename: str) -> List[Consumed]:
"""
reads information from the specified file and returns ...
"""
# return [] #stub
# Template from HtDAP
# loc contains the result so far
loc = [] # type: List[Consumed]
with open(filename) as csvfile:
reader = csv.reader(csvfile)
next(reader) # skip header line
for row in reader:
# you may not need to store all the rows, and you may n
eed
# to convert some of the strings to other types
c = Consumed(row[0], ... ,row[n])
loc.append(c)
return loc
start_testing()
expect(..., ...)
summary()
Problem 3 (worth 12 marks)
Now, complete the design of the count_voter_choices function below (tests, body, and any
helper functions not already available for your use). You may not change the provided signature,
purpose, stub, or how the function is templated. (I.e., you must use the List[Vote] template.)
You should assume the is_real_choice_vote function that is just a stub below is complete, correct,
and available for your use. DO NOT complete the is_real_choice_vote function as part of this
problem.
In [ ]: ## ASSUME that: is_real_choice_vote is CORRECT AND
## COMPLETE and available for your use.
##
## DO NOT complete these functions as part of the work for this pro
blem.
@typecheck
def is_real_choice_vote(v: Vote) -> bool:
"""
returns False (NOT True!) if v's choice is None and True otherw
ise (for 'real votes')
"""
return True #stub
## DO NOT COMPLETE ME
In [ ]: ## TODO: complete the design of count_voter_choices, without changi
ng the
## existing signature, purpose, or stub. Use the List[Vote] templat
e.
@typecheck
def count_voter_choices(lov: List[Vote], name: str, email: str) ->
int:
"""
returns the number of choices the voter with the given name and
e-mail address
selected. (Ignores voters that match one or the other but not b
oth.) If no such
voter exists or if the voter explicitly made no choice (None),
returns 0.

ASSUMES: No voter selects the same choice more than once. If a
voter chooses
None, they do so once and select no other choice.
"""
return 0 #stub
# template from List[Vote]
start_testing()
expect(..., ...)
summary()
Problem 4 (worth 8 marks)
Now, complete JUST THE BODY of the alternate implementation of the count_voter_choices
function below. It has the same signature, purpose, and stub as above. You may not change how the
function is templated. In particular, you must follow the composition plan provided. Do not supply
tests, since you already supplied them above.
You should assume the sum function that is just a stub below is complete, correct, and available for your
use. DO NOT complete the sum function as part of this problem.
Otherwise, where you need a helper function, design only its signature, purpose, and stub in the
same cell as sum and then assume like sum that it is complete and correct.
In [ ]: ## ASSUME that sum--and any functions whose signatures
## purposes, and stubs you add here--are CORRECT AND COMPLETE and
## available for your use.
## TODO: add here any signatures, purposes, and stubs for any helpe
r
## functions required by your get_pri_q_urls design below.
@typecheck
def sum(loi: List[int]) -> int:
"""
returns sum of the values in loi
"""
return 0 #stub
## DO NOT COMPLETE ME
In [ ]: ## TODO: complete the design of count_voter_choices, without changi
ng
## the existing signature, purpose, or stub. Use the provided
## composition plan.
##
## DO NOT FORGET to design signatures, purposes, and stubs for any
## new helpers in the cell above.
@typecheck
def count_voter_choices(lov: List[Vote], name: str, email: str) ->
int:
"""
returns the number of choices the voter with the given name and
e-mail address
selected. (Ignores voters that match one or the other but not b
oth). If no such
voter exists or if the voter explicitly made no choice (None),
returns 0.
ASSUMES: No voter selects the same choice more than once. If a
voter chooses
None, they do so once and select no other choice.
"""
return 0 #stub
# templated by composition
# Plan:
# 1) filter to only the votes matching name
# 2) filter to only the votes of those matching email
# 3) convert this to a list of "vote counts", where a vote coun
t is
# 1 for a normal choice, and 0 for None.
# 4) return the total of the values in the list

start_testing()
## DO NOT ADD TESTS. You should assume they are already complete.
summary()
Problem 5 (worth 8 marks)
Complete the design of the graphing function below. Ensure that the produced graph has a good title
and axis labels and remember to complete all remaining steps of the HtDF recipe including tests. Your
title must be of the form 'Votes for: ' followed by the name of the choice being plotted, e.g.,
'Votes for: unity' if the parameter c below had the value 'unity' or 'Votes for: Est 1' if c
had the value 'Est 1'.
DO NOT insert images into this file as we will not see them in grading. (Sorry! You have to describe
graphs in English rather than sketching them.)
You should assume the count_votes and get_first_half functions that are just stubs below are
complete, correct, and available for your use. DO NOT complete the count_votes and
get_first_half functions as part of this problem.
In [ ]: ## ASSUME that: count_votes and get_first_half are
## CORRECT AND COMPLETE and available for your use.
##
## DO NOT complete these functions as part of the work for this pro
blem.
@typecheck
def count_votes(lov: List[Vote], c: str) -> int:
"""
return the number of votes in lov for choice c
"""
return 0 #stub
## DO NOT COMPLETE ME
@typecheck
def get_first_half(lov: List[Vote]) -> List[Vote]:
"""
returns the first half of lov (rounded down if len(lov) is odd)
"""
return [] #stub
## DO NOT COMPLETE ME
In [ ]: ## TODO: complete the design of graph_votes_over_time, without chan
ging the
## existing signature, purpose, stub, or template comment.
@typecheck
def graph_votes_over_time(lov: List[Vote], c: str) -> None:
"""
Draws a line chart showing the number of votes for choice c as
the votes are totalled.
Specifically, the line has three points: one for x value 0 (0%
of the votes,
which always has y value 0), one for 50% of the votes (the coun
t for the first half
of lov; where 'half' is rounded down if the number of votes is
odd), and one for 100%
of the votes (the whole list).

The chart's title is 'Votes for: ' followed by c.
ASSUMES: lov is ordered by when the vote was cast.
For example, if there were five votes for: X, Y, Y, :NO VOTE:,
and Y, and we
plot choice Y, then: with 0% of the vote, Y receives 0 votes; w
ith 50% of the
vote (the first 2 votes X and Y, since there are 5 total), Y re
ceives 1 vote; and
with 100% (all 5 votes), Y receives 3 votes.
"""
return None #stub
# template from viz w/add'l parameter c

start_testing()
# DO NOT insert images into this file as we will not see them in gr
ading.
# You will have to describe the expected graph in English instead :
(
expect(..., ...)
summary()

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!