首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
辅导Python编程、Python程序调试、讲解data编程 讲解留学生Prolog|讲解R语言编程
项目预算:
开发周期:
发布时间:
要求地区:
Assignment
Python
Rename the HW3SampleTests.py file as HW3Tests.py and add your own test cases in this file. You
are expected to add one more test case for each problem. Choose test inputs different than those
provided in the assignment prompt.
o Near the top of your program write a debug function that can be turned on and off by changing a
single variable. For example,
debugging = True
def debug(*s):
if debugging:
print(*s)
o Where you want to produce debugging output use:
debug(”This is my debugging output”,x,y)
instead of print.
(How it works: Using * in front of the parameter of a function means that a variable number of arguments can be
passed to that parameter. Then using *s as print’s argument passes along those arguments to print.)
Problems:
1. (Dictionaries)
a) organizebyFlavor(feedingLog) – 15%
Consider the cat feeding log data you used in HW1 and HW2 . We re-format the same data as a
Python dictionary where keys are the timestamps (month, year pairs) and values are the feeding
logs.
Assume, you would like to re-organize the data and create a dictionary that includes flavors as keys
and the timestamps and number of cans you fed to your cat as values. For example, when you
organize the above dictionary you will get the following:
Define a function organizebyFlavor that organizes the feeding log data as described above. Your
function should not hardcode the cat food flavors and timestamps.
(The items in the output dictionary can have arbitrary order.)
You can start with the following code:
def organizebyFlavor(feedingLog):
#write your code here
b) popularFlavor(feedingLog) – 15%
Assume, you would like to find your cat’s most popular canned food flavor, i.e., the flavor that has
the max total number of cans. Define a function “popularFlavor” that will return the popular
flavor and its total can count. For example:
popularFlavor(myCatsLog) returns ('Chicken', 37)
#i.e., chicken has the max number of total cans among all food flavors,
which is 37.
Your function definition should not use loops or recursion but use the Python map and reduce
functions. You can also use the organizebyFlavor function you defined in part(a). You may
define and call helper (or anonymous) functions, however your helper functions should not use
loops or recursion. If you are using reduce, make sure to import it from functools.
myCatsLog =
{(7,2020):{"Oceanfish":7, "Tuna":1, "Whitefish":3, "Chicken":4, "Beef":2},
(8,2020):{"Oceanfish":6, "Tuna":2, "Whitefish":1, "Salmon":3, "Chicken":6},
(9,2020):{"Tuna":3, "Whitefish":3, "Salmon":2, "Chicken":5, "Beef":2, "Turkey":1, "Sardines":1},
(10,2020):{"Whitefish":5, "Sardines":3, "Chicken":7, "Beef":3},
(11,2020):{"Oceanfish":3, "Tuna":2, "Whitefish":2, "Salmon":2, "Chicken":4, "Beef":2, "Turkey":1},
(12,2020):{"Tuna":2, "Whitefish":2, "Salmon":2, "Chicken":4, "Beef":2, "Turkey":4, "Sardines":1},
(1,2021):{"Chicken":7,"Beef":3, "Turkey":4, "Whitefish":1, "Sardines":2} }
{'Beef': {(7,2020): 2, (9,2020): 2, (10,2020): 3, (11,2020): 2, (12,2020): 2, (1,2021): 3},
'Chicken': {(7,2020): 4, (8,2020): 6, (9,2020): 5, (10, 2020): 7,(11, 2020): 4,(12,2020): 4,(1,2021):7},
'Oceanfish': {(7,2020): 7, (8,2020): 6, (11,2020): 3},
'Salmon': {(8, 2020): 3, (9, 2020): 2, (11, 2020): 2, (12, 2020): 2},
'Sardines': {(9, 2020): 1, (10, 2020): 3, (12, 2020): 1, (1, 2021): 2},
'Tuna': {(7, 2020): 1, (8, 2020): 2, (9, 2020): 3, (11, 2020): 2, (12, 2020): 2},
'Turkey': {(9, 2020): 1, (11, 2020): 1, (12, 2020): 4, (1, 2021): 4},
'Whitefish': {(7,2020):3, (8,2020):1, (9,2020):3, (10,2020):5, (11,2020):2, (12,2020): 2, (1,2021): 1}}
2. (Lists) unzip(L) – 15%
Write a function unzip that calculates the reverse of the zip operation. unzip takes a list of 3-
tuples as input and returns a tuple of lists, where each list includes the first, second, or third
elements from input tuples, respectively. Give a solution using higher order functions (map,
reduce or filter), without using loops.
For example:
unzip ([(1,"a",10),(2,"b",11),(3,"c",12),(4,"d",13)])
returns
([1, 2, 3, 4], ['a', 'b', 'c', 'd'], [10, 11, 12, 13])
You can start with the following code:
def unzip(L):
#write your code here
3. findCycle(graph,start)– 15%
Consider the following directed graph where each node has at most one outgoing edge. Assume the
graph nodes are assigned unique labels. The edges of this graph can be represented as a Python
dictionary where the keys are the starting nodes of the edges and the values are the ending nodes.
Note that some nodes in the graph are halting nodes, i.e., they don’t have any outgoing edges.
Those nodes are marked with double lines in the graph.
{'A':'B','B':'D','C':'G','D':'E','E':'C','F':'I','G':'B','H':None,'I':'H'}
Write a recursive function, findCycle, which takes a graph dictionary and a starting node as input
and returns the sequence of nodes that form a cycle in the graph. It returns the first cycle that exists
in the path beginning at node “start”. The function returns the list of the cycle node labels where
the starting node of the cycle should be included both in the beginning and at the end. If the graph
doesn’t have any cycles, it returns None.
You may define helper functions to implement your solution. Either your findCycle function or
your helper should be recursive.
For example:
graph = {'A':'B','B':'D','C':'G','D':'E','E':'C','F':'I','G':'B','H':None,'I':'H'}
findCycle(graph,'A') returns ['B', 'D', 'E', 'C', 'G', 'B']
findCycle(graph,'F') returns None
A B C D E F G H I
You can start with the following code:
def findCycle(graph,start):
#write your code here
4. Generators
getNextNode(graph,start) – 10%
Write a generator function, getNextNode, which will generate an iterator for the given graph
dictionary -- similar to the one we defined in problem-4. The generator will be initialized with the graph
dictionary and the starting node label, and it will return the sequence of the node labels in the graph
one at a time.
For example:
graph= {'A':'B','B':'D','C':'G','D':'E','E':'C','F':'I','G':'B','H':None,'I':'H'}
graph_nodes = getNextNode(graph,'A')
graph_nodes.__next__() # returns 'A'
graph_nodes.__next__() # returns 'B'
L = []
for i in range(0,15):
L.append(graph_nodes.__next__())
print(L) #L is ['D','E','C','G','B','D','E','C','G','B','D','E','C','G','B']
You can start with the following code:
def getNextNode(graph,start):
#write your code here
5. Iterators
DecodeIter()– 25%
Create an iterator that represents the decoded sequence of words from an encoded string. The iterator
will be initialized with a dictionary “ttable” (for decoding characters) and an input string (“input”).
When the iterator’s __next__() method is called, it will retrieve the next word from the input,
decode it using the ttable, and return it. The iterator should stop when it reaches the end of the input
string. Decoding a word involves looking up the characters in the ttable and replacing them with
retrieved values. If a character can’t be found in the dictionary, it should be included as it is.
Note that your iterator should not split the complete string input into words and then return those
words one at a time. It should retrieve the next word from the string only when the __next__() call is
made. The “input” can be an iterator representing an infinite sequence of characters. (See the second
test for DecodeIter in HW3SampleTests.py.
ttable = {'C':'A', 'D':'B', 'E':'C', 'F':'D', 'G':'E', 'H':'F', 'I':'G', 'J':'H', 'K'
:'I', 'L':'J', 'M':'K', 'N':'L', 'O':'M', 'P':'N', 'Q':'O', 'R':'P', 'S':'Q', 'T':'R'
, 'U':'S', 'V':'T', 'W':'U', 'X':'V', 'Y':'W', 'Z':'X', '[':'Y', '\\':'Z', 'c':'a', '
d':'b', 'e':'c', 'f':'d', 'g':'e', 'h':'f', 'i':'g', 'j':'h', 'k':'i', 'l':'j', 'm':'
k', 'n':'l', 'o':'m', 'p':'n', 'q':'o', 'r':'p', 's':'q', 't':'r', 'u':'s', 'v':'t',
'w':'u', 'x':'v', 'y':'w', 'z':'x', '{':'y', '|':'z', '2':'0', '3':'1', '4':'2', '5':
'3', '6':'4', '7':'5', '8':'6', '9':'7', ':':'8', ';':'9'}
strInput = "R{vjqp ku cp gcu{ vq ngctp, rqygthwn rtqitcookpi ncpiwcig. Kv jcu ghhkekg
pv jkij-ngxgn fcvc uvtwevwtgu cpf c ukorng dwv ghhgevkxg crrtqcej vq qdlgev qtkgpvgfrtqitcookpi.
R{vjqp'u gngicpv u{pvcz cpf f{pcoke v{rkpi, vqigvjgt ykvj kvu kpvgtrtgvg
f pcvwtg, ocmg kv cp kfgcn ncpiwcig hqt uetkrvkpi cpf tcrkf crrnkecvkqp fgxgnqrogpv k
p ocp{ ctgcu qp oquv rncvhqtou."
For example:
'R{vjqp' is decoded to 'Python'
' jkij-ngxgn' is decoded to 'high-level'
words = DecodeIter(ttable,strInput)
words.__next__() # returns 'Python'
words.__next__() # returns 'is'
words.__next__() # returns 'an'
words.__next__() # returns 'easy'
L = []
for w in words:
L.append(w)
if w == 'language.': break
# L is ['to', 'learn,', 'powerful', 'programming', 'language.']
You can start with the following code:
class DecodeIter ():
# write your code here
Testing your functions (5%)
We will be using the unittest Python testing framework in this assignment. See
https://docs.python.org/3/library/unittest.html for additional documentation.
The file HW3SampleTests.py provides some sample test cases comparing the actual output with the
expected (correct) output for some problems. This file imports the HW3 module (HW3.py file) which
will include your implementations of the given problems.
Rename the HW3SampleTests.py file as HW3Tests.py and add your own test cases in this file. You
are expected to add at least one more test case for each problem. Make sure that your test inputs cover
all boundary cases. Choose test input different than those provided in the assignment prompt.
In Python unittest framework, each test function has a “test_” prefix. To run all tests, execute the
following command on the command line.
python -m unittest HW3SampleTests
You can run tests with more detail (higher verbosity) by passing in the -v flag:
python -m unittest -v HW3SampleTests
If you don’t add new test cases you will be deduced at least 5% in this homework.
In this assignment, we simply write some unit tests to verify and validate the functions. If you would like
to execute the code, you need to write the code for the "main" program. Unlike in C or Java, this is not
done by writing a function with a special name. Instead the following idiom is used. This code is to be
written at the left margin of your input file (or at the same level as the def lines if you've indented
those.
if __name__ == '__main__':
...code to do whatever you want done...
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代做ceng0013 design of a pro...
2024-11-13
代做mech4880 refrigeration a...
2024-11-13
代做mcd1350: media studies a...
2024-11-13
代写fint b338f (autumn 2024)...
2024-11-13
代做engd3000 design of tunab...
2024-11-13
代做n1611 financial economet...
2024-11-13
代做econ 2331: economic and ...
2024-11-13
代做cs770/870 assignment 8代...
2024-11-13
代写amath 481/581 autumn qua...
2024-11-13
代做ccc8013 the process of s...
2024-11-13
代写csit040 – modern comput...
2024-11-13
代写econ 2070: introduc2on t...
2024-11-13
代写cct260, project 2 person...
2024-11-13
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!