Question

In: Computer Science

The primary objective of this assignment is to reinforce the concepts of string processing. Part A:...

The primary objective of this assignment is to reinforce the concepts of string processing.


Part A: Even or Odd [10 marks]


For this first part of the assignment, write a function that will generate a random number within some range (say, 1 to 50), then prompt the user to answer if the number is even or odd. The user will then input a response and a message will be printed indicated whether it was correct or not. This process should be repeated 5 times, using a new random number for each play, regardless of whether the user was correct or not.


Notes


A correct response for an even number can be any of the following: “even”, “Even”, “EVEN”, “e”, “E”. “EiEiO”. The main criterion is that a correct response begins with the letter “e”, regardless of case. The same goes for an odd number – a correct response should begin with the letter “o”.


If the user enters an invalid response, they should be notified. However, an invalid response still counts as one of the 5 plays.


The solution should contain a loop (for or while) and a few if statements. An example execution is shown below:


>>> assign2PartA()
Is 41 odd or even? Odd
Correct
Is 48 odd or even? Even
Correct
Is 3 odd or even? e
Incorrect
Is 20 odd or even? o
Incorrect
Is 42 odd or even? xyz
You did not enter a correct reply.
Please answer using Odd or Even


Part B: Vowel Counting [10 marks]


The second task is to write a function that counts, and prints, the number of occurrences for each vowel (a, e, i, o and u) in a given phrase and outputs a (new) phrase with the vowels removed. The phrase should be accepted as a parameter to the function. If the (original) phrase is empty, you should output an appropriate error message, and obviously with no vowel counts. If the phrase contains no vowels, a message should be displayed, and the count can be omitted – since there is no point in displaying 0 for each vowel! A message should also be displayed if the phrase contains only vowels, but the counts should still be displayed.


Notes


