Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am trying to create a function that inputs a name and outputs a rank. what is the best way to create that function?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading