Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Car model name score calculator based on XKCD #1571 : "Car Model Names"
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.")
@Syncrossus

This comment has been minimized.

Copy link
Owner Author

@Syncrossus Syncrossus commented May 27, 2019

This code is released under the WTFPL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.