Created
September 3, 2015 05:40
-
-
Save hugovk/46525f8043316b1dbb63 to your computer and use it in GitHub Desktop.
Convert the letters of your full name into numbers, add them together, & determine the prime factorization of the sum
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
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
Today's challenge: | |
Convert the letters of your full name into numbers, add them together, | |
& determine the prime factorization of the sum | |
https://twitter.com/todayschallenge/status/638713672278351875 | |
https://twitter.com/todayschallenge/status/639075809060356096 | |
""" | |
from __future__ import print_function, unicode_literals | |
import argparse | |
# How many points does each letter have (in the font in this text editor)? | |
NONE = "BDO" | |
ONE = "Q" | |
TWO = "ACGJLMNPRSUVWZ" | |
THREE = "EFTY" | |
FOUR = "HIKX" | |
def letters_into_numbers(words): | |
sum = 0 | |
for c in words.upper(): | |
if c in NONE: | |
sum += 0 | |
elif c in ONE: | |
sum += 1 | |
elif c in TWO: | |
sum += 2 | |
elif c in THREE: | |
sum += 3 | |
elif c in FOUR: | |
sum += 4 | |
print("Sum = ", sum) | |
return sum | |
def prime_factorisation(number): | |
# http://www.calculatorsoup.com/calculators/math/prime-factors.php | |
saved = [] | |
divider = 2 | |
while(True): | |
result = number / float(divider) | |
print(number, "/", divider, "=", result, " integer:", | |
result.is_integer()) | |
if result.is_integer(): | |
saved.append(divider) | |
print("Save:", divider) | |
number = result | |
else: | |
divider += 1 | |
if result == 1: | |
break | |
print("Prime factorisation:", saved) | |
return saved | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser( | |
description="Today's challenge: Convert the letters of your full name " | |
"into numbers, add them together, & determine the prime factorization " | |
"of the sum", | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument('name', help="A name") | |
args = parser.parse_args() | |
sum = letters_into_numbers(args.name) | |
result = prime_factorisation(sum) | |
# End of file |
Author
hugovk
commented
Sep 3, 2015
$ python todayschallenge.py "Today's Challenge"
Sum = 32
32 / 2 = 16.0 integer: True
Save: 2
16.0 / 2 = 8.0 integer: True
Save: 2
8.0 / 2 = 4.0 integer: True
Save: 2
4.0 / 2 = 2.0 integer: True
Save: 2
2.0 / 2 = 1.0 integer: True
Save: 2
Prime factorisation: [2, 2, 2, 2, 2]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment