Advertisement
viligen

task_3

Feb 19th, 2022
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. def start_spring(**kwargs):
  2.     result = []
  3.     fl_by_type = {}
  4.     for flower_, type_ in kwargs.items():
  5.         if type_ not in fl_by_type:
  6.             fl_by_type[type_] = []
  7.         fl_by_type[type_].append(flower_)
  8.     sorted_types = sorted(fl_by_type.items(), key=lambda kvp: (-len(kvp[1]), kvp[0]))
  9.     for types, flowers in sorted_types:
  10.         result.append(f'{types}:\n')
  11.        
  12.         result.append('\n'.join('-' + fl for fl in sorted(flowers)) + '\n')
  13.     return ''.join(result).strip()
  14.  
  15.  
  16. example_objects = {"Water Lilly": "flower",
  17.                    "Swifts": "bird",
  18.                    "Callery Pear": "tree",
  19.                    "Swallows": "bird",
  20.                    "Dahlia": "flower",
  21.                    "Tulip": "flower",}
  22. print(start_spring(**example_objects))
  23. example_objects = {"Swallow": "bird",
  24.                    "Thrushes": "bird",
  25.                    "Woodpeckers": "bird",
  26.                    "Swallows": "bird",
  27.                    "Warblers": "bird",
  28.                    "Shrikes": "bird",}
  29. print(start_spring(**example_objects))
  30. example_objects = {"Magnolia": "tree",
  31.                    "Swallow": "bird",
  32.                    "Thrushes": "bird",
  33.                    "Pear": "tree",
  34.                    "Cherries": "tree",
  35.                    "Shrikes": "bird",
  36.                    "Butterfly": "insect"}
  37. print(start_spring(**example_objects))
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement