ALL IN PYTHON LANGUAGE 1 Implement the function matchfirstl

********ALL IN PYTHON LANGUAGE ***********

(1) Implement the function match_first_letter that takes a one-character string and a list of strings and prints all the strings in the list that start with the specified character.

(2) Implement the function match_area_code that takes as a list of telephone area codes (three-digit strings) and a list of phone numbers in the form shown below. The function will print the phone numbers whose area code is on the list of area codes.

(3) Copy your match_area_code function and name the copy matching_area_codes. Now modify your new function so that instead of printing out the matching phone numbers, it returns a list of the matching numbers. Follow the pattern of accumuating a result that we\'ve seen in class. (d.1) Write a function called is_vowel that takes a one-character-long string and returns True if that character is a vowel and False otherwise. [A vowel, for our purposes here, is one of the letters a, e, i, o, or u (lower case or upper case).] You can do the central part of this task using the in operator and a string containing the vowels; your task here is to package this up into a function that returns a boolean value.

(4) Write a function called is_vowel that takes a one-character-long string and returns True if that character is a vowel and False otherwise. [A vowel, for our purposes here, is one of the letters a, e, i, o, or u (lower case or upper case).] You can do the central part of this task using the in operator and a string containing the vowels; your task here is to package this up into a function that returns a boolean value. [Please check that the function name is capitalized precisely as shown above; it helps your TA in the grading.]

(5)Write a function called print_nonvowels that takes a string and prints out all the characters in the string that are not vowels. Use your is_vowel function (and one of the logical/boolean operators). (Note that this function prints its result rather than returning it. Whenever you define a function, the first thing to determine is whether the result should be returned or printed. When in doubt, return the value and let the program that called the function decide what to do with it. For class purposes, we\'ll typically just tell you which to do, as we did here.)

Write some tests (also involving print statements), enough so that if they pass, you\'re convinced that your function works correctly. And run the tests, of course.

(6) Write a function called nonvowels that takes a string and returns a string containing all the characters in the parameter string that are not vowels. Since you\'re returning a string instead of printing a character at a time, you\'ll need to construct that string in your function. Start with a variable (let\'s call it result) whose value is the empty string. Each time you find a nonvowel, add it to the end of result. Then, once you\'ve gone through the whole parameter, result is what you return. [We just gave you the algorithm; your job is to implement it in Python.]

Functions that return values are somewhat more convenient to test than functions that print values. You could do it the same way, printing out the tests and the results or printing out boolean expresions that are true if the test passes. Instead of print statements, though, you should use assert statements as shown below. Their advantage is that if the test passes, nothing happens, so you can leave the assert statements in your code after you\'re satisfied your function works, without their cluttering up your output. (It\'s good to keep the assert statements there because later you may make changes that \"break\" your existing code, or you may need to change your existing code, and with the tests still in place you\'ll find any problems at the earliest possible time.)

Here are some assert statements for double and for is_vowel; note that the the assert statement expects a boolean expression.

To see what happens when a test fails, assert something false like assert double(2)==5. (Note that when an assertion fails, there could be two reasons: (i) Your function may be incorrect, or (ii) your assertion—what you think the right answer is—may be wrong. You should consider both possibilities).

Write some tests for nonvowels using assert statements.

(7) Write a function called consonants that takes a string and returns a string containing all the letters in the parameter string that are not vowels. (This is not the same as nonvowels, whose definition refers to \"characters,\" which include digits and spaces and punctuation. Did you test nonvowels with strings including non-letters? If not, go back and do it, changing the function\'s definition if necessary to make it work correctly.)

Before you write the body of the function, follow the \"design recipe\" steps: specify the types of the parameter and return value; include a short purpose statement as a docstring; write examples in the form of assert statements.

(8) Write a function called select_letters that takes two parameters, both strings, and returns a string. If the first parameter is \'v\', it returns a string containing all the vowels in the second parameter; if the first parameter is \'c\', it returns a string containing all the consonants in the second parameter. If the first parameter is anything else, it returns the empty string. So, select_letters(\'v\', \'facetiously\') would return aeiou and select_letters(\'c\', \'facetiously\') would return fctsly. [If you count Y as a vowel, your results will be slightly different.]

(9) Write a function called hide_vowels that takes a string and returns a string in which every vowel in the parameter is replaced with a hyphen (\"-\") and all other characters remain unchanged. After your testing shows that it\'s correct, try running it with a couple of sentences; you may be able to understand the sentences even with all the vowels hidden.

These all connect to each other therefore i had to put it in one question, please help!

Solution

1)match_first_letter
# Sample strings.
list = [\"dog dot\", \"do don\'t\", \"dumb-dumb\", \"no match\"]
# Loop.
for element in list:
# Match if two words starting with letter d.
m = re.match(\"(d\\w+)\\W(d\\w+)\", element)

# See if success.
if m:
print(m.groups())
Output
(\'dog\', \'dot\')
(\'do\', \'don\')
(\'dumb\', \'dumb\')

Pattern: (d\\w+)\\W(d\\w+)

d Lowercase letter d.
\\w+ One or more word characters.
\\W A non-word character

2)match_area_code
def phone (nlist, nlist1):
results = {}
for x in nlist1:
results.setdefault(x[0:3], [])
results[x[0:3]].append(x)
for x in nlist:
if x in results:
print(results[x])

3)

4)def is_vowel(char):
vowels = (\'a\', \'e\', \'i\', \'o\', \'u\')
if char not in vowels:
return False
return True


if __name__ == \"__main__\":
print is_vowel(1)
print is_vowel(\'a\')
print is_vowel(\'b\')

5) import string

all_letters = string.ascii_letters

consonants = set(all_letters).difference(set((\'a\',\'e\',\'i\',\'o\',\'u\',\'A\',\'E\',\'I\',\'O\',\'U\')))

my_sentence = \'Here is my Sentence\'

sum_of_cons = sum(ele in consonants for ele in my_sentence)

OR

vowels = list(\"aeiou\")
consonants = list(\"bcdfghjklmnpqrstvwxyz\")

complete = False
while complete == False:
print(\"\")   
words = input(\"Kindly enter a sentence or a word :=> \").lower().strip()

number_of_consonants = sum(words.count(c) for c in consonants)
number_of_vowels = sum(words.count(c) for c in vowels)
  
print(\"\")   
print(\"The String is\" ,str(words).upper(),\".\")
print(\"\")
print(\" Number of Vowels : \",number_of_vowels)
print(\" Number of Consonants : \",number_of_consonants)
print(\"\")
choice = input(\"Are You Finish? Y/N: \").lower().strip()
if choice == \"y\":
complete = True
else:
print(\"\")
print(\"\ \")   


9) def hide_vowels(p: str) -> str:
   list = list(p)
   thingy = []
   for x in list:
       if is_vowel(x) == True:
           thingy.append(\'-\')
       else:
           thingy.append(x)
   return \'\'.join(thingy)
assert hide_vowels(\'abcd\') == \'-bcd\'
print(hide_vowels(\'aiopwdmawiopmd\'))

********ALL IN PYTHON LANGUAGE *********** (1) Implement the function match_first_letter that takes a one-character string and a list of strings and prints all
********ALL IN PYTHON LANGUAGE *********** (1) Implement the function match_first_letter that takes a one-character string and a list of strings and prints all
********ALL IN PYTHON LANGUAGE *********** (1) Implement the function match_first_letter that takes a one-character string and a list of strings and prints all

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site