Great, glad to hear it's working now! Happy coding :)

In summary, the code provided defines a procedure that takes in a string of numbers and outputs a list with the following parameters: every number in the string is inserted into the list, if a number is less than or equal to the preceding number it is inserted into a sublist, and the sublist continues until reaching a number that is greater than the preceding number. The code also includes test cases to check for accuracy.
  • #1
doktorwho
181
6

Homework Statement


Define a procedure that does the following:
From the string of numbers from 1 - 9 it creates a list that contains the numbers that are bigger than the preceding number and adds the smaller ones to a sub-list. Example:
string='3057842103'
print(numbers_in_list(string))
>>>[3,[0],5,7,8,[4,2,1,0,3]]
This is a Udacity CS101 problem from the ProblemSet in Lecture 3 named, Numbers in List.

Homework Equations


3. The Attempt at a Solution [/B]
Python:
def numbers_in_lists(string):
   list=[int(string[0])]
   list2=[]
   a=int(string[0])
   i=1
   while i<len(string):
     if a<int(string[i]):
       a=int(string[i])
       list.append(int(string[i]))
       list2=[]
       i+=1
     if a>=int(string[i]):
       list2.append(int(string[i]))
       if list2 not in list:
         list.append(list2)
       i+=1
   return list
When i run print(numbers_in_list(3057842103)) i get the correct result but when i copy it and paste it in the udacity python it looks like this:
Python:
# 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 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.

#Hint - "int()" turns a string's element into a number

def numbers_in_lists(string):
   list=[int(string[0])]
   list2=[]
   a=int(string[0])
   i=1
   while i<len(string):
     if a<int(string[i]):
       a=int(string[i])
       list.append(int(string[i]))
       list2=[]
       i+=1
     if a>=int(string[i]):
       list2.append(int(string[i]))
       if list2 not in list:
         list.append(list2)
       i+=1
   return list
           
#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
Now when i run i get this:
Python:
Traceback (most recent call last): File "vm_main.py", line 33, in <module> import main File "/tmp/vmuser_kzfoxtgohv/main.py", line 44, in <module> print repr(string), numbers_in_lists(string) == result File "/tmp/vmuser_kzfoxtgohv/main.py", line 25, in numbers_in_lists if a>=int(string[i]): IndexError: string index out of range
'543987' True '987654321' True '455532123266' True '123456789'
What is going on here, kinda weirded out..o_O
 
Technology news on Phys.org
  • #2
Look at your code again especially the line:

in numbers_in_lists if a>=int(string [ i ]): IndexError: string index out of range

and see if the could get out of range somehow.

You could also place print statements in your code to see the variables are set to.

One thing I noticed is that right before this line you have i+=1; and then you have the failure.

the i+=1 could now be equal to the length of the string and hence would index character past it a kind of edge case.
 
  • #3
It seems to me it should always fail. Suppose the string has 5 elements. Then in the while statement, you can have i=4. You do the first if statement, then increment i, so now i=5. Then you ask if a>=int(string):. But string only has 5 elements, so there is no string[5]. How did it ever work?
 
  • #4
jedishrfu said:
Look at your code again especially the line:

in numbers_in_lists if a>=int(string [ i ]): IndexError: string index out of range

and see if the could get out of range somehow.

You could also place print statements in your code to see the variables are set to.

One thing I noticed is that right before this line you have i+=1; and then you have the failure.

the i+=1 could now be equal to the length of the string and hence would index character past it a kind of edge case.

phyzguy said:
It seems to me it should always fail. Suppose the string has 5 elements. Then in the while statement, you can have i=4. You do the first if statement, then increment i, so now i=5. Then you ask if a>=int(string):. But string only has 5 elements, so there is no string[5]. How did it ever work?
I added a line under i+=1 to check for the length and if it exceeds it breaks. Works now. Thanks for the help :)!
 

1. What is the purpose of writing a procedure in python?

The purpose of writing a procedure in python is to create a reusable block of code that can be executed multiple times with different arguments. This helps in organizing and simplifying complex tasks, making code more efficient and easier to maintain.

2. How do you define a procedure in python?

To define a procedure in python, you can use the def keyword followed by the name of the procedure and a set of parentheses. Inside the parentheses, you can specify any parameters that the procedure will take. The procedure body is indented below the def statement.

3. What are parameters and arguments in a python procedure?

Parameters are the variables that are defined in the procedure's header and act as placeholders for the arguments that will be passed to the procedure when it is called. Arguments, on the other hand, are the actual values that are passed to the procedure when it is called.

4. How do you call a procedure in python?

To call a procedure in python, you simply need to use its name followed by a set of parentheses. Inside the parentheses, you can pass in any required arguments. If the procedure has no parameters, the parentheses can be left empty.

5. Can you return a value from a python procedure?

Yes, you can return a value from a python procedure using the return keyword. This allows the procedure to compute a result and pass it back to the code that called it. If no return statement is used, the procedure will return None by default.

Similar threads

  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
4K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top