-
Notifications
You must be signed in to change notification settings - Fork 0
/
6_Number in list.py
executable file
·46 lines (37 loc) · 1.48 KB
/
6_Number in list.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
# Numbers in lists by SeanMc from forums
# define a procedure that takes in a string of numbers from 1-9 and
# outputs a list with the following parameters:
# Every number in the string should be inserted into the list.
# If the first number in the string is greater than or equal
# to the proceeding number, the proceeding number should be inserted
# into a sublist. Continue adding to the sublist until the proceeding number
# is greater than the first number before the sublist.
# Then add this bigger number to the normal list.
#Hint - "int()" turns a string's element into a number
def numbers_in_lists(string):
finallist=[int(string[0])]
sublist=[]
for i in string:
if int(i)>finallist[-1] and i != string[0]:
if sublist:
finallist.append(sublist)
sublist=[]
finallist.append(int(i))
elif i != string[0]:
sublist.append(int(i))
if sublist:
finallist.append(sublist)
return finallist
#testcases
string = '543987'
result = [5,[4,3],9,[8,7]]
print repr(string), numbers_in_lists(string)== result
string= '987654321'
result = [9,[8,7,6,5,4,3,2,1]]
print repr(string), numbers_in_lists(string) == result
string = '455532123266'
result = [4, 5, [5, 5, 3, 2, 1, 2, 3, 2], 6, [6]]
print repr(string), numbers_in_lists(string) == result
string = '123456789'
result = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print repr(string), numbers_in_lists(string) == result