Write a class SocialNetwork
with private attributes for friends and blocked users. Implement methods for sending and receiving friend requests, making sure that blocked users cannot send requests.
Example 1:
Input: Friend Request: "John", Block User: "John" Output: "Blocked user cannot send friend requests"
Example 2:
Input: Friend Request: "John" Output: "Friend request sent"
Maintain separate lists for friends and blocked users. Check the blocked users list before sending a friend request.
class SocialNetwork: def __init__(self): self._friends = [] self._blocked_users = [] def send_friend_request(self, user): if user in self._blocked_users: return "Blocked user cannot send friend requests" else: self._friends.append(user) return "Friend request sent" def block_user(self, user): self._blocked_users.append(user) # Test the class social_network = SocialNetwork() social_network.block_user("John") print(social_network.send_friend_request("John")) # Output: Blocked user cannot send friend requests social_network = SocialNetwork() print(social_network.send_friend_request("John")) # Output: Friend request sent
Unlock AI & Data Science treasures. Log in!