Question: # Fill in the code for the functions below. # The starter code for each function includes a 'return' # which is just a placeholder
# Fill in the code for the functions below. # The starter code for each function includes a 'return' # which is just a placeholder for your code. Make sure to add what is going to be returned. (Python) # Part A. count_char # Define a function count_char(s, char) that takes a string and a character # and returns the number of times the given character appears in the string def count_char(s, char): # YOUR CODE HERE return # Part B. is_power_of # Define a function is_power_of(i,j) that takes 2 ints i and j # and checks if i is a power of j or not # the function should return True indicating that i is a power of j # otherwise return False def is_power_of(i,j): # YOUR CODE HERE return # Part C. longest_word # Define a function longest_word(s) that takes a string s # where s is a sentence made up of words separated by a single space " " # and returns the longest word in this sentence # if 2 or more words are tied as longest then return the one that occurs LAST in the sentence # if s is an empty string return an empty string def longest_word(s): # YOUR CODE HERE return
----------------------------------------------------------
test file
import pythonActivty # main() is already set up to call the functions # we want to test with a few different inputs, # printing 'OK' when each function is correct. # the simple provided test() function used in main() to print # what each function returns vs. what it's supposed to return. def test(got, expected): if got == expected: prefix = ' OK ' else: prefix = ' X ' print ('%s got: %s expected: %s' % (prefix, repr(got), repr(expected))) # Calls the functions in pythonBasics1 with interesting inputs. def main(): # set which functions to test check_count_char = True check_is_power_of = True check_longest_word = True if check_count_char: print('Testing count_char:') test(pythonBasics1.count_char('vest','v'), 1) test(pythonBasics1.count_char('crypt','c'), 1) test(pythonBasics1.count_char('#python##','#'), 3) test(pythonBasics1.count_char('hello','H'), 0) test(pythonBasics1.count_char('',' '), 0) test(pythonBasics1.count_char('123',''), 0) test(pythonBasics1.count_char('',''), 0) test(pythonBasics1.count_char(' ',' '), 1) test(pythonBasics1.count_char('1211211','1'), 5) test(pythonBasics1.count_char('Abracadabra','a'), 4) test(pythonBasics1.count_char('pXXXp','X'), 3) test(pythonBasics1.count_char(' 6 6',' '), 2) if check_is_power_of: print("---------------------------------------------------------") print('Testing is_power_of:') test(pythonBasics1.is_power_of(1,1), True) test(pythonBasics1.is_power_of(10,1), True) test(pythonBasics1.is_power_of(1000,1), True) test(pythonBasics1.is_power_of(2,16), True) test(pythonBasics1.is_power_of(4,16), True) test(pythonBasics1.is_power_of(4,17), False) test(pythonBasics1.is_power_of(3,81), True) test(pythonBasics1.is_power_of(3,-81), False) test(pythonBasics1.is_power_of(-2,16), True) test(pythonBasics1.is_power_of(-2,-16), False) test(pythonBasics1.is_power_of(-2,-8), True) test(pythonBasics1.is_power_of(3,0), False) test(pythonBasics1.is_power_of(0,0), True) if check_longest_word: print("-------------------------------------------------------") print('Testing longest_word') test(pythonBasics1.longest_word("This is a test"),"test") test(pythonBasics1.longest_word("Autumn is a second spring when every leaf is a flower"),"flower") test(pythonBasics1.longest_word("The truth springs from arguments amongst friends"),"arguments") test(pythonBasics1.longest_word("He who has a why to live for can bear almost any how"),"almost") test(pythonBasics1.longest_word("It is only those who do nothing who makes no mistake"),"mistake") test(pythonBasics1.longest_word(""),"") if __name__ == '__main__': main() Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
