From 82cd983d5ef290265a2dcdb920d01091cfd1071b Mon Sep 17 00:00:00 2001 From: Christina Date: Sat, 21 Nov 2020 21:29:26 +0000 Subject: [PATCH 1/2] Exercise 10 --- group.py | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/group.py b/group.py index e2ec347..711e21d 100644 --- a/group.py +++ b/group.py @@ -1,5 +1,44 @@ -"""An example of how to represent a group of acquaintances in Python.""" +# Using the example code from the lecture -# Your code to go here... +group = { + "Jill": { + "age": 26, + "job": "biologist", + "relations": { + "Zalika": "friend", + "John": "partner" + } + }, + "Zalika": { + "age": 28, + "job": "artist", + "relations": { + "Jill": "friend" + } + }, + "John": { + "age": 27, + "job": "writer", + "relations": { + "Jill": "partner" + } + }, + "Nash": { + "age": 34, + "job": "chef", + "relations": { + "John": "cousin", + "Zalika": "landlord" + } + } +} -my_group = +def mean(data): + """Compute the mean of a non-empty list of numbers.""" + return sum(data) / len(data) + + +print(max(person["age"] for person in group.values())) +print(mean([len(person["relations"]) for person in group.values()])) +print(max(person["age"] for person in group.values() if person["relations"])) +print(max(person["age"] for person in group.values() if "friend" in person["relations"].values())) \ No newline at end of file From e36fb287c84cf2b9b50302dc612730c3e7f4c0c3 Mon Sep 17 00:00:00 2001 From: Christina Date: Sun, 22 Nov 2020 10:20:14 +0000 Subject: [PATCH 2/2] HW3 json --- group.py | 12 +++++++++++- group_file.json | 0 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 group_file.json diff --git a/group.py b/group.py index 711e21d..a186825 100644 --- a/group.py +++ b/group.py @@ -41,4 +41,14 @@ def mean(data): print(max(person["age"] for person in group.values())) print(mean([len(person["relations"]) for person in group.values()])) print(max(person["age"] for person in group.values() if person["relations"])) -print(max(person["age"] for person in group.values() if "friend" in person["relations"].values())) \ No newline at end of file +print(max(person["age"] for person in group.values() if "friend" in person["relations"].values())) + +import json + +#write file +with open('group_file.json', 'w') as json_file: + json.dumps(group, json_file, indent=4) + +#read file +with open('group_file.json', 'r') as json_file: + group_data = json_file.read() \ No newline at end of file diff --git a/group_file.json b/group_file.json new file mode 100644 index 0000000..e69de29