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.
时间: 2024-02-25 22:58:50 浏览: 78
LFSR.rar_IMPLEMENT _LFSR FPGA_in_lfsr_lfsrimplement
Here's a possible implementation of the `time_per_word` function:
```python
from typing import List
from game import Game
def time_per_word(times_per_player: List[List[int]], words: List[str]) -> Game:
num_players = len(times_per_player)
num_words = len(words)
times = [[times_per_player[p][w+1] - times_per_player[p][w] for w in range(num_words-1)] for p in range(num_players)]
return Game(words=words, times=times)
```
This function takes in `times_per_player` and `words` as input parameters, and returns a `Game` object constructed from this information. The `Game` object has two attributes: `words` and `times`. `words` is simply the list of words provided as input. `times` is a list of lists that stores the time it took each player to type each word.
To compute `times`, we first determine the number of players and the number of words. Then, we iterate over each player and each word, and compute the time it took that player to type that word. This is done by subtracting the timestamp of the previous word from the timestamp of the current word. The result is a list of lists that represents the time it took each player to type each word.
Finally, we construct a `Game` object using the `Game` constructor, passing in the `words` and `times` as arguments. This ensures that the resulting object has the correct data type and structure.
阅读全文