forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem_120.py
36 lines (27 loc) · 881 Bytes
/
problem_120.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class SampleClass:
instances = dict()
even_instance = False
def __init__(self, instance_num):
self.instance_num = instance_num
@staticmethod
def initialize():
SampleClass.instances[0] = SampleClass(0)
SampleClass.instances[1] = SampleClass(1)
@staticmethod
def get_instance():
if not SampleClass.instances:
SampleClass.initialize()
SampleClass.even_instance = not SampleClass.even_instance
return SampleClass.instances[int(SampleClass.even_instance)]
# Tests
SampleClass.initialize()
i1 = SampleClass.get_instance()
assert i1.instance_num == 1
i2 = SampleClass.get_instance()
assert i2.instance_num == 0
i3 = SampleClass.get_instance()
assert i3.instance_num == 1
i4 = SampleClass.get_instance()
assert i4.instance_num == 0
i5 = SampleClass.get_instance()
assert i5.instance_num == 1