-
Notifications
You must be signed in to change notification settings - Fork 0
/
task2.py
80 lines (63 loc) · 1.76 KB
/
task2.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import random
def random_exclude(range_start: int, range_end: int, excludes):
"""
:param range_start:
:param range_end:
:param excludes:
:return:
"""
# recursive errors handle
try:
value = random.randint(range_start, range_end)
# check if value exists
if value in excludes:
# try to generate next
return random_exclude(range_start, range_end, excludes)
else:
# append to excludes row
excludes.append(value)
# set None if reaching recursive depth
except RecursionError:
value = None
return value
def get_numbers_ticket(_min: int, _max: int, _qty: int) -> list:
"""
:param _min:
:param _max:
:param _qty:
:return:
"""
_excludes, _list = [], []
# check input values
if not validated(_min, _max, _qty):
return _list
# iterate by range of _qty
for _ in range(_qty):
# append random number in range _min, _max
res = random_exclude(_min, _max, _excludes)
if res is not None:
_list.append(res)
# check if length equals quantity
if len(_list) < _qty:
return []
# sort ascending
_list.sort()
# return result
return _list
def validated(_min: int, _max: int, _qty: int) -> bool:
"""
:param _min:
:param _max:
:param _qty:
"""
if _qty < 1:
print("Invalid :quantity (> 0)")
return False
if _min >= _max or _min < 1 or _max > 1000:
print("Invalid :min (> 0) or :max (<=1000)")
return False
return True
print(get_numbers_ticket(10, 5, -2)) # input error
print(get_numbers_ticket(0, 400, 17)) # input error
print(get_numbers_ticket(10, 14, 6))
print(get_numbers_ticket(10, 15, 5))