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
public static Boolean valueOf(boolean b) { | |
return (b ? Boolean.TRUE : Boolean.FALSE); | |
} |
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
public class Person { | |
private String lastName; // required | |
private String firstName; // required | |
private String middleName; // required | |
private int bornYear; // required | |
private boolean isMale; // optional | |
private boolean isEmployed; // optional | |
private boolean isMarried; // optional |
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
# Initialize the dict with an empty list for each expert id: | |
friendships = {experts["id"]: [] for user in users} | |
# And loop over the friendship pairs to populate it: | |
for i, j in friendship_pairs: | |
friendships[i].append(j) # Add j as a friend of expert i | |
friendships[j].append(i) # Add i as a friend of expert j |
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
# Initialize the dict with an empty list for each expert id: | |
friendships = {expert["id"]: [] for expert in experts} | |
# And loop over the friendship pairs to populate it: | |
# {0: [1, 2], 1: [0, 2, 3], 2: [0, 1, 3], 3: [1, 2, 4], 4: [3, 5], 5: [4, 6, 7], 6: [5, 8], 7: [5, 8], 8: [6, 7, 9], 9: [8]} | |
for i, j in friendship_pairs: | |
friendships[i].append(j) # Add j as a friend of expert i | |
friendships[j].append(i) # Add i as a friend of expert j |
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
def number_of_friends(expert): | |
expert_id = expert["id"] | |
friend_ids = friendships[expert_id] | |
return len(friend_ids) | |
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
experts = [ | |
{"id": 0, "name": "Bill Gates"}, | |
{"id": 1, "name": "Mark Zuckerberg"}, | |
{"id": 2, "name": "Steve Jobs"}, | |
{"id": 3, "name": "Jeff Bezos"}, | |
{"id": 4, "name": "Ha Dong Nguyen"}, | |
{"id": 5, "name": "Trung Anh Dang"}, | |
{"id": 6, "name": "Sundar Pichai"}, | |
{"id": 7, "name": "Larry Page"}, | |
{"id": 8, "name": "Sergey Brin"}, |
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
# 24 | |
total_connections = sum(number_of_friends(expert) for expert in experts) | |
# length of the users list | |
num_experts = len(experts) | |
# 24 / 10 == 2.4 | |
avg_connections = total_connections / num_experts |
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
# Create a list (user_id, number_of_friends). | |
num_friends_by_id = [(expert["id"], number_of_friends(expert)) | |
for expert in experts] | |
# Sort the list | |
num_friends_by_id.sort( | |
key=lambda id_and_friends: id_and_friends[1], # by num_friends | |
reverse=True) # largest to smallest | |
# Each pair is (expert_id, num_friends): |
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
# foaf is short for "friend of a friend" | |
def foaf_ids_bad(expert): | |
return [foaf_id | |
for friend_id in friendships[expert["id"]] | |
for foaf_id in friendships[friend_id]] | |
print(foaf_ids_bad(experts[1])) |
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
def friends_of_friends(expert): | |
expert_id = expert["id"] | |
return Counter( | |
foaf_id | |
# For each of my friends, | |
for friend_id in friendships[expert_id] | |
# find their friends | |
for foaf_id in friendships[friend_id] | |
# who aren't me | |
if foaf_id != expert_id |
OlderNewer