Write the choose function, which selects which paragraph the
user will type. It takes a list of paragraphs (strings), a select
function that returns True for paragraphs that can be selected, and
a non-negative index k. The choose function return's the kth
paragraph for which select returns True. If no such paragraph
exists (because k is too large), then choose returns the empty
string.
def choose(paragraphs, select, k):
"""Return the Kth paragraph from PARAGRAPHS for which SELECT called
on the
paragraph returns true. If there are fewer than K such paragraphs,
return
the empty string.
"""
For an example of it working:
>>> from cats import choose
>>> ps = ['short', 'really long', 'tiny']
>>> s = lambda p: len(p) <= 5
>>> choose(ps, s, 0)
'short'
>>> choose(ps, s, 1)
'tiny'
code :
4
output:
z
raw_code:
def choose(paragraphs,select,k):
out = []
for i in paragraphs:
if(select(i)):
out.append(i) #appending if it returns true
if(k<len(out)):
print(out[k]) #printing kth value
else:
print(' ') #printing empty string if k is too large
Write the choose function, which selects which paragraph the user will type. It takes a list...
Write a function called most_consonants(words) that takes a list of strings called words and returns the string in the list with the most consonants (i.e., the most letters that are not vowels). You may assume that the strings only contain lowercase letters. For example: >>> most_consonants(['python', 'is', 'such', 'fun']) result: 'python' >>> most_consonants(['oooooooh', 'i', 'see', 'now']) result: 'now' The function that you write must use a helper function (either the num_vowels function from lecture, or a similar function that you...
Guessing one password: write a function called guess that takes a string (a guessed password) as an argument and prints out matching information if its encrypted string matches any of the strings in the encrypted password file. When you write the function, you may hardcode the name of the password file. Here is an example run: >>> guess("blue") Found match: blue Line number: 1 Encrypted value: 16477688c0e00699c6cfa4497a3612d7e83c532062b64b250fed8908128ed548 You will want to use the encrypt function presented in class: import hashlib...
Write the accuracy function, which takes a typed paragraph and a reference paragraph. It returns the percentage of words in typed that exactly match the corresponding words in reference. Case and punctuation must match as well. A word in this context is any sequence of characters separated from other words by whitespace, so treat "dog;" as all one word. If a typed word has no corresponding word in the reference because typed is longer than reference, then the extra words...
Use the FDR to design and write a function, maxValTimes, that takes a list of integers and returns a set containing the value(s) that occur the same number of times as the maximum value in the list. If the list is empty, return an empty set. For example if the list was [2,1,1,2,3,3,1] the function would return {2,3} as the maximum value is 3 which occurs twice, and 2 also occurs twice (but 1 occurs 3 times). For full marks,...
Python 3.7.3 ''' Problem 3 Write a function called names, which takes a list of strings as a parameter. The strings are intended to represent a person's first and last name (with a blank in between). Assume the last names are unique. The function should return a dictionary (dict) whose keys are the people's last names, and whose values are their first names. For example: >>> dictionary = names([ 'Ljubomir Perkovic', \ 'Amber Settle', 'Steve Jost']) >>> dictionary['Settle'] 'Amber' >>>...
Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns a list consisting of the elements of L multiplied by the index number of the element at odd positions. (Use list comprehensions) >>> times_i_at_odd([1,2,3,4,5,6,7,8,9,10]) [2, 12, 30, 56, 90] Problem 2. Write a recursive function sum_cols(grid, n) that takes a list of lists of integers grid and integer n and returns the sum of column n in grid. For example, the call sum_cols([[1,2,3,4], [10,20,30,40],...
Write a recursive function called abb pattern with a single parameter astr, which is a string. The function returns True if astr is a string of the form a nb 2n (n a-s followed by 2n b-s, where n is a positive integer) and False otherwise. For example, abb pattern("abb"), abb pattern("aabbbb"), and abb pattern("aaaabbbbbbbb") all return True, but abb pattern("") (parameter is an empty string), abb pattern("abbabb"), and abb pattern("aaabbbbb") all return False. you may not use any built-in...
Complete the get_mid_letter() function which is passed a list of strings as a parameter. The function returns a string made up of the concatenation of the middle letter of each word from the parameter list. The string returned by the function should be in lowercase characters. If the parameter list is an empty list, the function should return an empty string For example Test Result print("1.", get mid_letter"Jess", "Cain", Amity", "Raeann"])) 1. siia Answer (penalty regime: 0 %) 1 -Idef...
Python Write a function square(a) that takes a list a of numbers and squares each of the numbers in the list. The function should not return a value (more specifically, it should return None). Write a function squared(a) that takes a list a of numbers and returns a new list that contains each value in a squared. The original list a should not be modified by your function. Write a function lowercase(a) that takes a list a of strings and...
Write a function is_mirror(s) that takes as input a string s and returns True if s is a mirrored string (i.e., a string that could have been produced by your mirror function) and False otherwise. Examples: >>> is_mirror('baconnocab') result: True >>> is_mirror('baconnoca') result: False Warning Your function should return a boolean value – either True or False, without any quotes around them. If you see quotes around the result when you make the calls above from the console, you must...