Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import time
- from random import SystemRandom
- from decimal import Decimal, getcontext
- getcontext().prec = 3
- minimumWordLength = 5
- numberOfWords = 25
- class typingspeed:
- def __init__(self, wordfile):
- self.rdev = SystemRandom()
- try:
- possibleWords = set(open(wordfile, "r").read().splitlines())
- possibleWords = filter(lambda _: len(_) >= minimumWordLength, possibleWords)
- possibleWords = [word.strip() for word in possibleWords]
- assert(len(possibleWords) >= numberOfWords)
- # List comprehension form:
- # possibleWords=[word.strip() for word in filter(lambda _: len(_)>=5, set(open(wordfile, "r").readlines()))]
- except IOError:
- print("File '{}' does not exist.".format(wordfile))
- return
- # (word, [0, 0]) = word, [time taken for word, string attempt (for later comparison), errors]
- self.words = dict((word, None) for word in self.rdev.sample(possibleWords, numberOfWords))
- def begin(self):
- while raw_input("Type 'ready' to begin: ").lower() != "ready":
- pass
- for word in self.words:
- print(word)
- t1 = time()
- attempt = raw_input("")
- t2 = time()
- self.words[word] = [t2 - t1, attempt == word]
- def __str__(self):
- if not hasattr(self, "words"):
- return ""
- successfulWords = {
- word: [timetaken, correct]
- for (word, [timetaken, correct]) in self.words.items()
- if correct == True
- }
- if len(successfulWords) < 1:
- return "All words were errors. You are a fucking terrible typer."
- fastestWord = min(
- successfulWords.items(),
- key=lambda word: word[1][0] / len(word[0])
- )
- slowestWord = max(
- successfulWords.items(),
- key=lambda word: word[1][0] / len(word[0])
- )
- return '\n'.join([
- "Fastest Word: {} {}s/letter",
- "Slowest Word: {} {}s/letter"
- ]).format(
- fastestWord[0],
- Decimal(fastestWord[1][0])/len(fastestWord[0]),
- slowestWord[0],
- Decimal(slowestWord[1][0])/len(fastestWord[0])
- )
- ts = typingspeed("words.txt")
- ts.begin()
- print(ts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement