Re: [Tutor] printing tree structure

2009-07-03 Thread karma
Thanks all for the feedback. As you all rightly pointed out, I was confusing myself by not keeping a track of the recursion depth. Thanks again 2009/7/3 Alan Gauld : > > "karma" wrote > >> thinking that a recursive solution would work here, but so far I can't &g

[Tutor] printing tree structure

2009-07-03 Thread karma
Hi all , I have a nested list in the structure [root,[leftSubtree],[RightSubtree]] that I want to print out. I was thinking that a recursive solution would work here, but so far I can't quite get it working. This is what I have so far: Can someone suggest whether this is suited to a recursive sol

Re: [Tutor] Eliminating consecutive duplicates in a list

2009-06-18 Thread karma
/recipes/52560/ > > Robert > > > On Thu, 2009-06-18 at 15:15 +0200, karma wrote: >> I was playing around with eliminating duplicates in a list not using >> groupby. From the two solutions below, which is more "pythonic". >> Alternative solutions would be

[Tutor] Eliminating consecutive duplicates in a list

2009-06-18 Thread karma
I was playing around with eliminating duplicates in a list not using groupby. From the two solutions below, which is more "pythonic". Alternative solutions would be welcome. Thanks x=[1,1,1,3,2,2,2,2,4,4] [v for i,v in enumerate(x) if x[i]!=x[i-1] or i==0] [x[i] for i in range(len(x)-1) if i==0

Re: [Tutor] variable within function retains value

2009-06-18 Thread karma
Excellent, thanks for that answer Kent. Also thanks for the link 2009/6/18 Kent Johnson : > On Thu, Jun 18, 2009 at 6:21 AM, karma wrote: >> Hi All, >> >> I'm trying to write a function that flattens a list. However after I >> call the function more than once, it

[Tutor] variable within function retains value

2009-06-18 Thread karma
Hi All, I'm trying to write a function that flattens a list. However after I call the function more than once, it appends the result (a list) from the second call with the first. I can get around it by either setting the list to an empty one before calling the function, but I would like to keep it

Re: [Tutor] Need help solving this problem

2009-06-10 Thread karma
Hi Raj, I'm another learner, I used the following: >>> def toss(n): heads = 0 for i in range(n): heads += random.randint(0,1) return heads, n-heads >>> print "%d heads, %d tails" % toss(100) Best of luck in your python endeavors! 2009/6/10 Raj Medhekar :