forked from Vansh011/Pyhton_libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode1.py
48 lines (40 loc) · 1.62 KB
/
code1.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
37
38
39
40
41
42
43
44
45
46
47
from collections import defaultdict
import string
def next_server_number(current_numbers):
'''Function to allocate new server numbers.'''
if not current_numbers:
return 1
else:
highest_number = max(current_numbers)
for num in range(1, highest_number):
if num not in current_numbers:
return num
return highest_number + 1
class Tracker:
def __init__(self):
self.host_types = defaultdict(list) #noice to know this very hard
def allocate(self, host_type):
'''Function to allocate new server numbers.'''
available = next_server_number(self.host_types[host_type])
self.host_types[host_type].append(available)
return '{}{}'.format(host_type, available)
def deallocate(self, hostname):
'''Function to deallocate the inputted server.'''
# Find the position where the numeric part starts
last_letter = 0
for i, char in enumerate(hostname):
if char.isdigit():
last_letter = i - 1
break
host_type = hostname[:last_letter + 1]
host_num = int(hostname[last_letter + 1:])
if host_num in self.host_types[host_type]:
self.host_types[host_type].remove(host_num)
return None
# Example Usage:
tracker = Tracker()
print(tracker.allocate("apibox")) # "apibox1"
print(tracker.allocate("apibox")) # "apibox2"
print(tracker.deallocate("apibox1")) # None, deallocates "apibox1"
print(tracker.allocate("apibox")) # "apibox1", reuses "apibox1"
print(tracker.allocate("sitebox")) # "sitebox1"