Question
Python Programming. You will create a Python script to test your library module, it will demonstrate that each of the three constants and eight functions
Python Programming.
You will create a Python script to test your library module, it will demonstrate that each of the three constants and eight functions is implemented correctly.
a) It must contain one of the following import statements:
import lastname_firstname_lib5
from lastname_firstname_lib5 import *
b) It may contain additional import statements.
c) It may use any of the string methods listed in Section 4.7.1 of the Python Standard Library. For example, it may use the string method lower as a means of determining whether or not the value returned from function to_lower is correct.
d) The program will not perform any input operations (it will not call function input).
e) For each test case, the program will display the appropriate information so that someone reading the output can understand the purpose of the test and judge if the result is correct.
Here is my library:
ASCII_LOWERCASE = "abcdefghijklmnopqrstuvwxyz" ASCII_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" DECIMAL_DIGITS = "0123456789" #Takes one parameter (a string). It returns True if all of the # characters in the string are upper case or lower case ASCII letters # (it returns False otherwise). def is_alpha(str): for c in str: if (c not in ASCII_LOWERCASE) and (c not in ASCII_UPPERCASE): return False return True #Takes one parameter (a string). It returns True if all of the # characters in the string are ASCII decimal digits (it returns False otherwise). def is_digit(str): for c in str: if (c not in DECIMAL_DIGITS): return False return True #Takes one parameter (a string). It returns the string # which is a copy of the parameter, but where all of the upper case # ASCII letters have been converted to lower case ASCII letters. def to_lower(str): output = '' for i in range(len(str)): if str[i] in ASCII_UPPERCASE: output += ASCII_LOWERCASE[ASCII_UPPERCASE.index(str[i])] else: output += str[i] return output #Takes one parameter (a string). It returns the string # which is a copy of the parameter, but where all of the # lower case ASCII letters have been converted to upper case ASCII letters. def to_upper(str): output = '' for i in range(len(str)): if str[i] in ASCII_LOWERCASE: output += ASCII_UPPERCASE[ASCII_LOWERCASE.index(str[i])] else: output += str[i] return output #Takes two parameters (both strings), where the second parameter must be of length 1. # It returns the lowest index where the second parameter is found within the first parameter # (it returns -1 if the second parameter is not of length 1 or is not found within the first parameter). def find_chr(s1, s2): for i in range(len(s1)): if s1[i] == s2: return i return -1 #Takes two parameters (both strings). It returns the lowest index where the second parameter # is found within the first parameter (it returns -1 if the second parameter is not found within the first parameter). def find_str(s1, s2): l1 = len(s1) l2 = len(s2) for i in range(l1-l2+1): if s1[i:(i+l2)] == s2: return i return -1 #Takes three parameters (all strings), where the second and third parameters must be # of length 1. It returns the string which is a copy of the first parameter, but # where all occurrences of the second parameter have been replaced by the third parameter # (it returns the empty string if the second or third parameter are not of length 1). def replace_chr(s1, s2, s3): output = s1 index = find_chr(output, s2) while True: if index == -1: return output else: output = output[:index] + s3 + output[index+1:] index = find_chr(output, s2) #Takes three parameters (all strings). It returns the string which is a copy of the first # parameter, but where all occurrences of the second parameter have been replaced by the third parameter. # If there are no occurrences of the second parameter in the first, it returns a copy of the first parameter. # If the second parameter is the empty string, it returns the string which is a copy of the first parameter, # but with the third parameter inserted before the first character, between each character, and after the last character. def replace_str(s1, s2, s3): output = s1 index = find_str(output, s2) while True: if index == -1: return output else: output = output[:index] + s3 + output[index+len(s2):] index = find_str(output, s2)
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