Skip to content

Instantly share code, notes, and snippets.

@fuyi
Created July 5, 2021 13:56
Show Gist options
  • Save fuyi/1314168e6e09915ffd4e282e52f18d12 to your computer and use it in GitHub Desktop.
Save fuyi/1314168e6e09915ffd4e282e52f18d12 to your computer and use it in GitHub Desktop.
qingmiao_zhang_python
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)
@fuyi
Copy link
Author

fuyi commented Jul 5, 2021

Output:

{'A': [[1, 2, 3, 4, 5]], 'B': [[9, 8, 7], [7, 6, 5, 9], [3, 2]], 'C': [[6, 5], [4, 5]]}

@fuyi
Copy link
Author

fuyi commented Jul 5, 2021

你可以把上面的代码贴到这个页面上执行 https://www.programiz.com/python-programming/online-compiler/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment