In: Computer Science
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
Deliverable(s):
Your deliverable should be a Word document with screenshots showing the sample code you have created, and discuss the issues that you had for this project related to AWS and/or Python IDE and how you solved them.
Submit the program you develop including captured output. Also turn in screen captures from running your program inputting, as a minimum, three (3) sets word lists (no fewer than 8 words per list).
Here is the grogram
def SortByIncreasingLength(wlist):
nlist = sorted(wlist, key=len)
return nlist
def SortByDecreasingLength(wlist):
nlist = sorted(wlist, key=len, reverse=True)
return nlist
def countVowels(s):
s1 = "aeiou"
count = 0
for c in s:
if c in s1:
count = count + 1
#print('in countvowel', s, count)
return count
def SortByTheMostVowels(wlist):
nlist = sorted(wlist, key=countVowels, reverse=True)
return nlist
def SortByTheLeastVowels(wlist):
nlist = sorted(wlist, key=countVowels)
return nlist
def CapitalizeEveryOtherCharacter(wlist):
new_wlist = []
for w in wlist:
str1 = ""
for i in range(len(w)):
if i%2 == 0:
str1 = str1 + w[i].lower()
else:
str1 = str1 + w[i].upper()
new_wlist.append(str1)
return new_wlist
def ReverseWordOrdering(wlist):
nlist = []
for i in range((len(wlist)-1), -1, -1):
nlist.append(wlist[i])
return nlist
def FoldWordsOnMiddleOfList(wlist):
return wlist
def main():
words = []
cnt = 0
while True:
aword = input("Enter minimum 8 words one ny one (quit to
stop):")
if aword == "quit":
if cnt < 8:
print("You entered ", cnt, "words only. Enter more")
else:
break
words.append(aword)
cnt = cnt+1
#words =["sleep","cat","egg","little"]
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.Fold word on middle of list")
print("8.Exit")
ch = input("Enter your choice:")
if (ch == "1"):
print(words)
print("After Sort By increasing Lenth of words")
new_words = SortByIncreasingLength(words)
print(new_words)
if (ch == "2"):
print(words)
print("After Sort By decreasing Lenth of words")
new_words = SortByDecreasingLength(words)
print(new_words)
if (ch == "3"):
print(words)
print("After Sort By most number of vowels")
new_words = SortByTheMostVowels(words)
print(new_words)
if (ch == "4"):
print(words)
print("After Sort By least number of vowels")
new_words = SortByTheLeastVowels(words)
print(new_words)
if (ch == "5"):
new_words = CapitalizeEveryOtherCharacter(words)
print("After capitalizing every other character")
print(new_words)
if (ch == "6"):
print(words)
new_words = ReverseWordOrdering(words)
print("After reversing the ordering of words")
print(new_words)
if (ch == "7"):
new_words = FoldWordsOnMiddleOfList(list)
print("After folding words in middle of list")
print(new_words)
break
if (ch == "8"):
break
main()
code as image:
Output :