Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Homework Solutions: Added Comprehension Expressions #84

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Jill": {"age": 26, "job": "biologist", "connections": {"Zalika": "friend", "John": "partner"}}, "Zalika": {"age": 28, "job": "artist", "connections": {"Jill": "friend"}}, "John": {"age": 27, "job": "writer", "connections": {"Jill": "partner", "Nash": "cousin"}}, "Nash": {"age": 34, "job": "chef", "connections": {"John": "cousin", "Zalika": "landord"}}}
81 changes: 80 additions & 1 deletion group.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,84 @@
"""An example of how to represent a group of acquaintances in Python."""

# use dictionaries
# use lists

#person keys: name, age, job, connections to others


#Jill is 26, a biologist and she is Zalika's friend and John's partner.
#Zalika is 28, an artist, and Jill's friend
#John is 27, a writer, and Jill's partner.
#Nash is 34, a chef, John's cousin and Zalika's landlord.

#should allow people to have no job
#should allow people to have no connections
#reciprocal connections?

# Your code to go here...

my_group =
my_group = {
'Jill': {
'age': 26,
'job': 'biologist',
'connections': {
'Zalika': 'friend',
'John': 'partner',
}
},

'Zalika': {
'age': 28,
'job': 'artist',
'connections': {
'Jill': 'friend'
}
},

'John': {
'age': 27,
'job': 'writer',
'connections': {
'Jill': 'partner',
'Nash': 'cousin'
}
},

'Nash': {
'age': 34,
'job': 'chef',
'connections': {
'John': 'cousin',
'Zalika': 'landord'
}
}
}


# function to compute mean of non-empty list of numbers
def mean(data):
return sum(data)/len(data)

# max age of people in group
print(max(person['age'] for person in my_group.values())) #34

# average number of relations among members of the group
print(mean([len(person['connections'])for person in my_group.values()])) #1.75

# max age of people in the group that have min 1 relation
print(max(person['age'] for person in my_group.values() if person['connections'])) #34

# max age of people in group with min 1 friend
print(max(person['age'] for person in my_group.values() if 'friend' in person['connections'].values())) #28

## saving group dictionary as JSON file

import json

with open('group.json', 'w') as json_group_out:
json_group_out.write(json.dumps(my_group))

with open('group.json') as json_group_in:
group_again = json.load(json_group_in)

print(group_again)