Computers and Technology

Write the function for "fastest_words", which returns which words each player typed fastest. This function is called once both players have finished typing. It takes in a game as an argument. The game argument is a game data abstraction, like the one returned in Problem 9. You can access words in the game with selectors word_at, which takes in a game and the word_index (an integer). You can access the time it took any player to type any word using time. The fastest_words function returns a list of lists of words, one list for each player, and within each list the words they typed the fastest. In the case of a tie, consider the earliest player in the list (the smallest player index) to be the one who typed it the fastest. Be sure to use the accessor functions for the game data abstraction, rather than assuming a particular data format. def fastest_words(game):
"""Return a list of lists of which words each player typed fastest.

Arguments:
game: a game data abstraction as returned by time_per_word.
Returns:
a list of lists containing which words each player typed fastest
"""
players = range(len(all_times(game))) # An index for each player
words = range(len(all_words(game))) # An index for each word

Here are some examples of it working correctly:

>>> from cats import game, fastest_words
>>> p0 = [2, 2, 3]
>>> p1 = [6, 1, 2]
>>> fastest_words(game(['What', 'great', 'luck'], [p0, p1]))

returns [['What'], ['great', 'luck']]

>>> p0 = [2, 2, 3]
>>> p1 = [6, 1, 3]
>>> fastest_words(game(['What', 'great', 'luck'], [p0, p1])) # with a tie, choose the first player

returns [['What','luck'], ['great']]

Below are the supplementary functions mentioned in the explanation

def game(words, times):
"""A data abstraction containing all words typed and their times."""
assert all([type(w) == str for w in words]), 'words should be a list of strings'
assert all([type(t) == list for t in times]), 'times should be a list of lists'
assert all([isinstance(i, (int, float)) for t in times for i in t]), 'times lists should contain numbers'
assert all([len(t) == len(words) for t in times]), 'There should be one word per time.'
return [words, times]

def word_at(game, word_index):
"""A selector function that gets the word with index word_index"""
assert 0 <= word_index < len(game[0]), "word_index out of range of words"
return game[0][word_index]

def all_words(game):
"""A selector function for all the words in the game"""
return game[0]

def all_times(game):
"""A selector function for all typing times for all players"""
return game[1]

def time(game, player_num, word_index):
"""A selector function for the time it took player_num to type the word at word_index"""
assert word_index < len(game[0]), "word_index out of range of words"
assert player_num < len(game[1]), "player_num out of range of players"
return game[1][player_num][word_index]

def game_string(game):
"""A helper function that takes in a game object and returns a string representation of it"""
return "game(%s, %s)" % (game[0], game[1])

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 05:00, 105001964
Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. after the loop terminates, it prints out, on a line by itself and separated by spaces, the sum of all the even integers read, the sum of all the odd integers read, a count of the number of even integers read, and a count of the number of odd integers read, all separated by at least one space. declare any variables that are needed. assume the availability of a variable, stdin, that references a scanner object associated with standard input. that is, stdin = new scanner(system. in); is given.
Answers: 3
image
Computers and Technology, 22.06.2019 09:50, laurentsofia09
Assume that you have an sorted array of records. assume that the length of the array (n) is known. give two different methods to search for a specific value in this array. you can use english or pseudo-code for your algorithm. what is the time complexity for each algorithm and why?
Answers: 1
image
Computers and Technology, 22.06.2019 12:00, dani19cano
The following function returns a string of length n whose characters are all 'x'. give the order of growth (as a function of n) of the running time. recall that concatenating two strings in java takes time proportional to the sum of their lengths. public static string f(int n) { if (n == 0) return ""; if (n == 1) return "x"; return f(n/2) + f(n - n/2); } options: a) constant b) logarithmic c) linear d) linearithmic e)quadratic f)cubic g) exponential
Answers: 2
image
Computers and Technology, 23.06.2019 18:00, joybeth9591
What can a word user do with the customize ribbon dialog box? check all that apply. minimize the ribbon add a new tab to the ribbon remove a group from a tab add a group to a tab choose which styles appear choose which fonts appear choose tools to appear in a group
Answers: 1
Do you know the correct answer?
Write the function for "fastest_words", which returns which words each player typed fastest. This fu...

Questions in other subjects:

Konu
Computers and Technology, 16.06.2020 15:57
Konu
Mathematics, 16.06.2020 15:57
Konu
Chemistry, 16.06.2020 15:57
Konu
Mathematics, 16.06.2020 15:57
Konu
Mathematics, 16.06.2020 15:57