Question: 1) The function word_count should take a list of strings, words, and return a dictionary which contains the words in the array as the keys

1)

The function word_count should take a list of strings, words, and return a dictionary which contains the words in the array as the keys and the number of times they appear in the array as the values.

word_count(['a', 'bb', 'a', 'a', 'bb']) {'bb': 2, 'a': 3}

word_count(['a', 'b', 'a', 'c', 'b']) {'a': 2, 'b': 2, 'c': 1}

word_count(['this', "and", "this", ""]) {"": 1, "and": 1, "this": 2}


complete in python do not delete the starter code the starter code is listed bellow

def word_count(words):

#Your code here

pass #placeholder statement - delete when you have actual code here


print(word_count(['a', 'bb', 'a', 'a', 'bb'])) #{'bb': 2, 'a': 3}

print(word_count(['a', 'b', 'a', 'c', 'b'])) #{'a': 2, 'b': 2, 'c': 1}

2)

We'll say that 2 strings "match" if they are non-empty and their first chars are the same. Loop over the given list of non-empty strings as follows: if a string "matches" an earlier string in the list, swap the two strings in the list. When a position in the list has been swapped, it no longer matches anything and can not be swapped again. Using a dictionary, this can be solved making just one pass over the array. all_swap(['ab', 'ac']) ['ac', 'ab'] all_swap(['ax', 'bx', 'cx', 'cy', 'by', 'ay', 'aaa', 'azz']) ['ay', 'by', 'cy', 'cx', 'bx', 'ax', 'azz', 'aaa'] all_swap(['ax', 'bx', 'ay', 'by', 'ai', 'aj', 'bx', 'by']) ['ay', 'by', 'ax', 'bx', 'aj', 'ai', 'by', 'bx']

complete in python do not delete the starter code the starter code is listed bellow def all_swap(lst): pass #delete this when your function has actual code lst1 = ['ab', 'ac'] all_swap(lst1) print(lst1) #['ac', 'ab'] lst2 = ['ax', 'bx', 'cx', 'cy', 'by', 'ay', 'aaa', 'azz'] all_swap(lst2) print(lst2) #['ay', 'by', 'cy', 'cx', 'bx', 'ax', 'azz', 'aaa']

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!