This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
Adding changes that print out the requested values #61
Open
i-gayo
wants to merge
8
commits into
UCL-RITS:main
Choose a base branch
from
swishswish123:Iani
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3edd2ea
first commit
enkaoua 10d9d1b
Added connection function. Still to add: 2-way relationship
enkaoua fdf5ff6
Commenting out changes
i-gayo 5dcb5b8
Attempt 2 just using dictionary of dictionaries
i-gayo fa1cc07
Printing out values from group
i-gayo c74ca15
Printed additional info
i-gayo be6eb94
Adding json files
i-gayo db9e8c9
Correcting comprehensions
i-gayo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,116 @@ | ||
"""An example of how to represent a group of acquaintances in Python.""" | ||
|
||
# ATTEMPT 1: USING CLASSES BUT IT GOT TOO COMPLICATED WITH SELF | ||
""" | ||
#Clearing out variables | ||
jill = [] | ||
zalika = [] | ||
|
||
# Your code to go here... | ||
class Person: | ||
def __init__(self, name, age, job, connection= [] ): | ||
self.name = name | ||
self.age = age | ||
self.job =job | ||
self.connection = connection | ||
|
||
def add_connection(self, friend, relationship): | ||
#Problem: This also updates the connection of the friend using self of person and person's friend | ||
#self.connection.update({friend.name: relationship}) | ||
self.connection.append({friend.name : relationship}) | ||
|
||
|
||
jill=Person('Jill', 26, 'biologist') | ||
zalika = Person('Zalika', 28, 'artist') | ||
john=Person('John', 27, 'writer') | ||
#nash=Person('Nash', 34, 'chef') | ||
|
||
jill.add_connection(zalika, 'friend') | ||
jill.add_connection(john, 'partner') | ||
print("Jill's connection: ", jill.connection) | ||
zalika.add_connection(jill, 'friend') | ||
print("Zalika's connection: ", zalika.connection) | ||
""" | ||
|
||
# ATTEMPT 2: Using dictionary of dictionaries | ||
|
||
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" | ||
} | ||
} | ||
} | ||
|
||
#Unpacking things in nested dictionary of dictionaries : member name is string, member is actual dictionary value with its items | ||
[print(member['age']) for member_name, member in group.items()] | ||
|
||
# 1. Max age of people in the group | ||
#Not proper comprehensions --> Comprehensions make a list already | ||
#ages = [] | ||
#[ages.append(member['age']) for member_name, member in group.items()] | ||
|
||
#Making use of proper comprehensions | ||
ages = [person["age"] for person in group.values()] | ||
print("Maximum age of group: ", max(ages)) | ||
|
||
# 2. Average (mean) number of relations among members of the group | ||
no_relations = [len(member['relations']) for member_name, member in group.items()] | ||
avg_relations = sum(no_relations) / len(no_relations) | ||
print("Average number of relations: " , avg_relations) | ||
|
||
# 3. The maximum age of people in the group that have at least one relation | ||
ages_1relation = [member['age'] for member_name, member in group.items() if len(member['relations']) > 0 ] | ||
print("Max age of people in the group that have at least one relation: ", max(ages_1relation)) | ||
|
||
#4. Maximum age of people in the group that have at least one friend | ||
print('friend' in group['Jill']['relations'].values()) #Test if friend is in relations.values | ||
ages_wfriends = [member['age'] for member_name, member in group.items() if ('friend' in member['relations'].values())] | ||
print('Maximum age of people w/at least one friend: ', max(ages_wfriends)) | ||
|
||
""" Working with JSON files """ | ||
import json | ||
|
||
# Writing group dictionary to json file format | ||
with open('group.json', 'w') as json_file: | ||
|
||
#Writes dictionary group into json format to be placed inside file : ie string | ||
json_format = json.dumps(group, indent = 3) | ||
|
||
#Writes json formatted text to the json file | ||
json_file.write(json_format) #or could've done: json_file.write(json.dumps(group, indent =3 ) | ||
#Reading json file | ||
with open('group.json', 'r') as json_file_read: | ||
string_data = json_file_read.read() | ||
#print(string_data) | ||
|
||
# Turning the read data into json format | ||
group_read = json.loads(string_data) | ||
|
||
my_group = | ||
#Access jill contents | ||
#print(group_read['Jill']) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, you could also make a function that calculates the mean from a list and use that on the output of the list