Implement 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. 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 the time function provided in cats.py. 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.其中game函数如下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
时间: 2024-02-29 07:52:43 浏览: 29
Sure, I can help you with that. Here's the implementation of the function fastest_words():
```
def fastest_words(game):
num_players = len(game.times)
num_words = len(game.words)
player_words = [[] for _ in range(num_players)]
for word_index in range(num_words):
fastest_player = 0
fastest_time = game.time(0, word_index)
for player_index in range(1, num_players):
time = game.time(player_index, word_index)
if time < fastest_time:
fastest_player = player_index
fastest_time = time
player_words[fastest_player].append(game.word_at(word_index))
return player_words
```
This function takes in a game data abstraction and returns a list of lists of words, one list for each player, and within each list the words they typed the fastest. The function first initializes an empty list for each player to store their fastest words. It then loops through each word in the game and finds the player who typed the word the fastest. If there's a tie, the function considers the earliest player in the list to be the one who typed it the fastest.
I hope this helps! Let me know if you have any other questions.
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)