From c440c5fd67fbd627f237fc5827fab6408cf57099 Mon Sep 17 00:00:00 2001 From: Marcell Kovacs Date: Thu, 20 Oct 2022 10:31:50 +0100 Subject: [PATCH 1/4] Adding test comment --- group.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group.py b/group.py index e2ec347..dc94327 100644 --- a/group.py +++ b/group.py @@ -1,5 +1,5 @@ """An example of how to represent a group of acquaintances in Python.""" - +# Testing push/pull # Your code to go here... my_group = From 6c746d956c8cf596dcfb2397d456b3f0e60bbf06 Mon Sep 17 00:00:00 2001 From: Marcell Kovacs Date: Thu, 20 Oct 2022 10:56:15 +0100 Subject: [PATCH 2/4] Constructing dictionary for friend group --- group.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/group.py b/group.py index dc94327..7e0d212 100644 --- a/group.py +++ b/group.py @@ -2,4 +2,16 @@ # Testing push/pull # Your code to go here... -my_group = +my_group = { + # (name, age) as key + # ["job", list of relations as tuples (name, relation to name)] as value + ("Jill", 26):["biologist", ("John", "partner"), ("Zalika", "friend")], + ("Zalika", 28):["artist", ("Jill", "friend")], + ("John", 27):["writer", ("Jill":"partner")], + ("Nash", 34):["cook", ("John", "cousin"), ("Zalika":"landlord")] +} + +# Flexibly dataset to add people with various relations +# Difficulty in searching +# It allows people with no connections +# Doesn't assume reciprocal relations \ No newline at end of file From c427c987e25c923bb7a0acab7565f636fd7b43e0 Mon Sep 17 00:00:00 2001 From: Marcell Kovacs Date: Tue, 25 Oct 2022 15:20:53 +0100 Subject: [PATCH 3/4] Solving homework exercises. --- group.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/group.py b/group.py index 7e0d212..c6ca97c 100644 --- a/group.py +++ b/group.py @@ -7,11 +7,73 @@ # ["job", list of relations as tuples (name, relation to name)] as value ("Jill", 26):["biologist", ("John", "partner"), ("Zalika", "friend")], ("Zalika", 28):["artist", ("Jill", "friend")], - ("John", 27):["writer", ("Jill":"partner")], - ("Nash", 34):["cook", ("John", "cousin"), ("Zalika":"landlord")] + ("John", 27):["writer", ("Jill","partner")], + ("Nash", 34):["cook", ("John", "cousin"), ("Zalika","landlord")] } # Flexibly dataset to add people with various relations # Difficulty in searching # It allows people with no connections -# Doesn't assume reciprocal relations \ No newline at end of file +# Doesn't assume reciprocal relations + +# Find maximum age +max_age, max_age_person = None, None +people = [x[0] for x in my_group.keys()] +ages = [x[1] for x in my_group.keys()] +max_age = max(ages) +max_age_people = people[ages.index(max_age)] + +print('Find maximum age') +print(max_age, max_age_people) + +# Find mean no. connections in the group +mean_no_connections = None +no_connections = [len(x[1:]) for x in my_group.values()] +mean_no_connections = sum(no_connections)/len(my_group.keys()) +print('Find mean no. connections') +print(mean_no_connections) + +# Find maximum age with at least one relation + +max_age2, max_age_person2 = None, None + +# Find people with at least one connection +people_conditioned = [person for person in people if no_connections[people.index(person)]>0] +# Filter ages according to one-connection conditions +ages_conditioned = [age[1] for age in my_group.keys() if age[0] in people_conditioned ] +#print(people_conditioned, ages_conditioned) +max_age2 = max(ages_conditioned) +max_age_person2 = people[ages_conditioned.index(max_age2)] +print('Find people with at least one relation') +print(max_age2, max_age_person2) + +# Find maximum age with at least one relation + +max_age3, max_age_person3 = None, None + +# Find relations for each person +relations = [my_group[person][1:] for person in my_group.keys()] +#print(relations) + +# Find no_friends +no_friends = relations +for index, relation in enumerate(relations): + no_friends[relations.index(relation)] = 0 + for relation_sub in relation: + if relation_sub[1] == 'friend': + no_friends[index] += 1 + +#print(no_friends) +# Find people with at least one friendship + +people_conditioned2 = [person for person in people if no_friends[people.index(person)]>0] + +# Filter ages according to one-friendship conditions + +ages_conditioned2 = [age[1] for age in my_group.keys() if age[0] in people_conditioned2 ] + +max_age3 = max(ages_conditioned2) +max_age_person3 = people[ages_conditioned2.index(max_age3)] +print('Find people with at least one friend') +print(max_age3, max_age_person3) + From 303702dbec498832ca3a3a5ea0c1736ce5070acc Mon Sep 17 00:00:00 2001 From: Marcell Kovacs Date: Tue, 25 Oct 2022 17:11:00 +0100 Subject: [PATCH 4/4] Saving solution file --- group-solution.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 group-solution.py diff --git a/group-solution.py b/group-solution.py new file mode 100644 index 0000000..5fe16e5 --- /dev/null +++ b/group-solution.py @@ -0,0 +1,50 @@ +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" + } + } +} + +# Saving structure to JSON +import json + +print(json.dumps(group)) + +with open('group-data.json', 'w') as output: + output.write(json.dumps(group)) + +# Reading json +group_reload = None +with open('group-data.json', 'r') as input: + group_reload = input.read() + +group_reload = json.loads(group_reload) +print(group_reload) +print(type(group_reload)) +assert group_reload == group \ No newline at end of file