I'm doing a course in udacity, and i got stuck in one exercise. I've changed my code a thousand times but I think that I'm not even close to the righ answer. The exercise is this: 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 a number x in the string is less than or equal to the preceding number y, the number x should be inserted into a sublist. Continue adding the following numbers to the sublist until reaching a number z that is greater than the number y. Then add this number z to the normal list and continue. So if the string is '543987' the out put should be [5,[4,3],9,[8,7]].
and my code is:
string = '543987'
def numbers_in_lists(string):
result = []
sublist = []
counter = 0
ref = 0
for e in string:
if e == string[0]:
result.append(int(e))
if int(e) < ref:
sublist.append(int(e))
else:
result.append(sublist)
sublist = []
result.append(int(e))
counter = counter + string.find(e)
ref = int(string[counter])
return result
print numbers_in_lists(string)
# [5, [], 5, [4, 3], 9] which is not what i expected ([5,[4,3],9,[8,7]])
Is it too bad?? hope someone can help me. Thanks!!
-
575 fails. It returns [5,7,5] instead of [5,7,[5]] because the first check only checks if the character is equal to the first character, not if it's at the first character's position.ChristianFigueroa– ChristianFigueroa2017年08月13日 20:55:20 +00:00Commented Aug 13, 2017 at 20:55
-
why the down votes? he clearly put some effort in, he posted the problem statement, and the code he tried(which seems like a reasonable attempt to me)Joran Beasley– Joran Beasley2017年08月13日 20:59:10 +00:00Commented Aug 13, 2017 at 20:59
-
@JoranBeasley But he didn't say what the problem is with the code he posted. So what is he asking?khelwood– khelwood2017年08月13日 21:01:52 +00:00Commented Aug 13, 2017 at 21:01
-
in his second sentence he posted he was not getting the expected output... ive edited his question to include what his code generatesJoran Beasley– Joran Beasley2017年08月13日 21:06:00 +00:00Commented Aug 13, 2017 at 21:06
-
@JoranBeasley thank you very much for the help and the corrections!! and i couldn't care less about that guy's downvote, so never mindPedro Lopez– Pedro Lopez2017年08月14日 15:24:31 +00:00Commented Aug 14, 2017 at 15:24
2 Answers 2
there are only two options for each letter in the string
if int(letter) > last_letter:
if sublist: # if we have any entries in our sublist
result.append(sublist) # append them before appending this letter
sublist = [] # clear sublist
result.append(int(letter)) # append our letter
else: # it must be lessthan or equal
sublist.append(int(letter)) # so append to sublist
last_letter = int(letter) # update last_letter
then at the end before you return your result you want to do the same check
if sublist: result.append(sublist)
return result
Comments
def numbers_in_lists(string):
# YOUR CODE
A = [] #create main list
B = [] #create sublist
length = len(string)
i = 0
biggest = 0 #initiate biggest number to compare with other number in the string
while i < length:
if int(string[i]) > biggest:
biggest = int(string[i])
A.append(biggest)
B = [] #empty sublist to prepare for next round
else:
if B not in A: #prevent adding another sublist B to A
A.append(B)
B.append(int(string[i]))
i += 1
return A