Question: This is the file name you would need testif.py Problem 1 Write the code for this problem in a file named p 1 _ Lastname
This is the file name you would need testif.py
Problem
Write the code for this problem in a file named pLastnameFirstname.py as required above.
a Implement in Python a class for a
mutable
string called
Mstring
Class Mstring must have the following interface:
a constructor that takes
any object
as argument and constructs a mutable string object
method len with their standard role; returns int
methods strrepr with their standard role; must return str
methods add and radd for concatenating two Mstring objects or an Mstring with a
str;
must
return Mstring
method getitem that works like strgetitem; must work also with a negative index; must
return str
method setitem that sets a character at a given index; must work also with a negative index;
throws IndexError if the index is invalid
methods eqne that compare an Mstring with another Mstring, or with a str
method
replaces t
that replaces the first occurrence of a substring
s
with another string t; it returns
the index of the substring
s
if
s
was found, or otherwise
method
find
that works like strfind
Make sure your methods throw the same type of exception as equivalent methods in the str class as
applicable
Examples:
ms MstringHello World
printms
# prints: Hello World
printms
# prints: l
printms
# prints: r
printms
# throws IndexError
printlenms
# prints
ms Mstringabcdef
printms
xyz
# addition with regular string: prints abcdef
xyz Caution: must return
Mstring
print ms
# addition with regular string: prints abcdef. Caution: must return
Mstring
printms Mstring
# prints abcded Caution: must return Mstring
msX
printms
# prints abXdef
msY
throws index error
msY
throws index error
msZZ
throws ValueError since we cant insert a multicharacter string with
Mstringabcd Mstringabcd
should return true
Mstringabcd MstringabcX
should return false
ms Mstring
printmsfind
# prints
printmsfindabc
# prints
printmsreplaceXYZABC
# prints
printms
# prints XYZABC
ms Mstring
printmsreplaceABXYZ
# prints
printms
# prints
Important implementation details:
A
void recomputing a
str
value unless the underlying characters have changed. Use caching.
Pick an internal representation for the characters that is efficient when using setitem and
getitem
b Write a function called
unittests
that uses the
testif
function at the end of this file with unit tests
for the following Mstring methods: eqnestrlenaddradd
setitemgetitem
replace
Make sure you include success and failure cases. Test also with the empty string object Mstring
Add a print statement for each test explaining what is tested.
c Write a function not a method! called
quicksort
that
sorts in place
the characters in an Mstring
object and that relies on Mstrings operators to read and write characters. Use the classical quicksort
algorithm, of courses.
d Write a function
testsort
that tests the function from part c on some representative strings,
including the empty Mstring.
e write a function called
main
that runs
unittests
and
testsort
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
