Reputation: 171
I have input that provides a number and a sequence of distinct letters (upper/lowercase).
The program generates a dictionary for provided sequence of letters and the number should be converted using into letters using values from dictionary.
For example, I have following input:
Convert 49036 using fFeEdDcCbBaA.
The dictionary created for this sequence of letters is:
numberkey = {500000: 'f', 100000: 'F', 50000: 'e', 10000: 'E', 5000: 'd', 1000: 'D', 500: 'c', 100: 'C', 50: 'b', 10: 'B', 5: 'a', 1: 'A'}
The output of the conversion should be this:
EeDEBBBaA
Roman number conversion rules applies.
The output I'm getting so far is not correct:
EEEEdDDDDBBBaA
Would appreciate any help. Thanks.
Upvotes: 1
Views: 264
Reputation: 8111
Here's a simple and generalised approach, based on the Roman Numeral concept.
n = 49036
numberkey = {500000: 'f', 100000: 'F', 50000: 'e', 10000: 'E', 5000: 'd', 1000: 'D', 500: 'c', 100: 'C', 50: 'b', 10: 'B', 5: 'a', 1: 'A'}
x = list(numberkey.items())
x.sort()
new_base = [] #to get special symbols for 4's and 9's
for i in range(len(x)-1):
if x[i][0] < (x[i+1][0]/2):
new_base.append((x[i+1][0]-x[i][0], x[i][1]+x[i+1][1]))
if (str(x[i][0]).find("10")==0) and numberkey.get(x[i][0]//10, None):
num = numberkey[x[i][0]//10]
new_base.append((x[i][0]-(x[i][0]//10), num+x[i][1]))
x += new_base
x.sort()
ans = ""
i = len(x)-1
while n and i>=0:
count = n//x[i][0]
if count:
ans += count*x[i][1]
n -= count*x[i][0]
i -= 1
print(and) #EeDEBBBaA
Upvotes: 1