Advertisement
horozov86

Words Sorting

Jun 12th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. def words_sorting(*args):
  2.     dictionary = {}
  3.     total_sum = 0
  4.     sum_letters = 0
  5.     for word in args:
  6.         for letter in word:
  7.             sum_letters += ord(letter)
  8.             dictionary[word] = sum_letters
  9.         total_sum += sum_letters
  10.         sum_letters = 0
  11.     result = ''
  12.     if total_sum % 2 == 0:
  13.         sorted_dictionry = sorted(dictionary.items(), key=lambda x: x[0])
  14.         for el in sorted_dictionry:
  15.             result += el
  16.         return result
  17.    
  18.     else:
  19.         sorted_dictionry = sorted(dictionary.items(), key=lambda x: x[1])
  20.         for el in sorted_dictionry:
  21.             result += el
  22.         return result
  23.  
  24.  
  25. print(
  26.     words_sorting(
  27.         'escape',
  28.         'charm',
  29.         'mythology'
  30.   ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement