An algorithm problem

John Machin sjmachin at lexicon.net
Wed May 31 04:29:32 EDT 2006


On 31/05/2006 4:58 PM, Bo Yang wrote:
> Hi ,
> I have writen a python program to slove a problem described as below:
> 
> (Forgive my poor English !)
> 
> Put the 2^n 0 or 1 to form a ring ,

Sorry, I can't understand that. It would be very helpful if you wrote
out (for example, when n == 3) what the "0-1 ring" looks like.

Do you mean ring as in a circular buffer, or do you mean ring as in
"groups, rings, and fields"?

> and we can select any continuous n
> ones from
> the ring to constitute a binary number . And obviously we can get 2^n
> selections ,
> so the question is :
> Given a number n and you must
> Povide an algorithm by which the 0-1 ring is produced , and the 2^n
> number we get
> are just from 0 to 2^n-1 uniquely and entirely .
> 
> So I write the following program to slove the question , and it works
> well for the n below 10
> 
> 
> flag = 0
> input = 10
Problem! See below.
> number = 2**input
> set = set()
Problem!!
> list = []
Problem!!!

It is a very bad habit to re-use or "shadow" the names of built-in
functions (input, set, list). Please stop now. Try to think of
meaningful names for your input number, your set and your list.

What do you think would happen if your code needed a second set and you
wrote:
set2 = set()
?

> 
> def list2int(l , n ):

Please don't use "l" (that's "L".lower()) as an identifier. It is far
too easily confused with the digit "1" in many fonts.

> ret = 0
> for x in l :
> if( n == 0 ):
Please lose the (); they are redundant.

Please read http://www.python.org/dev/peps/pep-0008/
in particular the section on whitespace.
(Please forgive my exceedingly poor putonghua!) Dui bu qi, women shi
laowai -- we can't read our characters easily without the customary spacing.

> break

[snip]

Your code appears without any indentation in the two newsreaders that I
tried. As a result, I (and I presume others interested in helping you)
can't read your code without a lot of guesswork, and certainly can't run
it. Try changing leading tabs to spaces, and sending it again.
Alternatively attach it as a file.

Regards,
John



More information about the Python-list mailing list