python: def character_dict(prof_dict): """ Question 2 - Given a dictionary that maps a character to a list of professors they want to talk to, return a dictionary with the value being the list sorted by the last letter in each professors' last name. - If two professors have the same last letter of their last name, sort by the first letter of their first name. - THIS MUST BE DONE IN ONE LINE
python:
def character_dict(prof_dict):
"""
Question 2
- Given a dictionary that maps a character to a list of professors they want to
talk to, return a dictionary with
the value being the list sorted by the last letter in each professors' last
name.
- If two professors have the same last letter of their last name, sort by the
first letter of their first name.
- THIS MUST BE DONE IN ONE LINE
Args:
prof_dict (dict)
Returns:
dict
>>> character_dict({"Harry": ["Albus Dumbledore", "Minerva McGonagall",
"Severus Snape", "Rubeus Hagrid"], "Hermione": ["Remus Lupin", "Alastor Moody",
"Horace Slughorn"]})
{'Harry': ['Rubeus Hagrid', 'Albus Dumbledore', 'Severus Snape', 'Minerva
McGonagall'], 'Hermione': ['Horace Slughorn', 'Remus Lupin', 'Alastor Moody']}
>>> character_dict({"Scorpius": ["Severus Snape", "Dolores Umbridge", "Horace
Slughorn"], "Neville": ["Cuthbert Binns", "Rubeus Hagrid", "Minerva McGonagall"]})
{'Scorpius': ['Dolores Umbridge', 'Severus Snape', 'Horace Slughorn'],
'Neville': ['Rubeus Hagrid', 'Minerva McGonagall', 'Cuthbert Binns']}
"""
print(character_dict({"Harry": ["Albus Dumbledore", "Minerva McGonagall",
"Severus Snape", "Rubeus Hagrid"], "Hermione": ["Remus Lupin", "Alastor Moody",
"Horace Slughorn"]}))
print(character_dict({"Scorpius": ["Severus Snape", "Dolores Umbridge",
"Horace Slughorn"], "Neville": ["Cuthbert Binns", "Rubeus Hagrid", "Minerva
McGonagall"]}))
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images
python:
def character_gryffindor(character_list):
"""
Question 1
You are given a list of characters in Harry Potter.
Imagine you are Minerva McGonagall, and you need to pick the students from your
own house, which is Gryffindor, from the list.
To do so ...
- THIS MUST BE DONE IN ONE LINE
- First, remove the duplicate names in the list provided
- Then, remove the students that are not in Gryffindor
- Finally, sort the list of students by their first name
- Don't forget to return the resulting list of names!
Args:
character_list (list)
Returns:
list
>>> character_gryffindor(["Scorpius Malfoy, Slytherin", "Harry Potter,
Gryffindor", "Cedric Diggory, Hufflepuff", "Ronald Weasley, Gryffindor", "Luna
Lovegood, Ravenclaw"])
['Harry Potter, Gryffindor', 'Ronald Weasley, Gryffindor']
>>> character_gryffindor(["Hermione Granger, Gryffindor", "Hermione Granger,
Gryffindor", "Cedric Diggory, Hufflepuff", "Sirius Black, Gryffindor", "James
Potter, Gryffindor"])
['Hermione Granger, Gryffindor', 'James Potter, Gryffindor', 'Sirius Black,
Gryffindor']
"""
print(character_gryffindor(["Scorpius Malfoy, Slytherin", "Harry Potter,
Gryffindor", "Cedric Diggory, Hufflepuff", "Ronald Weasley, Gryffindor", "Luna
Lovegood, Ravenclaw"]))
print(character_gryffindor(["Hermione Granger, Gryffindor", "Hermione
Granger, Gryffindor", "Cedric Diggory, Hufflepuff", "Sirius Black, Gryffindor",
"James Potter, Gryffindor"]))