首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
辅导data程序、讲解program程序设计、Python编程语言调试 讲解留学生Processing|解析Haskell程序
项目预算:
开发周期:
发布时间:
要求地区:
Homework 8
Total Points: 100 (Correctness and Style)
Due Dates (SD time):
● Entire Assignment: Tuesday, December 1st, 11:59pm
● Checkpoint (read below): Sunday, November 29th, 11:59pm
Read ALL instructions carefully.
Starter Files
Download hw08.zip. Inside the archive, you will find starter files for the questions of
this homework. You cannot import anything to solve the problems.
Style Requirements
Please refer to the style guide on the course website.
Docstring and Doctest Requirements
● Each docstring is surrounded by triple double quotes (""") instead of triple
single quotes (''')
● Ensure that each of your functions has a well-formed docstring and that you
have at least 3 doctests of your own. Try to think of the cases that would
break your code rather than test it on easy to pass scenarios.
Testing
At any point of the homework, use the following command to test your work:
>>> python3 -m doctest hw08.py
For Windows users, please use py or python instead of python3.
Checkpoint Submission
Due Date: Sunday, November 29th, 11:59pm (SD time)
You can earn 5 points extra credit by submitting the checkpoint by the due date
above. In the checkpoint submission, you should complete Question 1 and submit
the hw08.py file to gradescope.
Checkpoint submission is graded by completion, which means you can get full
points if your code can pass some simple sanity check (no tests against edge
cases). Note that in your final submission, you should still submit these questions,
and you may modify your implementation if you noticed any errors.
Final Submission
Submit your work to gradescope, under the hw08 portal. You can submit multiple
times before the due date, but only the final submission will be graded.
Required Questions
1. DO NOT IMPORT ANY PACKAGES.
2. Please add your own doctests (at least one for the
class constructors, and at least three for the
remaining methods/functions) as the given doctests
are not sufficient. You will be graded on the doctests.
3. No assert statements are required for all questions. You
can assume that all arguments will be valid.
Question 1
In this question, you will implement a counter for iterable objects. A counter counts
and stores the occurrences of provided items, and it allows you to query the count
of a specific item in the counter.
All doctests for this question are in the function counter_doctests(). You need to
initialize 1 more instance of each class as constructor tests, and add 3 more
doctests for each method you implement.
Part 1: Counter
The Counter class abstracts a generalized counter for all kinds of iterable objects.
In the constructor (__init__), you need to initialize the following instance
attributes:
● nelems (int): The total number of items stored in the counter. Initialize it
with 0.
● counts (dict): A dictionary that stores all item (keys) to count (values)
pairs. Initialize it with an empty dictionary.
Then, implement the following 4 methods:
● size(self): Returns the total number of items stored in the counter.
● get_count(self, item): Returns the count of an object item. If the item
does not exist in the counter, return 0.
● get_all_counts(self): Returns a dictionary of all item to count pairs.
● add_items(self, items): Takes an iterable object (like list) of objects
(items) and adds them to the counter. Make sure to update both counts and
nelems attributes.
Note:
For the item argument and each object in the items argument, you can assume that
they can be used as a dictionary key. All immutable objects and a tuple of
immutable objects would qualify as valid dictionary keys. However, mutable objects
(such as list) are not valid keys, and you can assume they will not appear as items
to count. If you attempt to use a mutable object as a key of dictionary, the
following error will occur:
>>> counts[[1]]
Traceback (most recent call last):
File "
", line 1, in
TypeError: unhashable type: 'list'
Part 2: Alphanumeric Counter
In this part, we will build a counter for only Alphanumeric characters in strings.
When we need to count the characters in a string, we could further optimize the
counter in terms of time and space by replacing the dictionary with a counter list,
where each index represents an item, and the value at this index is the count of
this item.
To use a list as a counter, we need to figure out a way to convert characters to
integer indices, so that we could use the indices to query the list. Luckily, we
already have such a conversion rule available in our system. Since computers can
only understand numbers, each character is internally mapped to a non-negative
integer. The most basic conversion rule is ASCII, which provides mappings to
alphanumeric characters and some special characters. You can refer to the ASCII
table for the specific mapping.
In Python, we could perform the ASCII mapping through built-in functions ord() and
chr(). The ord() function takes a character and converts it to its integer
representation, and the chr() function does the opposite. For example:
After obtaining this mapping, we can start to build our counter list for the
alphanumeric characters. The list will be of length 10 + 26 + 26 = 62, which will
store digits (0-9), lowercase letters (a-z) and uppercase letters (A-Z). Specifically,
the mappings between each index and the character it represents are as follows:
To use this counter list (let’s call it counts), if we want to query the count of
character 'b', we first need to convert it to index 11, then retrieve counts[11].
For this class, you must use ord() and chr() for the conversion. You cannot
hardcode the entire mapping using list, dictionary, string, or if-elif-else statements.
>>> ord('0')
48
>>> chr(48)
'0'
>>> ord('A')
65
>>> chr(65)
'A'
>>> ord('a')
97
>>> chr(97)
'a'
index 0 1 ... 9 10 11 ... 35 36 37 ... 61
char '0' '1' ... '9' 'a' 'b' ... 'z' 'A' 'B' ... 'Z'
In the constructor (__init__), you need to initialize the following instance
attributes (you can reuse Counter’s constructor):
● nelems (int): The total number of items stored in the counter. Initialize it
with 0.
● counts (list): A counter list of length 62 as described above.
Since this counter performs a conversion between characters and indices to store
and query the count, you need to implement the following helper methods:
● get_index(self, item): Given an item, return its corresponding index (0-9
for digits, 10-35 for lowercase letters, 36-61 for uppercase letters). If the
item is not an alphanumeric character, return -1.
● get_char(self, index): Given an index (0-61), return the corresponding
character.
You can use the built-in functions str.isnumeric(), str.islower() and str.isupper() to
distinguish different kinds of characters.
Then, overwrite the following methods:
● get_count(self, item): Returns the count of a character item. If the item
does not exist in the counter, return 0.
● get_all_counts(self): Returns a dictionary of all item to count pairs. You
need to build the dictionary by iterating through the counts list and add all
pairs with non-zero counts to the new dictionary. The dictionary should have
characters as keys (instead of indices) and counts as values.
● add_items(self, items): Takes a string items and adds each character to
the counter. Note that you should not add non-alphanumeric characters to
this counter. Make sure to update both counts and nelems attributes.
Question 2
Write a recursive function that takes two sequences of numeric values (main and
sub), and calculate two kinds of sum of the main sequence:
(1)Sum of intersection: the sum of all numbers in the main sequence that also
appear in the sub sequence.
(2)Sum of differences: the sum of all numbers in the main sequence that do not
appear in the sub sequence.
This function should return a tuple of (sum_of_intersection, sum_of_difference), if
the main sequence is empty, return (0, 0).
You CANNOT use sum(), range(), or any loops/list comprehensions.
def find_two_sums_rec(main, sub):
"""
>>> main_seq = [0, 1, 1, 2, 3, 3, 4, 5, 5]
>>> find_two_sums_rec(main_seq, [])
(0, 24)
>>> find_two_sums_rec(main_seq, [1, 2])
(4, 20)
>>> find_two_sums_rec(main_seq, [3, 4, 5])
(20, 4)
"""
# YOUR CODE GOES HERE #
Question 3
Write a recursive function that takes a base string and a non-empty pattern string,
and computes the length of the largest substring of the base that starts and ends
with the pattern. If no match is found, return 0.
Notes:
(1) This function should look for exact matches (case-sensitive).
(2) The start pattern and end pattern of the largest substring could fully or
partially overlap (see examples below).
Examples:
def compute_max_string(base, pattern):
"""
>>> compute_max_string("jumpsjump", "jump")
9
>>> compute_max_string("hwhwhw", "hwh")
5
>>> compute_max_string("frontsdakonsakdna", "front")
5
>>> compute_max_string("life", "life")
4
"""
# YOUR CODE GOES HERE #
base pattern output
jumpsjump jump 9 (not overlapped)
hwhwhw hwh 5 (partially overlapped)
frontsdakonsakdna front 5 (fully overlapped)
life life 4 (fully overlapped)
Question 4 (Extra Credit)
Write a recursive function that takes a list of positive integers (nums, not empty)
and a positive integer target, and find whether it’s possible to pick a combination of
integers from nums, such that their sum equals the target. Return True if we can
pick such a combination of numbers from nums; otherwise, return False.
Hints:
(1) For each recursive call, you should only deal with one number in the list.
Think about how to approach these sub-problems with recursion: if we
include this number in the combination, can we reach the target sum? How
about excluding this number from the combination?
(2) Although we assume that the initial arguments are positive integers and the
initial nums list is not empty, you might observe that, at some point, a
recursive call will receive arguments that violate these assumptions (for
example, the nums list becomes empty). You might find it helpful to treat
these situations as base cases.
Examples:
def group_summation(nums, target):
"""
>>> group_summation([3, 34, 4, 12, 5, 2], 9)
True
>>> group_summation([1, 1, 1], 9)
False
>>> group_summation([1, 10, 9, 8], 17)
True
"""
# YOUR CODE GOES HERE #
nums target output
[3, 34, 4, 12, 5, 2] 9 True
[1, 1, 1] 9 False
[1, 10, 9, 8] 17 True
软件开发、广告设计客服
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-23:00
微信:codinghelp
热点项目
更多
代写math 1151, autumn 2024 w...
2024-11-14
代做comp4336/9336 mobile dat...
2024-11-14
代做eesa01 lab 2: weather an...
2024-11-14
代写comp1521 - 24t3 assignme...
2024-11-14
代写nbs8020 - dissertation s...
2024-11-14
代做fin b377f technical anal...
2024-11-14
代做ceic6714 mini design pro...
2024-11-14
代做introduction to computer...
2024-11-14
代做cs 353, fall 2024 introd...
2024-11-14
代做phy254 problem set #3 fa...
2024-11-14
代写n1569 financial risk man...
2024-11-14
代写csci-ua.0202 lab 3: enco...
2024-11-14
代写econ2226: chinese econom...
2024-11-14
热点标签
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
软件定制开发网!