Computers and Technology
Computers and Technology, 14.12.2021 01:20, tynan74

Building a String Library Sometimes when programming you need to introduce a new data type and develop a library of functions for manipulating instances of that data type. Often this is done as methods on a new class but not always.

In this assignment, you'll be defining a collection of functions on strings, most of which already exist as methods on the str class. However, you should pretend that they don't already exist and define them as functions. Of course, you can't just use the existing methods. This will give you a bunch of practice on manipulating strings and also on defining functions.

In this assignment, you'll be defining a collection of functions on strings. You should define each of these as a new function with a function header as below. The description of each is in the comment supplied. Recall that strings are immutable, so you will have to return a new copy of the string for those that call for you to return a string. If you are returning the same string as the input, just return it; there is no need to make a new copy, and it wouldn't anyway.

The only string functions/methods to use:

ord, chr
indexing and slicing
append (i. e., ``+'')
len, in, not in
equality comparison (== or !=))
Looping over strings (while or for loops) and selection (if statements) are allowed. BUT You cannot convert the strings to other types such as lists.

Define each of the following functions, with semantics as indicated by the comment. In each of the following ch is a single character, str, str1, str2 are strings, and i is a non-negative integer. You do not have to validate the inputs, except as indicated; you can assume these types.

At least one question asks you to return two values. That really means returning a tuple (pair) of values. You do that as follows:

return value1, value2
This actually returns the pair (value1, value2). The caller can then assign the members of the pair to two variables:

x, y = pairReturningFunction() # assumes function returns a pair
z, w = (value1, value2) # assigning a tuple to 2 variables
If you like, you can use earlier functions in later ones, or define helper functions, though it shouldn't really be necessary. Note that some of these are trivial to write, while others are a bit harder. I have done the first one for you.

def myAppend( str, ch ):
# Return a new string that is like str but with
# character ch added at the end
return str + ch

def myCount( str, ch ):
# Return the number of times character ch appears
# in str.

def myExtend( str1, str2 ):
# Return a new string that contains the elements of
# str1 followed by the elements of str2, in the same
# order they appear in str2.

def myMin( str ):
# Return the character in str with the lowest ASCII code.
# If str is empty, print "Empty string: no min value"
# and return None.

def myInsert( str, i, ch ):
# Return a new string like str except that ch has been
# inserted at the ith position. I. e., the string is now
# one character longer than before. Print "Invalid index" if
# i is greater than the length of str and return None.

def myPop( str, i ):
# Return two results:
# 1. a new string that is like str but with the ith
# element removed;
# 2. the value that was removed.
# Print "Invalid index" if i is greater than or
# equal to len(str), and return str unchanged and None

def myFind( str, ch ):
# Return the index of the first (leftmost) occurrence of
# ch in str, if any. Return -1 if ch does not occur in str.

def myRFind( str, ch ):
# Return the index of the last (rightmost) occurrence of
# ch in str, if any. Return -1 if ch does not occur in str.

def myRemove( str, ch ):
# Return a new string with the first occurrence of ch
# removed. If there is none, return str.

def myRemoveAll( str, ch ):
# Return a new string with all occurrences of ch.
# removed. If there are none, return str.

def myReverse( str ):
# Return a new string like str but with the characters
# in the reverse order.
Expected output:

>>> from MyStringFunctions import *
>>> s1 = "abcd"
>>> s2 = "efgh"
>>> myAppend( s1, "e" )
'abcde'
>>> myCount( s1, "e")
0
>>> myCount( s1, "a")
1
>>> myCount( "abcabc", "a")
2
>>> myExtend( s1, s2 )
'abcdefgh'
>>> myMin( "" )
Empty string: no min value # Note the None doesn't print
>>> myMin( "zqralm" )
'a'
>>> myMin( "Hello World!" )
' '
>>> myInsert( "abc", 0, "d")
'dabc'
>>> myInsert( "abc", 2, "d")
'abdc'
>>> myInsert( "abc", 4, "d")
Invalid index # Note the None doesn't print
>>> myPop( "abcd", 1 )
('acd', 'b')
>>> myPop( "abcd", 0 )
('bcd', 'a')
>>> myPop( "abcd", 5)
Invalid index
('abcd', None)
>>> myFind( "abcdabcd", "a")
0
>>> myFind( "abcdabcd", "c")
2
>>> myFind( "abcdabcd", "f")
-1
>>> myRFind("abcdabcd", "d")
7
>>> myRFind("abcdabcd", "e")
-1
>>> myRemove( "abcdabcd", "a")
'bcdabcd'
>>> myRemove( "abcdabcd", "x")
'abcdabcd'
>>> myRemove( "abcdabcd", "d")
'abcabcd'
>>> myRemoveAll("abcabcabca", "a")
'bcbcbc'
>>> myReverse( "abcd" )
'dcba'
>>> myReverse( "" )
''

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 19:30, bstine6678
When creating a presentation in libre office impress, where does the editing of slides take place? a. the slides panel b. the center panel c. the tasks panel, under the masters pages tab d. the tasks panel, under the layouts tab
Answers: 1
image
Computers and Technology, 23.06.2019 21:30, shadoris26
To move a file or folder in microsoft windows, you can click and hold down the left mouse button while moving your mouse pointer to the location you want the file or folder to be, which is also known as.
Answers: 3
image
Computers and Technology, 24.06.2019 10:00, ariyanadavid5989
1. which of these is not true about torsion bars? a. they can be used to adjust ride height b. they can be adjusted anytime since they don't affect alignment angles c. they attach between the frame and the lower control arm d. they twist to produce a spring effect
Answers: 1
image
Computers and Technology, 24.06.2019 15:30, jerry1496
If you want to delete an entire word at a time, which key should you press along with the backspace or delete key?
Answers: 1
Do you know the correct answer?
Building a String Library Sometimes when programming you need to introduce a new data type and de...

Questions in other subjects: