Issue
I am trying to create function that takes an input name and outputs a rank based on the order of the letters it has for example a=1
b=2
name = ab
rank = 3
import string
x = "richard"
y = "donald"
c = "Sam"
numbers = []
for i in range(1,27):
numbers.append(i)
print(numbers)
alphabet_string = string.ascii_lowercase
alphabet_list = list(alphabet_string)
print(alphabet_list)
new_x = list(x)
new_y = list(y)
new_c = list(c)
zip_iterators = zip(alphabet_list,numbers)
dic = list(zip_iterators)
print(dic)
def rank(name):
rank = 0
for l in range(0,len(name)):
for k,v in dic:
if l == k:
v += rank
print(rank)
rank(new_c)
but I failed so far
Solution
letter_rank = {letter:rank for rank, letter in enumerate(string.ascii_lowercase, 1)}
def rank(name):
return sum(letter_rank.get(c, 0) for c in name.lower())
Answered By - Marat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.