1

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!!

Joran Beasley
114k13 gold badges168 silver badges187 bronze badges
asked Aug 13, 2017 at 20:47
5
  • 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. Commented 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) Commented 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? Commented 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 generates Commented 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 mind Commented Aug 14, 2017 at 15:24

2 Answers 2

1

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
answered Aug 13, 2017 at 20:57
Sign up to request clarification or add additional context in comments.

Comments

0
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
Nico Griffioen
5,4362 gold badges29 silver badges36 bronze badges
answered Oct 10, 2019 at 10:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.