上述问题的输入和标准输出如下>>> p0 = [2, 2, 3] >>> p1 = [6, 1, 2] >>> fastest_words(game(['What', 'great', 'luck'], [p0, p1])) [['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 [['What', 'luck'], ['great']] >>> p2 = [4, 3, 1] >>> fastest_words(game(['What', 'great', 'luck'], [p0, p1, p2])) [['What'], ['great'], ['luck']]请修改代码
时间: 2024-01-21 11:04:31 浏览: 31
Sure, here's the updated implementation of the function fastest_words() that meets the requirements of the given test cases:
```
def fastest_words(game):
num_players = len(game.times)
num_words = len(game.words)
player_words = [[] for _ in range(num_players)]
fastest_times = [float('inf')] * num_words
for player_index in range(num_players):
for word_index in range(num_words):
time = game.time(player_index, word_index)
if time < fastest_times[word_index]:
fastest_times[word_index] = time
for word_index in range(num_words):
fastest_player = None
fastest_time = float('inf')
for player_index in range(num_players):
time = game.time(player_index, word_index)
if time == fastest_times[word_index]:
if fastest_player is None or player_index < fastest_player:
fastest_player = player_index
fastest_time = time
if fastest_player is not None:
player_words[fastest_player].append(game.word_at(word_index))
return player_words
```
This updated implementation first finds the fastest time for each word across all players, and then loops through each word again to find the player(s) who typed the word the fastest. If there's a tie, the function chooses 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.
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)