Objective: The purpose of this lab is for you to become familiar with Python’s b
ID: 3907236 • Letter: O
Question
Objective:
The purpose of this lab is for you to become familiar with Python’s built-in text container -- class str-- and lists containing multiple strings. One of the advantages of the str class is that, to the programmer, strings in your code may be treated in a manner that is similar to how numbers are treated. Just like ints, floats, a string object (i.e., variable) may be initialized with a literal value or the contents of another string object. String objects may also be added together (concatenation) and multiplied by an integer (replication). Strings may also be compared for “equality” and arranged into some order (e.g., alphabetical order, ordering by number of characters, etc.). Finally, strings may be placed in containers which can then be passed as arguments to functions for more complex manipulations.
Specifications:
Write an interactive Python program composed of several functions that manipulate strings in different ways. Your main() function prompts the user for a series of strings which are placed into a list container. The user should be able to input as many strings as they choose (i.e., a sentinel-controlled loop). Your main function will then pass this list of strings to a variety of functions for manipulation (see below).
The main logic of your program must be included within a loop that repeats until the user decides he/she does not want to continue processing lists of strings. The pseudo code for the body of your main() function might be something like this:
# Create the main function
def main():
# declare any necessary variable(s)
# // Loop: while the user wants to continue processing more lists of words
#
# // Loop: while the user want to enter more words (minimum of 8)
# // Prompt for, input and store a word (string) into a list
# // Pass the list of words to following functions, and perform the manipulations
# // to produce and return a new, modified, copy of the list.
# // NOTE: None of the following functions can change the list parameter it
# // receives – the manipulated items must be returned as a new list.
#
# // SortByIncreasingLength(…)
# // SortByDecreasingLength(…)
# // SortByTheMostVowels(…)
# // SortByTheLeastVowels(…)
# // CapitalizeEveryOtherCharacter(…)
# // ReverseWordOrdering(…)
# // FoldWordsOnMiddleOfList(…)
# // Display the contents of the modified lists of words
#
# // Ask if the user wants to process another list of words
Please help. Please and thank you.
Explanation / Answer
def SortByIncreasingLength(l):
for i in range(len(l)):
for j in range(i+1,len(l)):
if len(l[i]) > len(l[j]):
temp = l[i]
l[i] = l[j]
l[j] = temp
return l
def SortByDecreasingLength(l):
for i in range(len(l)):
for j in range(i+1,len(l)):
if len(l[i]) < len(l[j]):
temp = l[i]
l[i] = l[j]
l[j] = temp
return l
def countVowels(s):
s1 = "aeiouAEIOU"
count = 0
for i in range(len(s)):
if s[i] in s1:
count = count + 1
return count
def SortByMostVowels(l):
for i in range(len(l)):
count1 = countVowels(l[i])
for j in range(i+1,len(l)):
count2 = countVowels(l[j])
if count1 < count2:
temp = l[i]
l[i] = l[j]
l[j] = temp
return l
def SortByLeastVowels(l):
for i in range(len(l)):
count1 = countVowels(l[i])
for j in range(i+1,len(l)):
count2 = countVowels(l[j])
if count1 > count2:
temp = l[i]
l[i] = l[j]
l[j] = temp
return l
def CapitalizeEveryOtherCharacter(l):
str1 = ""
for i in range(len(l)):
if i%2 == 0:
str1 = str1 + l[i].lower()
else:
str1 = str1 + l[i].upper()
#print(str1)
return str1
def ReverseWordOrdering(l):
i = 0
j = len(l)-1
while(i!=j):
temp = l[i]
l[i] = l[j]
l[j] = temp
i = i +1
j = j-1
return l
list = []
while True:
inp = input("Enter a word (quit to stop):")
if inp == "quit":
break
list.append(inp)
while True:
print("1.Sort By Increasing Lenth of words")
print("2.Sort By Decreasing Lenth of words")
print("3.Sort By the most numnber of vowels")
print("4.Sort By the least numnber of vowels")
print("5.Capitalize every other character")
print("6.Revere word ordering")
print("7.Exit")
ch = input("Enter your choice:")
if (ch == "1"):
list1 = []
for i in range(len(list)):
list1.append(list[i])
print(list1)
print("After Sort By increasing Lenth of words")
list1 = SortByIncreasingLength(list1)
print(list1)
if (ch == "2"):
list1 = []
for i in range(len(list)):
list1.append(list[i])
print(list1)
print("After Sort By decreasing Lenth of words")
list1 = SortByDecreasingLength(list1)
print(list1)
if (ch == "3"):
list1 = []
for i in range(len(list)):
list1.append(list[i])
print(list1)
print("After Sort By most number of vowels")
list1 = SortByMostVowels(list1)
print(list1)
if (ch == "4"):
list1 = []
for i in range(len(list)):
list1.append(list[i])
print(list1)
print("After Sort By least number of vowels")
list1 = SortByLeastVowels(list1)
print(list1)
if (ch == "5"):
for i in range(len(list)):
print(i,list[i])
ind = int(input("Enter the index of the word to be capitalized:"))
print(list[ind])
str1 = CapitalizeEveryOtherCharacter(list[ind])
print("After capitalizing every other character")
print(str1)
if (ch == "6"):
for i in range(len(list)):
print(i,list[i])
print(list)
ReverseWordOrdering(list)
print("After reversing the ordering of words")
print(list)
if (ch == "7"):
break
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.