Why doesn't my heapify work? | | |
I want to turn an Array into a heap, but my code just doesn't work: no
change after execution.
A=[3,5,4,9,6,7]
m=len(A)-1
for i in range(m,1):
t=(i-1)/2
if A[i]>A[t]:
A[i],A[t]=A[t],A[i] | | | | re: Why doesn't my heapify work?
Dongsheng Ruan wrote: Quote:
I want to turn an Array into a heap, but my code just doesn't work: no
change after execution.
>
A=[3,5,4,9,6,7]
m=len(A)-1
>
>
>
for i in range(m,1):
t=(i-1)/2
if A[i]>A[t]:
A[i],A[t]=A[t],A[i]
First of all, there is the module heapq that will just do it.
And then you seem to misunderstand how the range-function works. range(m, 1)
will always be the empty list. See
pydoc range
for how it operates.
Overall, your code is very unpythonic, to say the least. I suggest you start
reading the python tutorial first: http://docs.python.org/tut/
Especially the looping techniques section: http://docs.python.org/tut/node7.htm...00000000000000
Diez | | | | re: Why doesn't my heapify work?
Can't range go from larger to smaller?
"Diez B. Roggisch" <deets@nospam.web.dewrote in message
news:52uiomF1olkevU1@mid.uni-berlin.de... Quote:
Dongsheng Ruan wrote:
> Quote:
>I want to turn an Array into a heap, but my code just doesn't work: no
>change after execution.
>>
>A=[3,5,4,9,6,7]
>m=len(A)-1
>>
>>
>>
>for i in range(m,1):
> t=(i-1)/2
> if A[i]>A[t]:
> A[i],A[t]=A[t],A[i]
>
First of all, there is the module heapq that will just do it.
>
And then you seem to misunderstand how the range-function works. range(m,
1)
will always be the empty list. See
>
pydoc range
>
for how it operates.
>
Overall, your code is very unpythonic, to say the least. I suggest you
start
reading the python tutorial first:
> http://docs.python.org/tut/
>
Especially the looping techniques section:
> http://docs.python.org/tut/node7.htm...00000000000000
>
Diez
| | | | re: Why doesn't my heapify work?
I found out what is wrong.
You must give it a negative step, like range(10,1,-1)
But my code is not good enought for heapify.
I will try again.
"Ruan" <ruan@jcmills.comwrote in message
news:eqd4nn$473e$1@netnews.upenn.edu... Quote:
Can't range go from larger to smaller?
>
>
"Diez B. Roggisch" <deets@nospam.web.dewrote in message
news:52uiomF1olkevU1@mid.uni-berlin.de... Quote:
>Dongsheng Ruan wrote:
>> Quote:
>>I want to turn an Array into a heap, but my code just doesn't work: no
>>change after execution.
>>>
>>A=[3,5,4,9,6,7]
>>m=len(A)-1
>>>
>>>
>>>
>>for i in range(m,1):
>> t=(i-1)/2
>> if A[i]>A[t]:
>> A[i],A[t]=A[t],A[i]
>>
>First of all, there is the module heapq that will just do it.
>>
>And then you seem to misunderstand how the range-function works. range(m,
>1)
>will always be the empty list. See
>>
>pydoc range
>>
>for how it operates.
>>
>Overall, your code is very unpythonic, to say the least. I suggest you
>start
>reading the python tutorial first:
>>
> http://docs.python.org/tut/
>>
>Especially the looping techniques section:
>>
> http://docs.python.org/tut/node7.htm...00000000000000
>>
>Diez
>
>
| | | | re: Why doesn't my heapify work?
ruanCan't range go from larger to smaller?
Yes. Read the doc:
range(...)
range([start,] stop[, step]) -list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
To go from larger to smaller you need a negative step size. Also, note that
the endpoint is never included. range(10) serves up 0 throu 9.
Skip |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|