Take two lists from the user (elements of the lists
separated by space) of
the same size. The first list will contain some names and the second list
will contain roll numbers. Your job is to assign each name
a roll number
from the list containing numbers following the criteria
given below:
Find the sum of the ASCII value of each name and then sort
the names in
descending order based on the sum. Then assign
lowest number to the
the
name that has the highest ASCII value sum as a roll number.
Sample Input 1:
Alan Jack Nancy Jim
10 15 20 50
Sample Output 1:
Nancy's roll number is: 10
Alan's roll number is: 15
Jack's roll number is: 20
Jim's roll number is: 50
# manual input
# names = [str(i) for i in input("Names: ").split(" ")]
# nums = [int(i) for i in input("Numbers: ").split(" ")]
# example
names = ['Anna', 'Aya', 'Boris', 'Maria', 'Denis']
nums = [111, 123, 189, 202, 115]
nums = sorted(nums)[::-1]
def get_ASCII(word):
sum = 0
for i in word:
sum += ord(i)
return sum
new_names = []
sort_names = {}
for i in names:
sort_names[i] = get_ASCII(i)
for i in sorted(sort_names.items(), key = lambda names: names[1]):
new_names.append(i[0])
def numbers(names, nums):
if len(names) != len(nums):
print("wrong length")
else:
for i in range(len(names)):
print(f"{names[i]} roll number is: {nums[i]}")
numbers(new_names, nums)
Comments
Leave a comment