Created
July 13, 2013 21:24
-
-
Save Caleb2501/5992273 to your computer and use it in GitHub Desktop.
Yay challenge 31 completed! This one was a lot of fun. I liked the way my code turned out too. I think it looks quite clean, and I used functions that call other functions. Pretty nifty.
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
# Write a function that takes two base-26 numbers in which digits are represented by letters with | |
# A=0, B=1, Z=25 and returns their product using the same notation. As an example, CSGHJ x CBA = FNEUZJA. | |
# Your task is to write the base-26 multiplication function. | |
base26 = {'A':0, 'B':1, 'C':2, 'D':3, 'E':4, 'F':5, 'G':6, 'H':7, 'I':8, 'J':9, 'K':10, 'L':11, 'M':12, | |
'N':13, 'O':14, 'P':15, 'Q':16, 'R':17, 'S':18, 'T':19, 'U':20, 'V':21, 'W':22, 'X':23, 'Y':24, 'Z':25} | |
base26rev = {0:'A', 1:'B', 2:'C', 3:'D', 4:'E', 5:'F', 6:'G', 7:'H', 8:'I', 9:'J', 10:'K', 11:'L', 12:'M', | |
13:'N', 14:'O', 15:'P', 16:'Q', 17:'R', 18:'S', 19:'T', 20:'U', 21:'V', 22:'W', 23:'X', 24:'Y', 25:'Z'} | |
def convert26(string): | |
"""takes a string and returns the equivalent base 26 integer.""" | |
string = string[::-1] | |
power = 0 | |
total = 0 | |
for i in string: | |
total += base26[i]*(26**power) | |
power += 1 | |
return total | |
def multiply(set1, set2): | |
""" Takes two strings converts them to base 26 equivalents and multiplies the two together. | |
Then returns an integer total. """ | |
num1 = convert26(set1) | |
num2 = convert26(set2) | |
total = num1 * num2 | |
return total | |
def revert26(number): | |
"""converts an integer to base 26 string equivalent and returns that string. """ | |
revertNum = "" | |
while number > 0: | |
remain = number % 26 | |
revertNum = base26rev[remain] + revertNum | |
number = (number - remain) / 26 | |
return revertNum | |
print "Enter some letters to multiply at base 26:" | |
first = raw_input("set 1 >> ") | |
second = raw_input("set 2 >> ") | |
first = first.upper() | |
second = second.upper() | |
print "Thank you...thinking..." | |
print "Those letters represent numbers in base 26: " | |
print "set 1: %d and set 2: %d" % (convert26(first), convert26(second)) | |
print "Multiplying these together we get:" | |
print "%s X %s = %s" % (first, second, revert26(multiply(first, second))) | |
print "Or, as viewed in base 10:" | |
print "%d X %d = %d" % (convert26(first), convert26(second), multiply(first, second)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment