Question

Write the following functions, as defined by the given IPO comments. Remember to include the IPO...

Write the following functions, as defined by the given IPO comments. Remember to include the IPO comments in your submission. Be sure to test your code -- you can use the examples given for each function, but you should probably test additional examples as well -- and then comment-out or remove your testing code from your submission!. We will be testing your functions using a separate program, and if your own testing code runs when we load your file it will interfere with our automated tests. PYTHON!!!

A. compute_frequency

Function description as IPO:

# function: compute_frequency
# input:    a string to analyze
# processing: counts the frequency of each character in the supplied string and 
#   produces a dictionary that represents this information
# output: a dictionary that contains the frequency analysis of the supplied string

Examples of the function in use:

# Example code:
f1 = compute_frequency("hello world")
print (f1)
# {'o': 2, 'e': 1, 'd': 1, 'l': 3, 'w': 1, 'h': 1, 'r': 1, ' ': 1}

f2 = compute_frequency("pika 4 prez")
print (f2)
# {'e': 1, 'r': 1, 'k': 1, 'a': 1, 'i': 1, 'p': 2, 'z': 1, '4': 1, ' ': 2}

f3 = compute_frequency("") 
print (f3)
# {}

B. count_consonants

# function:  count_consonants
# input:     a word to examine (String)
# processing: counts the number of times that a consonant character occurs
#  in the source word. For this program a vowel is ‘a’, ‘e’, ‘i’, ‘o’ or ‘u’ and 
#  a consonant is any alphabetic character that is NOT a vowel
# output: the number of times a consonant character appears in the word (integer)
# Example code:
count1 = count_consonants("Hello World") 
print (count1) # 7
count2 = count_consonants("AEIOUaeiou") 
print (count2) # 0
count3 = count_consonants("") 
print (count3) # 0

C. remove_puctuation

# function:  remove_puctuation
# input:     a piece of text (String)
# processing: go through the text and remove any character that is not a letter, number or space
# output: a copy of the string with all special characters and punctuation removed
# Example code:
my_str = "This -- it can be said -- wasn't the 1st, or, indeed, the 2nd, 3rd, or last! -- time."
out_str = remove_punctuation(my_str)
print(out_str)
# "This  it can be said  wasnt the 1st or indeed the 2nd 3rd or last  time"
## Note that there are some double spaces, where a '--' was removed.

D. my_range

# function:  my_range
# input: a starting value, an ending value and a skip value (all integers)
# processing: mimics the functionality of the range() function by constructing a list that begins
#   at the start value, continues to (but does not include) the end value and skips by the
#   skip value each time. YOU CANNOT USE THE range() FUNCTION TO DO THIS! Note that you can 
#   assume that the function will be supplied with a valid skip value (i.e. you won’t have to deal
#   with my_range(5, 0, 1) which is an impossible range) but you DO have to account for negative skip values!
# output: returns the generated list
  # Example code:
range1 = my_range(0, 10, 2) 
print (range1) 
# [0, 2, 4, 6, 8]

range2 = my_range(2, 5, 1) 
print (range2) 
# [2, 3, 4]

range3 = my_range(5, 0, -1) 
print (range3) 
# [5, 4, 3, 2, 1]

E. is_float

Write a Boolean function called is_float that will analyze a given string to determine if it represents a valid floating-point value, and return True if it does, or False if it does not. Remember, the number may be positive or negative, and may or may not have a decimal component (integers are a subset of floats).

Examples:

>>> is_float("hello")    
False
>>> is_float("25")    
True
>>> is_float("-13.4") 
True

This function shouldn't require a lot of lines of code. Just make sure your function accepts all possible valid floats, and rejects anything that cannot be a float. For example: "3.2.4", "-75-", "a345" should all return False.

Here is the IPO specification:

# is_float
# Input:  a string that may represent a floating-point number
# Processing:  check if the string could be converted to a float value
#         without error, by analyzing the characters in the string.
# Output: (bool) True if the string represents a valid float, else False

Note: DO NOT USE try/except FOR THIS FUNCTION! For this assignment you should actually examine the string to see if it contains a valid float value. This is a case where you know what conditions might cause an error, so you should be able to test for them before trying to convert the string to a float. There are only a few conditions you need to test and you have all the tools you need to do this.

PYTHON!!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#FIND ATTACHED SS

# function: compute_frequency
# input: a string to analyze
# processing: counts the frequency of each character in the supplied string and
#produces a dictionary that represents this information
# output: a dictionary that contains the frequency analysis of the supplied string

def compute_frequency(string):
d = {}
for i in string:
if i not in d.keys():
d[i]=1
else:
d[i]+=1
return d


# function: count_consonants
# input: a word to examine (String)
# processing: counts the number of times that a consonant character occurs
# in the source word. For this program a vowel is ‘a’, ‘e’, ‘i’, ‘o’ or ‘u’ and
# a consonant is any alphabetic character that is NOT a vowel
# output: the number of times a consonant character appears in the word (integer)

def count_consonants(string):
count = 0
for i in string:
if i not in ['a','e','i','o','u','A','E','I','O','U',' '] and ((ord(i)>=65 and ord(i)<=90) or (ord(i)>=97 and ord(i)<=122)):
count+=1
return count

# function: remove_puctuation
# input: a piece of text (String)
# processing: go through the text and remove any character that is not a letter, number or space
# output: a copy of the string with all special characters and punctuation removed
# Example code:

def remove_punctuation(string):
s=''
for i in string:
if i in ['0','1','2','3','4','5','6','7','8','9',' '] or ((ord(i)>=65 and ord(i)<=90) or (ord(i)>=97 and ord(i)<=122)):
s=s+i
return s


# function: my_range
# input: a starting value, an ending value and a skip value (all integers)
# processing: mimics the functionality of the range() function by constructing a list that begins
# at the start value, continues to (but does not include) the end value and skips by the
# skip value each time. YOU CANNOT USE THE range() FUNCTION TO DO THIS! Note that you can
# assume that the function will be supplied with a valid skip value (i.e. you won’t have to deal
# with my_range(5, 0, 1) which is an impossible range) but you DO have to account for negative skip values!
# output: returns the generated list

def my_range(start,end,skip):
l=[]
i = start
if skip > 0:
while(i<end):
l.append(i)
i+=skip
return l
else:
while(i>end):
l.append(i)
i+=skip
return l

# is_float
# Input: a string that may represent a floating-point number
# Processing: check if the string could be converted to a float value
# without error, by analyzing the characters in the string.
# Output: (bool) True if the string represents a valid float, else False

def is_float(string):
dot = False
minus = False
x=""
if string=="-." or string==".-":
return False
for i in string:
if i.isdigit():
x+=i
elif i=='.' and dot==False and len(string)>1:
dot = True
x+=i
  
elif i=='-' and minus == False and len(string)>1:
minus=True
x+=i

else:
return False
if float(x):
return True
else:
return False

File Edit Format Run Options Window Help # function: compute_frequency # input: a string to analyze # processing: counts thedef count_consonants (string): count = 0 for i in string: if i not in [a, e,i,o, u,A, E,I,0,U, ) and ((# function: my_range # input: a starting value, an ending value and a skip value (all integers) # processing: mimics the func# is float Input: a string that may represent a floating-point number # Processing: check if the string could be converted to

Add a comment
Know the answer?
Add Answer to:
Write the following functions, as defined by the given IPO comments. Remember to include the IPO...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment 1) Your function will only write the Nth character of the array, supplied as a...

    Assignment 1) Your function will only write the Nth character of the array, supplied as a parameter by the caller. This is an example prototype (although you may want to change the return value type, based on the second twist, below). void writeArrayNthBackward(const char [], int, int, int); The first three arguments serve the same purpose as in the iterative example. The fourth argument (an int) supplies the N for the Nth character to print in the array. If n...

  • In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first'...

    In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and 'last' is the last index void writeArrayBackward(const char anArray[], int first, int last) { int i = 0; for (i = last; i >= first; i--) { std::cout << anArray[i]; } std::cout << std::endl; } // test driver int main() { const char *s = "abc123"; writeArrayBackward(s, 0, strlen(s) - 1); } // Using the...

  • Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc"....

    Please use Python def findSubstrings(s):     # Write your code here Consider a string, s = "abc". An alphabetically-ordered sequence of substrings of s would be {"a", "ab", "abc", "b", "bc", "c"}. If the sequence is reduced to only those substrings that start with a vowel and end with a consonant, the result is {"ab", "abc"}. The alphabetically first element in this reduced list is "ab", and the alphabetically last element is "abc". As a reminder: • Vowels: a, e, i,...

  • Write a program(Python language for the following three questions), with comments, to do the following:   ...

    Write a program(Python language for the following three questions), with comments, to do the following:    Ask the user to enter an alphabetic string and assign the input to a variable str1. Print str1. Check if str1 is valid, i.e. consists of only alphabetic characters. If str1 is valid Print “<str1> is a valid string.” If the first character of str1 is uppercase, print the entire string in uppercase, with a suitable message. If the first character of str1 is...

  • In C Sixth: Pig Latin (10 Points) For this part of the assignment, you will need...

    In C Sixth: Pig Latin (10 Points) For this part of the assignment, you will need to write a program that reads an input string representing a sentence, and convert it into pig latin. We'll be using two simple rules of pig latin: 1. If the word begins with a consonant then take all the letters up until the first vowel and put them at the end and then add "ay" at the end. 2. If the word begins with...

  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • In this task you are asked to completely implement the following functions (ie. write both the...

    In this task you are asked to completely implement the following functions (ie. write both the function header and function body) 1) You have been tasked to write a Processing function called isleapYear, which receives an integer representing a year. The function should return true if the year is a leap year and false otherwise. (For your information, a year is leap year if it is divisible by 4 but not 100, or is divisible by 400) 2) You have...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • PYTHON PLEASE Write a function to censor words from a sentence code: def censor_words(sentence, list_of_words): """...

    PYTHON PLEASE Write a function to censor words from a sentence code: def censor_words(sentence, list_of_words): """ The function takes a sentence and a list of words as input. The output is the sentence after censoring all the matching words in the sentence. Censoring is done by replacing each character in the censored word with a * sign. For example, the sentence "hello yes no", if we censor the word yes, will become "hello *** no" Note: do not censor the...

  • String Processing Labs Directions: Write a main program to test the three functions described below, Input...

    String Processing Labs Directions: Write a main program to test the three functions described below, Input is from a file, and output is to a file. 1. The function Another parameter is a char variable with a letter value. The function outputs the number of times the char value appears in the string. processes a string containing letters of the alphabet, which is a parameter 2. This Boolean function has two string parameters. It returns true when the first str...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT