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 1
Write the code for this problem in a file named p1_Lastname_Firstname.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 __str__,__repr__ 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 str.__getitem__; 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 __eq__,__ne__ that compare an Mstring with another Mstring, or with a str.
- method
replace(s, 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 -1 otherwise
- method
find
that works like str.find
Make sure your methods throw the same type of exception as equivalent methods in the str class (as
applicable).
Examples:
ms1= Mstring(Hello World)
print(ms1)
# prints: Hello World
print(ms1[3])
# prints: l
print(ms1[-3])
# prints: r
print(ms1[20])
# throws IndexError
print(len(ms1))
# prints 11
ms2= Mstring(abcdef)
print(ms2+
xyz)
# addition with regular string: prints abcdef
xyz. Caution: + must return
Mstring
print(0123+ ms2)
# addition with regular string: prints 0123abcdef. Caution: + must return
Mstring
print(ms2+ Mstring(1234))
# prints abcded1234. Caution: + must return Mstring
ms2[2]=X
print(ms2)
# prints abXdef
ms2[7]=Y
throws index error
ms2[-7]=Y
throws index error
ms2[3]=ZZ
throws ValueError since we cant insert a multi-character string with []=
Mstring(abcd)== Mstring(abcd)
should return true
Mstring(abcd)== Mstring(abcX)
should return false
ms3= Mstring(01234567)
print(ms3.find(45))
# prints 4
print(ms3.find(abc))
# prints -1
print(ms3.replace(34,XYZABC))
# prints 3
print(ms3)
# prints 012XYZABC567
ms4= Mstring(01234567)
print(ms4.replace(AB,XYZ))
# prints -1
print(ms4)
# prints 01234567
Important implementation details:
A
void re-computing 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
unit_tests
that uses the
testif
() function (at the end of this file) with unit tests
for the following Mstring methods: __eq__,__ne__,__str__,__len__,__add__,__radd__,
__setitem__,__getitem__,
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
test_sort
that tests the function from part c) on some representative strings,
including the empty Mstring.
e) write a function called
main
that runs
unit_tests
and
test_sort

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!