Implement time_per_word, which takes in times_per_player, a list of lists for each player with timestamps indicating when each player finished typing each word. It also takes in a list words. It returns a game with the given information. A game is a data abstraction that has a list of words and times. The times are stored as a list of lists of how long it took each player to type each word. times[i][j] indicates how long it took player i to type word j. For example, if times_per_player = [[1, 3, 5], [2, 5, 6]], the corresponding time attribute of the game would be [[2, 2], [3, 1]]. Timestamps are cumulative and always increasing, while the values in time are differences between consecutive timestamps. Be sure to use the game constructor when returning a game, rather than assuming a particular data format.Given timing data, return a game data abstraction, which contains a list of words and the amount of time each player took to type each word. Arguments: times_per_player: A list of lists of timestamps including the time the player started typing, followed by the time the player finished typing each word. words: a list of words, in the order they are typed.
时间: 2024-02-25 12:59:00 浏览: 35
Here's a possible implementation of the `time_per_word` function:
```python
from typing import List
from dataclasses import dataclass
@dataclass
class Game:
words: List[str]
times: List[List[int]]
def time_per_word(times_per_player: List[List[int]], words: List[str]) -> Game:
n_players = len(times_per_player)
n_words = len(words)
times = [[0] * (n_words - 1) for _ in range(n_players)]
for i in range(n_players):
for j in range(1, n_words):
times[i][j - 1] = times_per_player[i][j] - times_per_player[i][j - 1]
return Game(words=words, times=times)
```
The function takes in `times_per_player`, a list of lists where each inner list represents the timestamps for a single player, and `words`, a list of strings representing the words in the order they were typed. It returns a `Game` object containing the words and the time each player took to type each word.
The implementation uses a nested loop to iterate over all pairs of players and words, and computes the time difference between consecutive timestamps for each player and word. The resulting `times` matrix has dimensions `(n_players, n_words - 1)` to account for the fact that the first timestamp only indicates when typing started, not when the first word was completed. The `Game` object is constructed using the `dataclass` decorator from Python 3.7+ to create a simple data structure with named fields.
Note that this implementation assumes that all players typed the same set of words in the same order, and that the timestamps are cumulative and increasing (i.e., the time to type each word is always positive). If these assumptions don't hold, the function may produce incorrect results or raise errors.
相关推荐
![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)
![](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)