Recall that you can use the tab escape character (“\t”) for spacing to align the text, or use a given width in the f-string (print(f"{count_a:4}, ……”), where the value of count_a here is right justified in 4 columns.)


Be sure to correctly handle both upper- and lower-case letters (e.g. both “a” and “A” should be counted as an instance of the letter “A”.) The new phrase (with vowels removed) should preserve the letter cases from the original phrase. Be sure to show output for all possible scenarios in your submitted output.


>>> assign2PartB("Remember that context here defines \"+\" as 'Concatenate'")
A E I O U
4 10 1 2 0


The original phrase is: Remember that context here defines "+" as 'Concatenate'
The phrase without vowels is: Rmmbr tht cntxt hr dfns "+" s 'Cnctnt'


>>> assign2PartB("bcdfghjklmnpqrstvwxyz")


The phrase contains no vowels: bcdfghjklmnpqrstvwxyz


>>> assign2PartB("aeiouAEIOU")
A E I O U
2 2 2 2 2


The phrase contains only vowels: aeiouAEIOU


>>> assign2PartB("")


The input phrase is empty!


>>>


using python

Solutions

Expert Solution

Code:

#importing module random
import random
#function named demo
def demo():
   #this loop iterates 5 times
   for i in range(0,5):
       #generating a random number
       n=random.randint(1,50)
       #taking input from user
       a=input("is "+str(n)+" even or odd: ")
       #selection statements based on given conditions
       if(n%2==0):
           #checks the first character of input string
           if(a[0]=="E" or a[0]=="e"):
               print("Correct")
           elif(a[0]=="O" or a[0]=="o"):
               print("Incorrect")
           else:
               print("You didnot enter a Correct reply")
       else:
           if(a[0]=="E" or a[0]=="e"):
               print("Incorrect")
           elif(a[0]=="O" or a[0]=="o"):
               print("Correct")
           else:
               print("You didnot enter a Correct reply")

#function named vowel
def vowelcounter(input1):
   #variables storing count of each vowel
   a=0
   e=0
   i=0
   o=0
   u=0
   #length of string input
   l=len(input1)
   #if string is empty
   if(a==""):
       print("The input phrase is empty!")
   #else
   else:
       #checking each character in the string and
       #incrementing corresponding vowel counter
       for j in input1:
           if(j=='a' or j=='A'):
               a=a+1
           if(j=='e' or j=='E'):
               e=e+1
           if(j=='i' or j=='I'):
               i=i+1
           if(j=='o' or j=='O'):
               o=o+1
           if(j=='u' or j=='U'):
               u=u+1
       #sum of all vowel counters
       s=a+e+i+o+u
       #if sum is equal to 0
       if(s==0):
           print("The phrase contains no vowels: "+input1)
       #if sum is equal to length
       elif(s==l):
           #printing count of each vowel
           print("A E I O U")
           print(str(a)+" "+str(e)+" "+str(i)+" "+str(o)+" "+str(u))
           print("The phrase contains only vowels:"+input1)
       #else
       else:
           #printing count of each vowel
           print("A E I O U")
           print(str(a)+" "+str(e)+" "+str(i)+" "+str(o)+" "+str(u))
           print("The original phrase is: "+input1)
           vowels = ('a', 'e', 'i', 'o', 'u','A','E','I','O','U')
           #removing vowels from input1
           for k in input1:
               if(k in vowels):
                   input1=input1.replace(k,"")
           #printing
           print("The phrase without vowels is: "+input1)
#main function
def main():
   #caling each function
   demo()
   vowelcounter("Remember that context here defines \"+\" as 'Concatenate'")
   vowelcounter("bcdfghjklmnpqrstvwxyz")
   vowelcounter("aeiouAEIOU")
   vowelcounter("")

if __name__ == '__main__':
   main()

Output:

Code Screenshot:

code snippet:

#importing module random 
import random
#function named demo 
def demo():
        #this loop iterates 5 times
        for i in range(0,5):
                #generating a random number
                n=random.randint(1,50)
                #taking input from user
                a=input("is "+str(n)+" even or odd: ")
                #selection statements based on given conditions
                if(n%2==0):
                        #checks the first character of input string
                        if(a[0]=="E" or a[0]=="e"):
                                print("Correct")
                        elif(a[0]=="O" or a[0]=="o"):
                                print("Incorrect")
                        else:
                                print("You didnot enter a Correct reply")
                else:
                        if(a[0]=="E" or a[0]=="e"):
                                print("Incorrect")
                        elif(a[0]=="O" or a[0]=="o"):
                                print("Correct")
                        else:
                                print("You didnot enter a Correct reply")

#function named vowel
def vowelcounter(input1):
        #variables storing count of each vowel
        a=0
        e=0
        i=0
        o=0
        u=0
        #length of string input
        l=len(input1)
        #if string is empty
        if(a==""):
                print("The input phrase is empty!")
        #else
        else:
                #checking each character in the string and
                #incrementing corresponding vowel counter
                for j in input1:
                        if(j=='a' or j=='A'):
                                a=a+1
                        if(j=='e' or j=='E'):
                                e=e+1
                        if(j=='i' or j=='I'):
                                i=i+1
                        if(j=='o' or j=='O'):
                                o=o+1
                        if(j=='u' or j=='U'):
                                u=u+1
                #sum of all vowel counters
                s=a+e+i+o+u
                #if sum is equal to 0
                if(s==0):
                        print("The phrase contains no vowels: "+input1)
                #if sum is equal to length
                elif(s==l):
                        #printing count of each vowel
                        print("A E I O U")
                        print(str(a)+" "+str(e)+" "+str(i)+" "+str(o)+" "+str(u))
                        print("The phrase contains only vowels:"+input1)
                #else
                else:
                        #printing count of each vowel
                        print("A E I O U")
                        print(str(a)+" "+str(e)+" "+str(i)+" "+str(o)+" "+str(u))
                        print("The original phrase is: "+input1)
                        vowels = ('a', 'e', 'i', 'o', 'u','A','E','I','O','U')
                        #removing vowels from input1
                        for k in input1:
                                if(k in vowels):
                                        input1=input1.replace(k,"")
                        #printing
                        print("The phrase without vowels is: "+input1)
#main function
def main():
        #caling each function
        demo()
        vowelcounter("Remember that context here defines \"+\" as 'Concatenate'")
        vowelcounter("bcdfghjklmnpqrstvwxyz")
        vowelcounter("aeiouAEIOU")
        vowelcounter("")

if __name__ == '__main__':
        main()


Related Solutions

Using Python 3 The primary objective of this assignment is to reinforce the concepts of string...
Using Python 3 The primary objective of this assignment is to reinforce the concepts of string processing. Part A: Even or Odd For this first part, write a function that will generate a random number within some range (say, 1 to 50), then prompt the user to answer if the number is even or odd. The user will then input a response and a message will be printed indicated whether it was correct or not. This process should be repeated...
The purpose of this assignment is to reinforce Ada concepts. Define a Complex-numbers package includes the...
The purpose of this assignment is to reinforce Ada concepts. Define a Complex-numbers package includes the following operations: Addition Subtraction Multiplication Division A “main program” needs to create one or more complex numbers, perform the various arithmetic operations, and then display results. Develop the program in ADA code with several packages. Write a short report that documents all work done, including the overall design, explanation of the implementation, the input data used to run the program, the output of the...
The objective of this homework assignment is to demonstrate proficiency with reading files, and using string...
The objective of this homework assignment is to demonstrate proficiency with reading files, and using string methods to slice strings, working with dictionaries and using-step wise development to complete your program. Python is an excellent tool for reading text files and parsing (i.e. filtering) the data. Your assignment is to write a Python program in four steps that reads a Linux authentication log file, identifies the user names used in failed password attempts and counts the times each user name...
Objective This assignment aims to investigate the concepts in solving problems on the special theory of...
Objective This assignment aims to investigate the concepts in solving problems on the special theory of relativity, analyze the effects of relativity and evaluate the validity of results on the application of relativity. Problem Solving, Analysis and Evaluating of the Validity of Results Direction: Solve the given problem using systematic/logical solution. Make an analysis and evaluation of the results. Rubrics will be used in marking. (1) Determine Ƴ if v = 0.01c; 0.1c; 0.5c; 0.6c; 0.8c; 0.9c; 0.99c; 1.00c; and...
Database Application Development Project/Assignment Milestone 1 (part 1) Objective: In this assignment, you create a simple...
Database Application Development Project/Assignment Milestone 1 (part 1) Objective: In this assignment, you create a simple HR application using the C++ programming language and Oracle server. This assignment helps students learn a basic understanding of application development using C++ programming and an Oracle database Submission: This Milestone is a new project that simply uses what was learned in the SETUP. Your submission will be a single text-based .cpp file including your C++ program for the Database Application project/assignment. The file...
The purpose of this problem set is to reinforce your knowledge of some basic chemical concepts...
The purpose of this problem set is to reinforce your knowledge of some basic chemical concepts that are important for the origin of the elements. 1. Use the abundances of the stable isotopes of strontium and the masses of these nuclides (found at http://atom.kaeri.re.kr/nuchart/) to calculate the atomic weight of strontium. Compare the value that you get with the value shown in the periodic table found at http://www.rsc.org/periodic-table. Show your work. 2. Consult the chart of the nuclides (http://atom.kaeri.re.kr/nuchart/ )...
The primary objective in setting transfer prices is to _______
The primary objective in setting transfer prices is to _______  A) establish a system that determines the best transfer prices for the company as a whole B) evaluate the managers of the responsibility centers involved C) achieve goal congruence by selecting a price that will maximize overall company profits D) make it easy for managers to select prices that maximize division profits
Composite engineering In a design assignment, you are asked to use continuous carbon fibres to reinforce...
Composite engineering In a design assignment, you are asked to use continuous carbon fibres to reinforce an epoxy matrix to achieve Young's modulus of 250 GPa along the longitudinal direction of the composite. The epoxy matrix to use has a Young's modulus of 2 GPa and the continuous carbon fibres 400 GPa. If the allowed maximum loading of the C fibres is 60 vol%, is the design goal achievable? You are required to derive any equation needed in the calculation...
how does income as the primary objective and capital growth as a sole objective affect investments
how does income as the primary objective and capital growth as a sole objective affect investments
Objective: The objective of this assignment is to learn about how to use common concurrency mechanism.        ...
Objective: The objective of this assignment is to learn about how to use common concurrency mechanism.         Task: In this assignment you will be doing the followings: 1. Write a program (either using Java, python, C, or your choice of programming language) to create three threads: one for deposit function and two for withdraw functions. 2. Compile and run the program without implementing any concurrency mechanism. Attach your output snapshot. 3. Now, modify your program to implement any concurrency mechanism. Compile...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT