Created
July 5, 2021 13:56
-
-
Save fuyi/1314168e6e09915ffd4e282e52f18d12 to your computer and use it in GitHub Desktop.
qingmiao_zhang_python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INPUT = [ | |
1, | |
2, | |
3, | |
4, | |
5, | |
11111, | |
9, | |
8, | |
7, | |
11111, | |
7, | |
6, | |
5, | |
9, | |
11111, | |
3, | |
2, | |
22222, | |
6, | |
5, | |
22222, | |
4, | |
5, | |
33333, | |
] | |
def distribute_numbers(input: list): | |
""" | |
This function distribute a series of numbers to buckets | |
this function assumes the seperators are 11111,22222,33333 and | |
they come in order. eg 11111 always comes before 22222, | |
22222 always comes before 33333. | |
33333 terminates this processor | |
""" | |
# Initiate some variables | |
# 3 classes | |
classes = ("A", "B", "C") | |
current_class = classes[0] | |
current_dim = 0 | |
# keep track of the first class encounter | |
first_b = True | |
first_c = True | |
output = { | |
"A": [], | |
"B": [], | |
"C": [], | |
} | |
for num in input: | |
if num == 11111: | |
current_class = classes[1] | |
if first_b: | |
current_dim = 0 | |
first_b = False | |
else: | |
current_dim += 1 | |
continue | |
elif num == 22222: | |
current_class = classes[2] | |
if first_c: | |
current_dim = 0 | |
first_c = False | |
else: | |
current_dim += 1 | |
continue | |
elif num == 33333: | |
break | |
else: | |
try: | |
# append number to correct class and dimention | |
output[current_class][current_dim].append(num) | |
except Exception: | |
# in case the dimention array is not initiated, initiate here | |
output[current_class].insert(current_dim, []) | |
output[current_class][current_dim].append(num) | |
return output | |
if __name__ == "__main__": | |
result = distribute_numbers(INPUT) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: