Skip to content

Commit

Permalink
upload new problem with solution
Browse files Browse the repository at this point in the history
  • Loading branch information
dombroks committed Dec 29, 2020
1 parent 2e6e637 commit b8b8f2a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Microsoft_Problems/Microsoft_Problem_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
This problem was asked by Microsoft.
Implement the singleton pattern with a twist. First, instead of storing one instance, store two instances. And in every even call of getInstance(), return the first instance and in every odd call of getInstance(), return the second instance.
"""
call_times = 0


class String:
def __init__(self):
self.string = None

def get_instance(self):
global call_times
call_times += 1
first_instance = String()
first_instance.string = "instance 1"
second_instance = String()
second_instance.string = "instance 2"
if call_times % 2 == 0:
return first_instance
else:
return second_instance


# Driver code
for i in range(6):
obj = String().get_instance()
print("".join("Call n°" + str(i)) + ":")
print(obj.string)

0 comments on commit b8b8f2a

Please sign in to comment.