Re: An algorithm problem
- From: John Machin <sjmachin@xxxxxxxxxxx>
- Date: Wed, 31 May 2006 18:29:32 +1000
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 nProblem! See below.
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
number = 2**inputProblem!!
set = set()
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 = 0Please lose the (); they are redundant.
for x in l :
if( n == 0 ):
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
.
- References:
- An algorithm problem
- From: Bo Yang
- An algorithm problem
- Prev by Date: Re: genexp performance problem?
- Next by Date: Re: create a text file
- Previous by thread: Re: An algorithm problem
- Next by thread: problem with google api / xml
- Index(es):