If the version of your code on github does not run, it is considered non-functioning. If it does not run on your personal computer WITHOUT CHANGES, it is considered non-functioning.
WARNING: Graders will not grade any part of your code that does not execute.
TOTAL: 25 points
__init__
takes a string as a parameter__str__
passes all testsvowels()
passes all testsbothEnds()
passes all testsfixStart()
passes all testsasciiSum()
passes all testscipher()
passes all testsIn this lab, you will learn to:
Classes are the fundamental building blocks of Object Oriented data structures. One of the reasons data structures are so important is that they hide the complexity of a system.
In this lab, you will use a class to create an importable module for strings.
Before you create your module, you should write some tests to make sure it works
(generally, programmers write tests for their code before writing the code)
For this lab, you should have the following already in your main.py
file:
from stringutility import StringUtility
def main():
# create a list of StringUtility objects to use for testing
test_strings = ["interesting", "aardvark", "aaa", "aeiouAEIOU", "a b c d e f g h i j k l m n o p q r s t u v w x y z", '']
su = []
for i in test_strings:
su.append(StringUtility(i))
print("=========== Testing __str__ method... ===========")
expected_results = ["interesting", "aardvark", "aaa", "aeiouAEIOU", "a b c d e f g h i j k l m n o p q r s t u v w x y z", '']
i = 0
for s in su:
result = str(s)
print(f"{s}, got: {result}, expected: {expected_results[i]}")
assert(result == expected_results[i])
i += 1
print("=========== ...__str__ method passed ===========")
print("=========== Testing vowels method... ===========")
expected_results = ["4", "3", "3", "many", "many", "0"]
i = 0
for s in su:
result = s.vowels()
print(f"{s}, got: {result}, expected: {expected_results[i]}")
assert(result == expected_results[i])
i += 1
print("=========== ...vowels method passed ===========")
print("=========== Testing bothEnds method... ===========")
expected_results = ["inng", "aark", "aaaa", "aeOU", "a z", '']
i = 0
for s in su:
result = s.bothEnds()
print(f"{s}, got: {result}, expected: {expected_results[i]}")
assert(result == expected_results[i])
i += 1
print("=========== ...bothEnds method passed ===========")
print("=========== Testing fixStart method... ===========")
expected_results = ["interest*ng", "a*rdv*rk", "a**", "aeiouAEIOU", "a b c d e f g h i j k l m n o p q r s t u v w x y z", '']
i = 0
for s in su:
result = s.fixStart()
print(f"{s}, got: {result}, expected: {expected_results[i]}")
assert(result == expected_results[i])
i += 1
print("=========== ...fixStart method passed ===========")
print("=========== Testing asciiSum method... ===========")
expected_results = [1196,844,291,902,3647,0]
i = 0
for s in su:
result = s.asciiSum()
print(f"{s}, got: {result}, expected: {expected_results[i]}")
assert(result == expected_results[i])
i += 1
print("=========== ...asciiSum method passed ===========")
print("=========== Testing cipher method... ===========")
expected_results = ["tyepcpdetyr", "iizldizs", "ddd", "kosyeKOSYE", "z a b c d e f g h i j k l m n o p q r s t u v w x y", ""]
i = 0
for s in su:
result = s.cipher()
print(f"{s}, got: {result}, expected: {expected_results[i]}")
assert(result == expected_results[i])
i += 1
print("=========== ...cipher method passed ===========")
print("=========== Testing __str__ method (again)... ===========")
expected_results = ["interesting", "aardvark", "aaa", "aeiouAEIOU", "a b c d e f g h i j k l m n o p q r s t u v w x y z", '']
i = 0
for s in su:
result = str(s)
print(f"{s}, got: {result}, expected: {expected_results[i]}")
assert(result == expected_results[i])
i += 1
print("=========== ...__str__ method passed (again) ===========")
print("=========== Tests Complete! ===========")
main()
Remember, in Python, strings are immutable, meaning you cannot alter them after they have been defined. Once you create a string, “Hello”, that string can never change; however, you can manipulate existing strings to create new ones.
stringutility.py
.
StringUtility
to work with the tests given above.You should focus on passing one test at a time. Once you have passed a test, move on to the next failing test.
Since strings are just lists of characters, we can index into strings to access specific letters, just like we would index into a list. For example, if I had
mystring = "Hello"
I could access the first character with mystring[0]
.
print(mystring[0]) #prints 'H'
We can also index from the end of the string (instead of the beginning) by using negative numbers for an index. I could access the last character with mystring[-1]:
print(mystring[-1]) #prints 'o'
You can get the length of a string using the len()
function by passing the string as an argument
mystring = 'Hello'
size = len(mystring) #returns 5
Lastly, you can concatenate two or more strings with the + symbol.
mystring = "Hello" + " Goodbye"
print(mystring) #prints "Hello Goodbye"
Take a look at the driver code from above. Notice the first line:
from stringutility import StringUtility
The main.py
file will import a file called stringutility.py
.
You must create this file with the name stringutility.py
in the same folder as the main.py
and write your StringUtility
class in the imported file.
To begin, define the following methods for your StringUtility class:
def __init__(self, string)
def __str__(self)
:
You are welcome to use any additional string functions or features available in Python.
Once you are passing the first 2 tests, you may move on to part B.
Note that for this lab you are not writing ANY driver code, and should not have a main in the
stringutility.py
file.
Don’t forget to write docstrings for all methods other than ‘init’ (see Style Guide).
Add the following methods to your StringUtility class:
def vowels(self)
:
count
is less than 5, return the str representation of the number'3'
and "Incomprehensibilities" returns 'many'
def bothEnds(self)
:
def fixStart(self)
:
babble
yields ba**le
.def asciiSum(self)
:
int
that contains the sum of all ascii values in the string.def cipher(self)
:
Remember that you are writing the code that the
main.py
is using. The test code makes certain assumptions about your code (method names, parameters, class name, etc.), and you have to meet those assumptions.
Don't modify the provided test code.
Each of the 5 methods of the StringUtility
class share a notable feature:
They can all be completed in 1 line of code.
So in other words, you can write those methods like this:
def methodName(self):
return #your one line of code here
In order to do this, you will need to use some features of python we have not discussed. I am listing them below so you can review them if you choose.
main.py
Complete your submission by making a commit and push using either the Source Control tab in VSCode or the command line in the shell.
That's it! You've completed the work for this lab.