473,461 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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]
Feb 7 '07 #1
5 2080
Dongsheng Ruan wrote:
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
Feb 7 '07 #2
Can't range go from larger to smaller?
"Diez B. Roggisch" <de***@nospam.web.dewrote in message
news:52*************@mid.uni-berlin.de...
Dongsheng Ruan wrote:
>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

Feb 7 '07 #3
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" <ru**@jcmills.comwrote in message
news:eq***********@netnews.upenn.edu...
Can't range go from larger to smaller?
"Diez B. Roggisch" <de***@nospam.web.dewrote in message
news:52*************@mid.uni-berlin.de...
>Dongsheng Ruan wrote:
>>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


Feb 7 '07 #4

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
Feb 7 '07 #5
You do know about the heapq module?
http://docs.python.org/dev/lib/module-heapq.html

--
mvh Björn
Feb 7 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Scott David Daniels | last post by:
I am sorry, but in the Python 2.4 description of "heapify", I find the description of "Transform list x into a heap, in-place, in linear time," unbelievable. I understand the hand-wave that makes...
7
by: AnnMarie | last post by:
My JavaScript Form Validation doesn't work at all in Netscape, but it works fine in IE. I made some of the suggested changes which enabled it to work in IE. I couldn't make all the changes...
39
by: Mark Johnson | last post by:
It doesn't seem possible. But would the following also seem a violation of the general notions behind css? You have a DIV, say asociated with class, 'topdiv'. Inside of that you have an anchor...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
4
by: bbp | last post by:
Hello, In an ASPX page I have a "Quit" button which make a simple redirect in code-behind. This button doesn't work no more since (I think) I moved from the framework 1.0 to 1.1 and it doesn't...
10
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET...
2
by: Traclo | last post by:
With this code I'm trying to create a huffman encoding tree, to compress text. But heapify just won't work for me :( def create_tree(dict): listofNodes = for element in dict.items(): ...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.