Skip to content

Instantly share code, notes, and snippets.

@aryamansharda
Created June 11, 2021 15:51
Show Gist options
  • Save aryamansharda/f30f586a8caef9222a49a79d35313d9b to your computer and use it in GitHub Desktop.
Save aryamansharda/f30f586a8caef9222a49a79d35313d9b to your computer and use it in GitHub Desktop.
Typing Distance Calculator
file = open('blog.txt', 'r')
characters = {
"1": 4,
"2": 4,
"3": 4,
"4": 4,
"5": 4,
"6": 5,
"7": 4,
"8": 4,
"9": 4,
"0": 4,
"a": 0,
"b": 3,
"c": 2,
"d": 0,
"e": 2,
"f": 0,
"g": 2,
"h": 2,
"i": 2,
"j": 0,
"k": 0,
"l": 0,
'm': 2,
"n": 2,
"o": 2,
"p": 2,
"q": 2,
"r": 2,
"s": 0,
"t": 2,
"u": 2,
"v": 2,
"w": 2,
"x": 2,
"y": 3,
"z": 2,
",": 2,
".": 2,
"<": 2,
">": 2,
"/": 2,
"?": 2,
";": 0,
"'": 1,
":": 0,
"\"": 2,
"[": 2,
"]": 4,
"\\": 5.5,
"{": 2,
"}": 4,
"|": 5.5,
"`": 5,
"~": 5,
"!": 4,
"@": 4,
"#": 4,
"$": 4,
"%": 4,
"^": 5,
"&": 4,
"*": 4,
"(": 4,
")": 4,
"-": 4,
"_": 4,
"=": 4.5,
"+": 4.5,
" ": 0,
"\n": 4
}
# All measurements are in centimeters
sum = 0
totalCharacterCount = 0
distanceToShift = 3
currentTravelDistance = 0
previousCharacter = None
while True:
# read by character
char = file.read(1)
# Used to track the total number of key presses
# We'll add this in the end to include the vertical distance of typing into our result
if not char:
break
totalCharacterCount += 1
if char.lower() in characters:
# Handling different potential Shift scenarios
if previousCharacter is None and char.isupper():
sum += distanceToShift
# If the character is now uppercase, we want to assume they've started hitting Shift
elif char.isupper() and not previousCharacter.isupper():
sum += distanceToShift
# If the current character is no longer uppercase, we can assume the finger has returned from Shift
elif previousCharacter is not None and previousCharacter.isupper() and not char.isupper():
sum += distanceToShift
if char != previousCharacter and previousCharacter is not None:
sum += characters[str(previousCharacter.lower())]
currentTravelDistance = characters[str(char.lower())]
sum += currentTravelDistance
elif previousCharacter is None:
sum += characters[str(char.lower())]
previousCharacter = char
# Handles coming home after pressing the last character
sum += currentTravelDistance
# Handles the case that the less character pressed was uppercase
if previousCharacter.isupper():
sum += distanceToShift
# ---------------------------------- #
# CM -> Miles conversion
print("Miles: " + str(sum * 0.0000062137))
# Adds 1mm for every character to account for the vertical key press
sum += totalCharacterCount * .1
print("Miles including vertical distance traveled: " + str(sum * 0.0000062137))
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment