Can anyone please help me in understanding the following python code
Code : - def mergeSort(alist): print("Splitting ",alist) if len(alist)>1: mid = len(alist)//2 lefthalf = alist[:mid] righthalf = alist[mid:] mergeSort(lefthalf) mergeSort(righthalf) i=0 j=0 k=0 while ihttp://mail.python.org/mailman/listinfo/python-list
Re: Can anyone please help me in understanding the following python code
Thanks for the reply Chris. I am newbie to python, so please excuse me if I am asking chilly questions. Can you please explain more about the following sentence. "When it says "Splitting" with a single-element list, it then immediately prints "Merging" and returns (because all the rest of the code is guarded by the 'if'). Execution then continues where it left off, in the parent." Because I am not sure how the control can go back to top of the function unless there is no loops there. Also, Can you please let me know how did you found out that I am using Python 2 Interpreter. Bharath -- http://mail.python.org/mailman/listinfo/python-list
Re: Can anyone please help me in understanding the following python code
Thanks Chris, Wolfgang and Joshua for your replies. --- In step 2b, all the steps from 1 through 3 are executed again (twice). Soon, those calls will just output "Splitting" followed by "Merging"; and then we go back to 2c. That's why it *seems* that the code goes from 3 to 2c. You'll notice that the call depth decreases when this happens Chris, Can you please let me know what makes the control of the program code go to 2c after the output "Merging". Also, please look into the following output, before calling main mergesort Splitting [54, 26, 93, 17, 77, 31, 44, 55, 20] left half before split [54, 26, 93, 17] right half before split [77, 31, 44, 55, 20] Splitting [54, 26, 93, 17] left half before split [54, 26] right half before split [93, 17] Splitting [54, 26] left half before split [54] right half before split [26] Splitting [54] Merging [54] Splitting [26] Merging [26] HERE AFTER SPLIT left half after split [54] right half after split [26] Merging [26, 54] Splitting [93, 17] left half before split [93] right half before split [17] Splitting [93] Merging [93] Splitting [17] Merging [17] HERE AFTER SPLIT left half after split [93] right half after split [17] Merging [17, 93] HERE AFTER SPLIT left half after split [26, 54] right half after split [17, 93] Merging [17, 26, 54, 93] Splitting [77, 31, 44, 55, 20] left half before split [77, 31] right half before split [44, 55, 20] Splitting [77, 31] left half before split [77] right half before split [31] Splitting [77] Merging [77] Splitting [31] Merging [31] HERE AFTER SPLIT left half after split [77] right half after split [31] Merging [31, 77] Splitting [44, 55, 20] left half before split [44] right half before split [55, 20] Splitting [44] Merging [44] Splitting [55, 20] left half before split [55] right half before split [20] Splitting [55] Merging [55] Splitting [20] Merging [20] HERE AFTER SPLIT left half after split [55] right half after split [20] Merging [20, 55] HERE AFTER SPLIT left half after split [44] right half after split [20, 55] Merging [20, 44, 55] HERE AFTER SPLIT left half after split [31, 77] right half after split [20, 44, 55] Merging [20, 31, 44, 55, 77] HERE AFTER SPLIT left half after split [17, 26, 54, 93] right half after split [20, 31, 44, 55, 77] Merging [17, 20, 26, 31, 44, 54, 55, 77, 93] after calling main mergesort [17, 20, 26, 31, 44, 54, 55, 77, 93] - In the above output, the control goes to "HERE AFTER SPLIT" after the "Merging" statement which is of-course the last statement in the function.On what condition this is happening. Ideally as per my understanding, after the last statement of a function the control should come out of the function. Please correct me if I am wrong here. There is something with respect-to functions in python that I am not able to understand. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can anyone please help me in understanding the following python code
On Thursday, May 30, 2013 6:09:20 PM UTC+5:30, bhk...@gmail.com wrote: > Thanks Chris, Wolfgang and Joshua for your replies. > > > > --- > > In step 2b, all the steps from 1 through 3 are executed again (twice). > > Soon, those calls will just output "Splitting" followed by "Merging"; > > and then we go back to 2c. That's why it *seems* that the code goes > > from 3 to 2c. You'll notice that the call depth decreases when this > > happens > > > > Chris, Can you please let me know what makes the control of the program code > go to 2c after the output "Merging". > > Also, please look into the following output for the same program with more > print statements, > -- > before calling main mergesort > > Splitting [54, 26, 93, 17, 77, 31, 44, 55, 20] > > left half before split [54, 26, 93, 17] > > right half before split [77, 31, 44, 55, 20] > > Splitting [54, 26, 93, 17] > > left half before split [54, 26] > > right half before split [93, 17] > > Splitting [54, 26] > > left half before split [54] > > right half before split [26] > > Splitting [54] > > Merging [54] > > Splitting [26] > > Merging [26] > > > > HERE AFTER SPLIT > > > > left half after split [54] > > right half after split [26] > > Merging [26, 54] > > Splitting [93, 17] > > left half before split [93] > > right half before split [17] > > Splitting [93] > > Merging [93] > > Splitting [17] > > Merging [17] > > > > HERE AFTER SPLIT > > > > left half after split [93] > > right half after split [17] > > Merging [17, 93] > > > > HERE AFTER SPLIT > > > > left half after split [26, 54] > > right half after split [17, 93] > > Merging [17, 26, 54, 93] > > Splitting [77, 31, 44, 55, 20] > > left half before split [77, 31] > > right half before split [44, 55, 20] > > Splitting [77, 31] > > left half before split [77] > > right half before split [31] > > Splitting [77] > > Merging [77] > > Splitting [31] > > Merging [31] > > > > HERE AFTER SPLIT > > > > left half after split [77] > > right half after split [31] > > Merging [31, 77] > > Splitting [44, 55, 20] > > left half before split [44] > > right half before split [55, 20] > > Splitting [44] > > Merging [44] > > Splitting [55, 20] > > left half before split [55] > > right half before split [20] > > Splitting [55] > > Merging [55] > > Splitting [20] > > Merging [20] > > > > HERE AFTER SPLIT > > > > left half after split [55] > > right half after split [20] > > Merging [20, 55] > > > > HERE AFTER SPLIT > > > > left half after split [44] > > right half after split [20, 55] > > Merging [20, 44, 55] > > > > HERE AFTER SPLIT > > > > left half after split [31, 77] > > right half after split [20, 44, 55] > > Merging [20, 31, 44, 55, 77] > > > > HERE AFTER SPLIT > > > > left half after split [17, 26, 54, 93] > > right half after split [20, 31, 44, 55, 77] > > Merging [17, 20, 26, 31, 44, 54, 55, 77, 93] > > > > after calling main mergesort > > > > [17, 20, 26, 31, 44, 54, 55, 77, 93] > > --- > > In the above output, the control goes to "HERE AFTER SPLIT" after the > "Merging" statement which is of-course the last statement in the function.On > what condition this is happening. > > Ideally as per my understanding, after the last statement of a function the > control should come out of the function. > > Please correct me if I am wrong here. > > There is something with respect-to functions in python that I am not able to > understand. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can anyone please help me in understanding the following python code
On Thursday, May 30, 2013 6:09:20 PM UTC+5:30, bhk...@gmail.com wrote: > Thanks Chris, Wolfgang and Joshua for your replies. > > > > --- > > In step 2b, all the steps from 1 through 3 are executed again (twice). > > Soon, those calls will just output "Splitting" followed by "Merging"; > > and then we go back to 2c. That's why it *seems* that the code goes > > from 3 to 2c. You'll notice that the call depth decreases when this > > happens > > > > Chris, Can you please let me know what makes the control of the program code > go to 2c after the output "Merging". > > Also, please look into the following output for the same program with more > print statements, > -- > before calling main mergesort > > Splitting [54, 26, 93, 17, 77, 31, 44, 55, 20] > > left half before split [54, 26, 93, 17] > > right half before split [77, 31, 44, 55, 20] > > Splitting [54, 26, 93, 17] > > left half before split [54, 26] > > right half before split [93, 17] > > Splitting [54, 26] > > left half before split [54] > > right half before split [26] > > Splitting [54] > > Merging [54] > > Splitting [26] > > Merging [26] > > > > HERE AFTER SPLIT > > > > left half after split [54] > > right half after split [26] > > Merging [26, 54] > > Splitting [93, 17] > > left half before split [93] > > right half before split [17] > > Splitting [93] > > Merging [93] > > Splitting [17] > > Merging [17] > > > > HERE AFTER SPLIT > > > > left half after split [93] > > right half after split [17] > > Merging [17, 93] > > > > HERE AFTER SPLIT > > > > left half after split [26, 54] > > right half after split [17, 93] > > Merging [17, 26, 54, 93] > > Splitting [77, 31, 44, 55, 20] > > left half before split [77, 31] > > right half before split [44, 55, 20] > > Splitting [77, 31] > > left half before split [77] > > right half before split [31] > > Splitting [77] > > Merging [77] > > Splitting [31] > > Merging [31] > > > > HERE AFTER SPLIT > > > > left half after split [77] > > right half after split [31] > > Merging [31, 77] > > Splitting [44, 55, 20] > > left half before split [44] > > right half before split [55, 20] > > Splitting [44] > > Merging [44] > > Splitting [55, 20] > > left half before split [55] > > right half before split [20] > > Splitting [55] > > Merging [55] > > Splitting [20] > > Merging [20] > > > > HERE AFTER SPLIT > > > > left half after split [55] > > right half after split [20] > > Merging [20, 55] > > > > HERE AFTER SPLIT > > > > left half after split [44] > > right half after split [20, 55] > > Merging [20, 44, 55] > > > > HERE AFTER SPLIT > > > > left half after split [31, 77] > > right half after split [20, 44, 55] > > Merging [20, 31, 44, 55, 77] > > > > HERE AFTER SPLIT > > > > left half after split [17, 26, 54, 93] > > right half after split [20, 31, 44, 55, 77] > > Merging [17, 20, 26, 31, 44, 54, 55, 77, 93] > > > > after calling main mergesort > > > > [17, 20, 26, 31, 44, 54, 55, 77, 93] > > --- > > In the above output, the control goes to "HERE AFTER SPLIT" after the > "Merging" statement which is of-course the last statement in the function.On > what condition this is happening. > > Ideally as per my understanding, after the last statement of a function the > control should come out of the function. > > Please correct me if I am wrong here. > > There is something with respect-to functions in python that I am not able to > understand. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can anyone please help me in understanding the following python code
Thanks Chris, Wolfgang and Joshua for your replies. In step 2b, all the steps from 1 through 3 are executed again (twice). Soon, those calls will just output "Splitting" followed by "Merging"; and then we go back to 2c. That's why it *seems* that the code goes from 3 to 2c. You'll notice that the call depth decreases when this happens Chris, Can you please let me know what makes the control of the program code go to 2c after the output "Merging". Also, please look into the following output for the same program with more print statements, -- before calling main mergesort Splitting [54, 26, 93, 17, 77, 31, 44, 55, 20] left half before split [54, 26, 93, 17] right half before split [77, 31, 44, 55, 20] Splitting [54, 26, 93, 17] left half before split [54, 26] right half before split [93, 17] Splitting [54, 26] left half before split [54] right half before split [26] Splitting [54] Merging [54] Splitting [26] Merging [26] HERE AFTER SPLIT left half after split [54] right half after split [26] Merging [26, 54] Splitting [93, 17] left half before split [93] right half before split [17] Splitting [93] Merging [93] Splitting [17] Merging [17] HERE AFTER SPLIT left half after split [93] right half after split [17] Merging [17, 93] HERE AFTER SPLIT left half after split [26, 54] right half after split [17, 93] Merging [17, 26, 54, 93] Splitting [77, 31, 44, 55, 20] left half before split [77, 31] right half before split [44, 55, 20] Splitting [77, 31] left half before split [77] right half before split [31] Splitting [77] Merging [77] Splitting [31] Merging [31] HERE AFTER SPLIT left half after split [77] right half after split [31] Merging [31, 77] Splitting [44, 55, 20] ft half before split [44] ght half before split [55, 20] Splitting [44] Merging [44] Splitting [55, 20] eft half before split [55] ight half before split [20] Splitting [55] Merging [55] Splitting [20] Merging [20] HERE AFTER SPLIT left half after split [55] right half after split [20] Merging [20, 55] HERE AFTER SPLIT left half after split [44] right half after split [20, 55] Merging [20, 44, 55] HERE AFTER SPLIT left half after split [31, 77] right half after split [20, 44, 55] Merging [20, 31, 44, 55, 77] HERE AFTER SPLIT left half after split [17, 26, 54, 93] right half after split [20, 31, 44, 55, 77] Merging [17, 20, 26, 31, 44, 54, 55, 77, 93] after calling main mergesort [17, 20, 26, 31, 44, 54, 55, 77, 93] --- In the above output, the control goes to "HERE AFTER SPLIT" after the "Merging" statement which is of-course the last statement in the function.On what condition this is happening. Ideally as per my understanding, after the last statement of a function the control should come out of the function. Please correct me if I am wrong here. There is something with respect-to functions in python that I am not able to understand. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can anyone please help me in understanding the following python code
Got It!!!, Finally. Thanks Dave So, the control goes back to the place after the recursive function is called once the no. of element is equal to one and starts merging after which it will again start to split the remaining items. Thank you Chris for your multiple explanations. It was also very useful. One final question, Is there a way to edit the message once it has been posted? -- http://mail.python.org/mailman/listinfo/python-list
Re: Can anyone please help me in understanding the following python code
Got It!!!, Finally. Thanks Dave So, the control goes back to the place after the recursive function is called once the no. of element is equal to one and starts merging after which it will again start to split the remaining items. Thank you Chris for your multiple explanations. One final question, Is there a way to edit the message once it has been posted? -- http://mail.python.org/mailman/listinfo/python-list
Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__'
I am trying to create 2D arrays without using advanced features like numpy, for this I have created 2 separate modules arrays.py and array2D.py. Here's the code for that: arrays.py module: == import ctypes class Array: #Creates an array with size elements. def __init__( self, size ): assert size > 0, "Array size must be > 0" self._size = size print "sixe is %s" %self._size # Create the array structure using the ctypes module. PyArrayType = ctypes.c_int * size self._elements = PyArrayType() print "type is e", type(self._elements) #self._elements = ctypes.c_int * size print "Elements are self.element %s" % self._elements # Initialize each element. #for i in range(self._size): # self.clear( i ) # Returns the size of the array. def __len__( self ): return self._size # Gets the contents of the index element. def __getitem__( self, index ): assert index >= 0 and index < len(self), "Array subscript out of range" return self._elements[ index ] # Puts the value in the array element at index position. def __setitem__( self, index, value ): assert index >= 0 and index < len(self), "Array subscript out of range" print "Type is ", type(index) self._elements[ index ] = value # Clears the array by setting each element to the given value. def clear( self, value ): for i in range( len(self) ) : self._elements[i] = value # Printing the arrays: def __str__(self): return self._elements array2D.py module == import arrays class Array2D : # Creates a 2-D array of size numRows x numCols. def __init__( self, numRows, numCols ): # Create a 1-D array to store an array reference for each row. self._theRows = arrays.Array( numRows ) # Create the 1-D arrays for each row of the 2-D array. print "Num of Cloumns is", numCols for i in range( numRows ) : self._theRows[i] = arrays.Array( numCols ) # Returns the number of rows in the 2-D array. def numRows( self ): return len( self._theRows ) # Returns the number of columns in the 2-D array. def numCols( self ): return len( self._theRows[0] ) # Clears the array by setting every element to the given value. def clear( self, value ): for row in range( self.numRows() ): row.clear( value ) # Gets the contents of the element at position [i, j] def __getitem__( self, ndxTuple ): assert len(ndxTuple) == 2, "Invalid number of array subscripts." row = ndxTuple[0] col = ndxTuple[1] assert row >= 0 and row < self.numRows() \ and col >= 0 and col < self.numCols(), \ "Array subscript out of range." the1dArray = self._theRows[row] return the1dArray[col] # Sets the contents of the element at position [i,j] to value. def __setitem__( self, ndxTuple, value ): #assert len(ndxTuple) == 3, "Invalid number of array subscripts." row = ndxTuple[0] col = ndxTuple[1] assert row >= 0 and row < self.numRows() \ and col >= 0 and col < self.numCols(), \ "Array subscript out of range." the1dArray = self._theRows[row] the1dArray[col] = value arr = Array2D(2,4) print "arr is %s" %arr Traceback is : sixe is 2 type is e Elements are self.element Cols in 4 Num of Cloumns is 4 !! i is 0 sixe is 4 type is e Elements are self.element Type is Traceback (most recent call last): File "C:\Python27\Lib\array2D.py", line 53, in arr = Array2D(2,4) File "C:\Python27\Lib\array2D.py", line 16, in __init__ self._theRows[i] = arrays.Array( numCols ) File "C:\Python27\Lib\arrays.py", line 36, in __setitem__ self._elements[ index ] = value AttributeError: Array instance has no attribute '__trunc__' -- http://mail.python.org/mailman/listinfo/python-list
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__'
On Monday, April 8, 2013 3:37:38 PM UTC+5:30, bhk...@gmail.com wrote: > I am trying to create 2D arrays without using advanced features like numpy, > for this I have created 2 separate modules arrays.py and array2D.py. Here's > the code for that: > > > > arrays.py module: > > == > > import ctypes > > > > class Array: > > > > #Creates an array with size elements. > > def __init__( self, size ): > > assert size > 0, "Array size must be > 0" > > self._size = size > > print "sixe is %s" %self._size > > > > # Create the array structure using the ctypes module. > > PyArrayType = ctypes.c_int * size > > self._elements = PyArrayType() > > print "type is e", type(self._elements) > > #self._elements = ctypes.c_int * size > > > > print "Elements are self.element %s" % self._elements > > # Initialize each element. > > #for i in range(self._size): > > # self.clear( i ) > > > > > > # Returns the size of the array. > > def __len__( self ): > > return self._size > > > > # Gets the contents of the index element. > > def __getitem__( self, index ): > > assert index >= 0 and index < len(self), "Array subscript out of > range" > > return self._elements[ index ] > > > > # Puts the value in the array element at index position. > > def __setitem__( self, index, value ): > > assert index >= 0 and index < len(self), "Array subscript out of > range" > > print "Type is ", type(index) > > self._elements[ index ] = value > > > > # Clears the array by setting each element to the given value. > > def clear( self, value ): > > for i in range( len(self) ) : > > self._elements[i] = value > > > > # Printing the arrays: > > def __str__(self): > > return self._elements > > > > > > > > array2D.py module > > == > > > > > > import arrays > > > > class Array2D : > > # Creates a 2-D array of size numRows x numCols. > > def __init__( self, numRows, numCols ): > > # Create a 1-D array to store an array reference for each row. > > > > self._theRows = arrays.Array( numRows ) > > # Create the 1-D arrays for each row of the 2-D array. > > print "Num of Cloumns is", numCols > > > > for i in range( numRows ) : > > self._theRows[i] = arrays.Array( numCols ) > > > > # Returns the number of rows in the 2-D array. > > def numRows( self ): > > return len( self._theRows ) > > > > # Returns the number of columns in the 2-D array. > > def numCols( self ): > > return len( self._theRows[0] ) > > > > # Clears the array by setting every element to the given value. > > def clear( self, value ): > > for row in range( self.numRows() ): > > row.clear( value ) > > > > # Gets the contents of the element at position [i, j] > > def __getitem__( self, ndxTuple ): > > assert len(ndxTuple) == 2, "Invalid number of array subscripts." > > row = ndxTuple[0] > > col = ndxTuple[1] > > assert row >= 0 and row < self.numRows() \ > > and col >= 0 and col < self.numCols(), \ > > "Array subscript out of range." > > the1dArray = self._theRows[row] > > return the1dArray[col] > > > > # Sets the contents of the element at position [i,j] to value. > > def __setitem__( self, ndxTuple, value ): > > #assert len(ndxTuple) == 3, "Invalid number of array subscripts." > > row = ndxTuple[0] > > col = ndxTuple[1] > > assert row >= 0 and row < self.numRows() \ > > and col >= 0 and col < self.numCols(), \ > > "Array subscript out of range." > > the1dArray = self._theRows[row] > > the1dArray[col] = value > > > > > > arr = Array2D(2,4) > > > > print "arr is %s" %arr > > > > > > Traceback is : > > > > sixe is 2 > > type is e > > Elements are self.element > > Cols in 4 > > Num of Cloumns is 4 > > !! i is 0 > > sixe is 4 > > type is e > > Elements are self.element > > Type is > > Traceback (most recent call last): > > File "C:\Python27\Lib\array2D.py", line 53, in > > arr = Array2D(2,4) > > File "C:\Python27\Lib\array2D.py", line 16, in __init__ > > self._theRows[i] = arrays.Array( numCols ) > > File "C:\Python27\Lib\arrays.py", line 36, in __setitem__ > > self._elements[ index ] = value > > AttributeError: Array instance has no attribute '__trunc__' Hi Dylan, Thank you for the alternative solution. I will look into that. -- http://mail.python.org/mailman/listinfo/pyt
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__'
Hi Dylan, Thank you for the alternative solution. I will look into that. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__'
Thanks Steven for pointing that out. This is my first topic in Google Groups. So, I did not notice that the previous contents will also be taken in the new post. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__'
Awesome!!! Thanks a lot Peter. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__'
Awesome Peter!!!. Thanks a lot for detailed explanation... -- http://mail.python.org/mailman/listinfo/python-list