Question
Assignment Description The assignment this week focuses on string methods, slices, working with files, and automated testing . Also, for the functions you are required
Assignment Description
The assignment this week focuses onstring methods, slices, working with files, and automated testing. Also, for the functions you are required to implement, you should assume that all functions will receive a correct type of input - a string.
Part 1:reverse(s: str) -> str
Write a function that takes a string as an argument and returns a new string which is the reverse of the argument. E.g.
reverse('abc') == 'cba' # This expression should be True
We saw in the lecture that'abc'[::-1]reverses the string. Youmustimplement your own solution to reverse the string.YouMAY NOTuse any of Python's built in reverse functions.
Part 2:substring(target: str, s: str) -> int
Write a substring function that is similar to Python's string.find(target) method that returns the offset from the beginning ofstring s where target occurs in s.Return -1 if target is not a substring in s.For example,
>>> substring("he", "hello") 0 >>> substring("ell", "hello") 1 >>> substring("xxx", "hello") -1
NOTE:Youmay notuse any Python string methods including string.find(), string.startswith(), string.index(), etc.
HINT: slices might be very helpful
Part 3:find_second(target: str, string: str) -> int
Thestr.find(substr)(Links to an external site.)
method finds the first occurrence of substr in string and returns the offset of where s begins in string. Write a functionfind_second(s1: str, s2: str)-> int that returns the offset of thesecondoccurrence of s1 in s2. Return -1if s1 does not occur twice in s2. E.g.find_second('iss','Mississippi') == 4. Careful, the second occurrence might be in the middle of the first occurrence, e.g.find_second('abba', 'abbabba') == 3.
Hint: Python's string.find method accepts an optional second argument that specifies the offset of where to start looking for target in string. E.g.'abaa'.find('a', 1)finds the first occurrence of'a'in'abaa'beginning at offset 1 rather than offset 0.
Part 4:get_lines(path: str) -> Iterator[str]
Write agenerator,get_lines(path: str) -> Iterator[str], that opens a file for reading and returns one line from the file at a time.get_lines(path)must:
- combine lines that end with a backslash (a continuation) with the subsequent line or lines until a line is found that does not end with a backslash
- get_lines(path)should remove all comments from the file where comments begin with a '#' anywhere on the line and continue until the end of the line
- Third, any line that begins with a comment should be ignored and not returned.
get_lines() should raise a FileNotFound exception if the file can't be opened for reading.
E.g. given a file:
# this entire line is a comment - don't include it in the output# this entire line is a comment - don't include it in the output # comment # this entire line is a comment - don't include it in the output # comment \ # more comment1 \ more comment2> # here's a comment line continued to the next line \ this line is part of the comment from the previous line
get_lines(path)should yield the following lines, one at a time:
Hints:
- Combine lines where line i ends with line i + 1 before removing comments.
- Be careful to remove trailing newline ( ) but not to remove leading and trailing blanks and tabs.string.strip() removes whitespace from both the beginning and end of a string.string.rstrip(' ') removes only ' ' from the end of the string.
- The backslash character (\) has special semantics.If you want to compare a string to the backslash character, then use eitherstring == '\\'orstring == r'\'
- Files may have blank lines.Be sure that your solution does not eliminate blank lines.
Here's my main() routine to demonstrate using the generator
def main() -> None: file_name = '/Users/jrr/Downloads/HW05/test1.txt' for line in get_lines(file_name): print(line)
Here's my test case for the file above
class GetLinesTest(unittest.TestCase): def test_get_lines(self): file_name = '/Users/jrr/Downloads/hw05.txt' expect: List[str] = ['' , '' , '' , '' ,'' , '' , '' ] result: List[str] = list(get_lines(file_name)) self.assertEqual(result, expect)
Part 5: Write human readable code
Be sure your code includes docstrings and follows the PEP-8 coding guidelines, e.g.CamelCase only for class names, appropriate spaces, etc.
Insure that all of your functions include type hints for parameters and return types.Be sure to include type hints for all variables.
Provide adequate unittest test cases to demonstrate that your functions work properly.
Separate your code into two files- one for code logic and one for unit test. It's always a good practice to separate the code and the test.
File1.pyshould define all of your functions.
File2.pyshould define all of your automated test cases.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started