In: Computer Science
THE CODE MUST BE PYTHON
superHeroes = {
"MoleculeMan": {
"age": 29,
"secretIdentity": "Dan Jukes",
"superpowers": [
"Radiation Immunity",
"Turning tiny",
"Radiation blast"
]
},
"MadameUppercut": {
"age": 39,
"secretIdentity": "Jane Wilson",
"superpowers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
},
"EternalFlame": {
"age": 1000,
"secretIdentity": "Unknown",
"superpowers": [
"Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"
]
},
"Doomguy": {
"age": 27,
"secretIdentity": "Space Trooper",
"superpowers": [
"Immortality",
"Heat Immunity",
"Teleportation",
"Super Speed",
"Superhuman reflexes"
]
},
"Maui":{
"age": 2352,
"secretIdentity": "Unknown",
"superpowers": [
"Immortality",
"Super Speed"
]
},
"Superwoman":{
"age": 1167,
"secretIdentity": "Lois Lane",
"superpowers": [
"Immortality",
"Super Speed",
"Superhuman reflexes"
]
} ,
}
# Question 2 (5 points)
# Output a set containing the ages of all the superheroes.
# Output the name and the age of the youngest superhero.
# Sample output is below.
Set of all ages: {39, 1000, 1167, 2352, 27, 29}
Doomguy is 27 years old.
Here I have implemented python3 code which is print the output as per your given format. also, I have attached a screenshot of the output. please let me know in the comment section if you have any queries regarding the below code.
import sys
# Given dictionary
superHeroes = {
"MoleculeMan": {
"age": 29,
"secretIdentity": "Dan Jukes",
"superpowers": [
"Radiation Immunity",
"Turning tiny",
"Radiation blast"
]
},
"MadameUppercut": {
"age": 39,
"secretIdentity": "Jane Wilson",
"superpowers": [
"Million tonne punch",
"Damage resistance",
"Superhuman reflexes"
]
},
"EternalFlame": {
"age": 1000,
"secretIdentity": "Unknown",
"superpowers": [
"Immortality",
"Heat Immunity",
"Inferno",
"Teleportation",
"Interdimensional travel"
]
},
"Doomguy": {
"age": 27,
"secretIdentity": "Space Trooper",
"superpowers": [
"Immortality",
"Heat Immunity",
"Teleportation",
"Super Speed",
"Superhuman reflexes"
]
},
"Maui":{
"age": 2352,
"secretIdentity": "Unknown",
"superpowers": [
"Immortality",
"Super Speed"
]
},
"Superwoman":{
"age": 1167,
"secretIdentity": "Lois Lane",
"superpowers": [
"Immortality",
"Super Speed",
"Superhuman reflexes"
]
} ,
}
# initialize with maxvalue
min_age=sys.maxsize
# Name of minimum age superHero
name=''
# set which stores all superHeroes age
age=set()
# loop over the whole dictionary
for i in superHeroes:
# current index superHero's age
tmp=superHeroes[i]['age']
# add age into a set of age
age.add(tmp)
# find minimum age superHero
if tmp<min_age:
min_age=tmp
name=i
# print output as per given format
print('Set of all ages:',age)
print('{} is {} years old.'.format(name,min_age))
Output: