Last active
October 5, 2019 01:55
-
-
Save Syncrossus/dd69d185d9af39d84f8a600871b27691 to your computer and use it in GitHub Desktop.
Car model name score calculator based on XKCD #1571 : "Car Model Names"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
# values for 0-9 a-z as specified in the comic, with an additional 0 for | |
# unrecognized characters. | |
values = [60, -74, 6, 55, 35, 74, 6, -58, -67, -37, -14, -5, 27, -21, -45, 5, | |
27, -44, -21, 64, 32, 12, 19, -46, -80, -27, 40, 8, 15, -18, -68, | |
41, -20, 126, -90, 83, 0] | |
def to_index(c): | |
""" Determines the index to look at in the `values' list given a character c | |
""" | |
i = ord(c.lower()) | |
# numbers | |
if 48 <= i <= 57: | |
i -= 48 | |
# letters | |
elif 97 <= i <= 122: | |
i -= 87 | |
# unrecognized | |
else: | |
i = 36 | |
return i | |
def score(in_str): | |
return (sum([values[to_index(c)]for c in in_str]) / | |
(10 * len(in_str))) | |
# This script can either be used by calling `python file.py modelname' | |
# in which case the script will compute the score and terminate, | |
# or it can be called with no argument in which case it will loop | |
# and multiple model names can be proposed. | |
if len(sys.argv) > 1: | |
print(score("".join(sys.argv[1:]))) | |
else: | |
while True: | |
in_str = input("Enter a name to evaluate: ") | |
print("This scores ", score(in_str), " as a car model name.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is released under the .