Created
November 6, 2017 06:45
-
-
Save DagnyTagg2013/cfd5809aa262e986cabdd114d8ee8f05 to your computer and use it in GitHub Desktop.
build Friendships Adjacency List
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
# | |
# You have data for social network on who is friends with whom. | |
# You need to write a function that returns this data in the form of an adjacency list representation, i.e. a mapping of each employee ID to a list of his/her friends on the site | |
# # | |
# You have two input data sets to work with. The first data set is the employees at your company, and the second is all the pairs of employees who are virtually friends so far. It does not matter which employee's ID is in which column, the friendships are bidirectional. | |
# | |
# employees_input = [ | |
# "1,Richard,Engineering", | |
# "2,Erlich,HR", | |
# "3,Monica,Business", | |
# "4,Dinesh,Engineering", | |
# "6,Carla,Engineering", | |
# ] | |
# | |
# friendships_input = [ | |
# "1,2", | |
# "1,3", | |
# "2,4", | |
# ] | |
# | |
# Answers: (Format does not matter) | |
# 1: 2, 3 | |
# 2: 1, 4 | |
# 3: 1 | |
# 4: 2 | |
# 6: None | |
# | |
# ITER1: load key to LIST of TO friends from SRC friend | |
# ITER2: REvERSE-SWAP SRC DEST and do above | |
# ITER3: original friends list DRIVEs the total key entries! | |
# ATTN to what MATTERs: | |
# - NOT MATTER | |
# integer formatting conversion does NOT matter, as String char assoc is OK | |
# to ensure uniqueness | |
# - RESTATE PROBLEM | |
# - BREAKDOWN CASES => TDD! | |
# - NOT GENERALIZE if unecessary for direction | |
# code what you know you need first, then tweak for variations LATER | |
# - SPEED is to just LOOP code with indices swapped instead of parameterizing function direction for SRC and DEST -- or LOOP WITHOUT EXTRA function | |
# - SPEED is to use SHORTER variable names | |
# - ATTN for Python3 is not to do a get with None default and check isNone | |
# BUT INSTEAD need to check if X in Keys FIRST to PREVENT OVERWRITE of | |
# if it doesn't already exist | |
# -- OTHERWISE, read-access may throw a KEY error! | |
def loadFriendsBiDirection(directedFriendsPairs): | |
adjacentFriendsFromSrc = {} | |
for aPair in directedFriendsPairs: | |
# TRICK: parsout to good tuple element names instead of just tuple reference [0] [1] | |
src, dest= aPair.split(",") | |
# repeate mapping 2X, once for each direction; no separate function overhead | |
for i in range(0, 2): | |
# save src -> dest | |
if (src in adjacentFriendsFromSrc): | |
adjacentFriendsFromSrc[src].append(dest) | |
else: | |
adjacentFriendsFromSrc[src] = [ dest, ] | |
# TRICK: Python SWAP | |
# now, for BI-directional, SWAP dest as src, in OPPOSITE mapping! | |
src, dest = dest, src | |
return adjacentFriendsFromSrc | |
def fillPeopleWithoutFriends(allPeople, adjacentFriendsFromSrc): | |
for oneEmployee in employees_input: | |
empID, empName, empJob = oneEmployee.split(",") | |
if (empID in adjacentFriendsFromSrc): | |
# checking for not in is expensive, so just do pass, ELSE | |
pass | |
else: | |
# initialize to NO friends | |
adjacentFriendsFromSrc[empID] = [] | |
return adjacentFriendsFromSrc | |
# ******************** TDD DRIVER Script ************************* | |
input_directed_friendships = [ | |
"1,2", | |
"1,3", | |
"2,4", | |
] | |
employees_input = [ | |
"1,Richard,Engineering", | |
"2,Erlich,HR", | |
"3,Monica,Business", | |
"4,Dinesh,Engineering", | |
"6,Carla,Engineering", | |
] | |
# expected_adjacentFriendsFromSrc = | |
# 1: 2, 3 | |
# 2: 1, 4 | |
# 3: 1 | |
# 4: 2 | |
# 6: None | |
adjacentFriendsFromSrc = loadFriendsBiDirection(input_directed_friendships) | |
adjacentFriendsFromSrc = fillPeopleWithoutFriends(employees_input, | |
adjacentFriendsFromSrc) | |
print("FRIENDS FROM SRC: BI-DIRECTIONAL, and THOSE WITH NO FRIENDS!") | |
print(adjacentFriendsFromSrc) | |
# ************* ASIDE: String-parsing in Python3 and FP operators **************** | |
# GOTCHA in PYTHON 3: map, filter, etc return GENERATORS, NOT materialized Lists | |
# string-parsing tricks | |
print("\n\nPARSING TRICKS") | |
# - split to element | |
# - filter out None | |
# - convert to int | |
# - convert to tuple | |
directedPair = "1,2" | |
a = directedPair.split(',') | |
print(a) | |
print(a[0]) | |
print(a[1]) | |
src, dest = directedPair.split(',') | |
print(src) | |
print(dest) | |
print("FUNCTIONAL TRICKS") | |
lambda x: x % 2 | |
# https://stackoverflow.com/questions/13638898/how-to-use-filter-map-and-reduce-in-python-3 | |
# https://www.python-course.eu/python3_lambda.php | |
nums = [2,3,4,5] | |
odds = list(filter(lambda x: x % 2, nums)) | |
print(odds) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment