Implement wpm, which computes the words per minute, a measure of typing speed, given a string typed and the amount of elapsed time in seconds. Despite its name, words per minute is not based on the number of words typed, but instead the number of characters, so that a typing test is not biased by the length of words. The formula for words per minute is the ratio of the number of characters (including spaces) typed divided by 5 (a typical word length) to the elapsed time in minutes. For example, the string "I am glad!" contains three words and ten characters (not including the quotation marks). The words per minute calculation uses 2 as the number of words typed (because 10 / 5 = 2). If someone typed this string in 30 seconds (half a minute), their speed would be 4 words per minute.
时间: 2024-02-25 21:57:38 浏览: 63
Sure, I can help you with that. Here's a Python function that computes the words per minute based on the input string and elapsed time:
```
def wpm(string, time):
num_chars = len(string)
num_words = (num_chars + 4) // 5 # round up to the nearest word length
minutes = time / 60
wpm = num_words / minutes
return wpm
```
You can call this function with the input string and elapsed time in seconds to get the words per minute. For example:
```
>>> string = "I am glad!"
>>> time = 30
>>> wpm(string, time)
4.0
```
Note that we add 4 to the number of characters before dividing by 5 to round up to the nearest word length, since the formula requires the number of words, not the number of complete words.
阅读全文