Advertisement
mbratanov

03. Naughty or Nice_

Oct 12th, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. def sort_kids(kids_list, categories, collection, by_name=False):
  2.     for el in collection:
  3.         if by_name:
  4.             criteria, behavior = el
  5.             matching_kids = [kid for kid in kids_list if kid[1] == criteria]
  6.         else:
  7.             criteria, behavior = el.split('-')
  8.             matching_kids = [kid for kid in kids_list if str(kid[0]) == criteria]
  9.  
  10.         if len(matching_kids) == 1:
  11.             kid = matching_kids[0]
  12.             categories[behavior].append(kid[1])
  13.             kids_list.remove(kid)
  14.  
  15.  
  16. def output(categories):
  17.     result = []
  18.     for cat, kids in categories.items():
  19.         if kids:
  20.             result.append(f"{cat}: {', '.join(kids)}")
  21.     return "\n".join(result)
  22.  
  23.  
  24. def naughty_or_nice_list(kids_list, *commands, **keywords):
  25.     sorted_by_behavior = {
  26.         "Nice": [],
  27.         "Naughty": [],
  28.         "Not found": []
  29.     }
  30.  
  31.     sort_kids(kids_list, sorted_by_behavior, commands)
  32.  
  33.     sort_kids(kids_list, sorted_by_behavior, keywords.items(), by_name=True)
  34.  
  35.     for kid in kids_list:
  36.         sorted_by_behavior["Not found"].append(kid[1])
  37.  
  38.     return output(sorted_by_behavior)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement