Python coding
i want to split list shown below
a=[5,4,2,5,7,5,4,10,2]
if this list is given, i want to split it into
b=[[5,4,2,5],[7,5,4],[10,2]]
the algorithm is split until there is bigger number than 5 then 5,4,2,5 is in one list, next number is 7, so split the list until there is bigger then 7 which is 10. how can i do this?
Advertisement
Answer
JavaScript
x
14
14
1
arr = [5,4,2,5,7,5,4,10,2]
2
3
current = arr[0]
4
temp = []
5
res = []
6
for num in arr:
7
if num > current:
8
res.append(temp)
9
current = num
10
temp = []
11
temp.append(num)
12
res.append(temp)
13
print(res)
14
Prints:
[[5, 4, 2, 5], [7, 5, 4], [10, 2]]