Skip to content

Commit

Permalink
block, follow_unfollow_ find_online
Browse files Browse the repository at this point in the history
  • Loading branch information
shahabhm committed Jul 4, 2021
1 parent 37a71a2 commit 231b7ee
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 27 deletions.
11 changes: 4 additions & 7 deletions account.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ def __init__(self, data: str):
self.id = int(data[0])
self.name = data[1]
self.last_seen = int(data[-1])
if data[-2] == "online":
if data[-1] == "online":
self.status = False
else:
self.status = True
self.connections = set()
self.blocked = set()
self.blocked_by = set()
for relation in data[2:-3]:
for relation in data[3:-2]:
if relation.startswith("-"):
self.blocked.add(relation)
elif relation.startswith("*"): # means this is blocked by another account
self.blocked_by.add(relation)
else:
self.connections.add(relation)

Expand All @@ -36,5 +33,5 @@ def __str__(self):
online = " online "
else:
online = " offline "
return str(self.id) + " " + self.name + " ".join(self.connections) +\
" -".join(self.blocked) + " *".join(self.blocked_by) + online + str(self.last_seen)
return str(self.id) + " " + self.name + " " + str(len(self.connections)) + " " + " ".join(self.connections) + \
" -".join(self.blocked) + online + str(self.last_seen)
43 changes: 41 additions & 2 deletions cache_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import itertools
from account import account
from bloomfilter import bloomfilter


class cache_manager:
Expand All @@ -22,6 +23,12 @@ class cache_manager:

def __init__(self, disk_manager):
self.dm = disk_manager
self.recommend_cache = disk_manager.new("recommend", 100000)
for i in range (0,101):
res = []
for j in range(1,101):
res.append(str(100*i+j))
disk_manager.write_block("recommend", res)

def does_id_exist(self, new_id: int):
return new_id < self.dataset_accounts_count
Expand Down Expand Up @@ -111,7 +118,6 @@ def block_unblock(self, id_1: int, id_2: int, block: bool):
# now for blocked account
acc = self.find_account(id_2)
acc.connections.remove(str(id_1))
acc.blocked_by.add("*" + str(id_1))
self.blocked_bloom[id_1] = self.update_bloom(acc.blocked)
else:
pass
Expand All @@ -126,5 +132,38 @@ def fetch_block_for_id(self, id: int):
return self.dm.read_block(), seek

# this creates the bloom based on the blocked and blocker accounts for an account
def update_bloom(self, acc:account):
def update_bloom(self, acc: account):
pass

def is_blocked(self, account_id: int, destination_id: int) -> bool:
acc = self.find_account(destination_id)
if acc.blocked.__contains__(account_id):
return True
return False

def follow_unfollow(self, id_1: int, id_2: int, follow: bool):
acc = self.find_account(id=id_1)
if follow:
if acc.blocked.__contains__(id_2):
print("can't follow: you have blocked this user")
elif self.is_blocked(account_id=id_1, destination_id=id_2):
print("can't follow: you have been blocked by this user")
else:
acc.connections.add(id_2)
else:
acc.connections.remove(id_2)

def find_online_friends(self, account_id: int):
acc = self.find_account(account_id)
res = []
for friend in acc.connections:
friend_acc = self.find_account(int(friend))
if friend_acc.status:
res.__add__(friend)
print(res)
return

# do we need a new disk for this one?
def recommend_new_accounts(self, account_id: int, time: int):
pass

23 changes: 21 additions & 2 deletions challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Challenge:
code_follow_unfollow = "0 4 "
code_find_online_friends = "1 1 "
code_recommend_accounts_to_follow = "1 2 "

# endregion

def __init__(self, disk_manager):
Expand All @@ -25,6 +24,12 @@ def run(self, events_or_queries):
self.change_online_status(line.replace(self.code_change_online_status, "", 1))
elif line.startswith(self.code_follow_unfollow):
self.follow_unfollow(line.replace(self.code_follow_unfollow, "", 1))
elif line.startswith(self.code_block_unblock):
self.block_unblock(line.replace(self.code_block_unblock, "", 1))
elif line.startswith(self.code_find_online_friends):
self.find_online_friends(line.replace(self.code_find_online_friends, "", 1))
elif line.startswith(self.code_recommend_accounts_to_follow):
self.recommend_new_accounts(line.replace(self.code_recommend_accounts_to_follow, "", 1))
else:
print("unknown command code")
except:
Expand Down Expand Up @@ -61,4 +66,18 @@ def block_unblock(self, command):
return

def follow_unfollow(self, command):
pass
id_1, id_2, follow = command.split(" ")
id_1, id_2 = int(id_1), int(id_2)
follow = False if follow == "0" else True
self.cache.follow_unfollow(id_1=id_1, id_2=id_2, follow=follow)
return

def find_online_friends(self, command):
account_id = int(command)
self.cache.find_online_friends(account_id)
return

def recommend_new_accounts(self, command):
account_id, time = int(command.split(" ")) # todo: will this work?
self.cache.recommend_new_accounts(account_id=account_id, time=time)
return
33 changes: 17 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
from shutil import copyfile

try:
copyfile("dataset_backup", "dataset_100000.d") #regenereate the dataset- delete at the end
copyfile("dataset_backup", "dataset_100000.d") # regenerate the dataset- delete at the end
except:
print("couldn't find the backup dataset. please insert dataset manually")
dm = DiskManager()

try:
app = Challenge(dm)
events_and_queries = []
while True:
line = input()
if line == "end":
break
elif line == "save end":
# app.cache.dump() // in case we needed to save the cached data before exit
pass
else:
events_and_queries.append(line)
dm.write_block("dataset", dm.read_block("dataset")[0:10])

app.run(events_and_queries)
finally:
pass
# try:
# app = Challenge(dm)
# events_and_queries = []
# while True:
# line = input()
# if line == "end":
# break
# elif line == "save end":
# # app.cache.dump() // in case we needed to save the cached data before exit
# pass
# else:
# events_and_queries.append(line)
# app.run(events_and_queries)
# finally:
# pass

0 comments on commit 231b7ee

Please sign in to comment.