-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbering.py
executable file
·87 lines (62 loc) · 2.58 KB
/
numbering.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
81
82
83
84
85
86
87
#!/bin/env python3
# Function to compute the next readable number for the sample.
def getNextNumber(currentMax, computation = "running"):
# Handle the problem that no current max can be determined
# for first dataset in table.
if currentMax is None:
currentMax = 0
# "running" means a generic running number (n = n + 1).
if computation == "running":
return currentMax + 1
# Do not allow repeating digits as in 311 or 10222.
elif computation == "no_repeating":
# Start with the assumption that the next bigger integer
# is a suitable candidate.
candidate = currentMax + 1
# As long as there are repeating digits (turning the stringified form of
# candidate into a set removes doubles) add one.
while len(set(str(candidate))) < len(str(candidate)):
candidate = candidate + 1
return candidate
# Do not allow permutations of the new max to be smaller numbers.
# E.g. if you have 124 you can't have 421 or 241 or 214 ...
elif computation == "no_swaps":
import itertools # For creating permutations.
# Start with the assumption that the next bigger integer
# is a suitable candidate.
candidate = currentMax + 1
# Create permutation for current string.
sPermutations = [''.join(p) for p in itertools.permutations(str(candidate))]
# Check wether a permutation of candidate is a smaller number.
while any(
item in str(list(range(1, currentMax))) for item in sPermutations
):
# If so try the next.
candidate = candidate + 1
# Also recreate permutation.
sPermutations = [
''.join(p) for p in itertools.permutations(str(candidate))
]
return candidate
# Allow neither repeating nor swappable digits.
elif computation == "save":
import itertools # For creating permutations.
# Start with the assumption that the next bigger integer
# is a suitable candidate.
candidate = currentMax + 1
# Create permutation for current string.
sPermutations = [''.join(p) for p in itertools.permutations(str(candidate))]
# Check wether a permutation of candidate is a smaller number.
while any(
item in str(list(range(1, currentMax))) for item in sPermutations
) or len(set(str(candidate))) < len(str(candidate)):
# If so try the next.
candidate = candidate + 1
# Also recreate permutation.
sPermutations = [
''.join(p) for p in itertools.permutations(str(candidate))
]
return candidate
# Anything else was probably a typo.
else:
raise ValueError("Computation type \"" + computation + "\" is not defined.")