-
Notifications
You must be signed in to change notification settings - Fork 0
/
ancestral_names.py
30 lines (24 loc) · 925 Bytes
/
ancestral_names.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def convert(s):
s = s[::-1]
# the first index is value, the second index are for conditions purpose
dic = {'I': [1,0], 'V': [5,1], 'X': [10,2], 'L': [50,3], 'C': [100,4], 'D': [500,5], 'M': [1000,6]}
result = dic[s[0]][0]
for i in range(len(s) - 1):
if dic[s[i]][1] > dic[s[i+1]][1]:
result -= dic[s[i+1]][0]
else:
result += dic[s[i+1]][0]
return result
def sortRoman(names):
mapped = []
for raw in names:
name, num = raw.split()
num = convert(num) # this is from leetcode
mapped.append((name ,num, raw))
mapped.sort(key= lambda x: (x[0],x[1]))
# print(mapped, sorted(mapped))
return [roman for normal,num,roman in mapped]
# sortRoman(["Loius IX", "Louis VIII"])
print(sortRoman(["Philippe I", "Philip II"]))
print(sortRoman(["Louis IX", "Louis VIII"]))
print(sortRoman(["Francois VI","Francois XX"]))