Re: [Tutor] declaring list

2006-01-11 Thread Logesh Pillay
hello list

I asked yesterday how we can declare an array in python.  Thanks Brian
van den Broek and Frank Schley for your responses.

A[None] * n works for me.

To answer Brian's question, I was writing the standard backtracking
procedure to find the permutations of 1..n taken r at a time.

def permutations (n, r):
A, Used = [None] * (r+1), [False] * (n+1)
def choose (m):
if m  r:
print A[1:],
else:
for j in range (1, n+1):
if not Used [j]:
Used [j] = True
A[m] = j
choose (m+1)
Used [j] = False
choose (1)

Thanks 
Logesh Pillay

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] declaring list in python

2006-01-10 Thread Logesh Pillay
Hello list

I want to declare a list of a specific size as global to some nested
function like so

def foo (n):
A[] (of size n)
def foo1 
...

The only way I can think to declare the list is to use dummy values:
A = [0] * n

A = [] * n doesn't work.  [] * n = []

I'd prefer not to use dummy values I have no use for.  Is there any way?

Thanks

Logesh Pillay


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] permutations using list comprehensions

2005-05-01 Thread Logesh Pillay
Dear list
I was really impressed by this version of quicksort:-
def qsort (t):
   if len (t) == 0:
   return []
   else:
   return qsort([x for x in t[1:] if x = t[0]]) + [t[0]] + 
qsort([x for x in t[1:] if x  t[0]])

I'm trying to produce something of a similar structure to generate the 
permutations of a sequence by translating the ffg bit of Haskell:-
perms [] = [[]]
perms xs = [ x : ps | x - xs , ps - perms ( xs\\[x]) ]

'\\' applies to lists  means elements of the first list excluding any 
elements in common with the second list.

The best I've been able to do is pretty obvious:-
def perms (t):
   if len (t) == 0:
   return []
   else:
   return [[x] + perms ([y for y in t if y  x]) for x in t]
  
Needless to say, it doesn't work.  It does produce a list of lists but 
they are so horribly nested that I've no idea whether I am getting 
close.  I've also tried adding and removing square brackets in various 
combinations.

Any ideas?
Logesh
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] variable scope in nested functions

2005-04-25 Thread Logesh Pillay
Thanks Kent for your reply.
You said
This is a limitation of Python's nested scopes. You can't assign to a variable in an enclosing 
scope. One way to work around this is to use a mutable value like a list in the enclosing scope:
def foo (n):
   counter = [0]
   def choose (i):
  if (solution found):
 counter[0] += 1
 print counter[0], (solution)
  else choose (i+1)
   choose (1)

I can't help thinking that this is a little unsatisfactory and suggests 
(with all due respect)  an unfinished quality to Python.

Consider a program I've just translated into python from my version in C.
def erdos(n): #Explores 
Erdos theorem that any
   found, r, s = False, 1, [0]*1000#integer can be 
expressed as +-1.1
   def choose (d, k):  #+-2.2...+-r.r 
for some r and some
   for i in -1, 1:   
#combinations of +'s and -'s
   s[d] = i
   if d  r:
   choose (d+1, k - i*d*d)
   elif k == i*d*d:
   found = True
   printout ()
   def printout ():
   print n, '=',
   for i in range (1, r+1):
   if s[i] == 1:
   print '+', i,'.',i,
   else:
   print '-',i,'.',i,
   print '\n'
   while not found:
   choose(1, n)
   r += 1

The program is supposed to return to the python prompt as soon as it 
finds solution(s) at the smallest width r.

I entered it exactly as it is expecting the same sort of error message 
for the boolean variable 'found'.  There was none.  Instead python 
simply fails to perform 'found = True' in choose() and it just keeps 
running (incidentally demonstrating the other part of this Erdos theorem 
that the no. can be  so  expressed  in an infinite number of ways).  
Why  the inconsistency  in handling enclosing scope variables of type 
'int' and 'bool'?  Also, it can't be desirable that the interpreter 
effectively ignores a line of code without warning.

Logesh
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] variable scope in nested functions

2005-04-24 Thread Logesh Pillay
Hello list
I am having trouble with a variable to act as a counter in a nested 
recursive function which will only occasionally find an answer.  
Something like a static variable in C.

Why does this sort of thing not work?
def foo (n):
   counter = 0
   def choose (i):
  if (solution found):
 counter += 1
 print counter, (solution)
  else choose (i+1)
   choose (1)
I get an error message that counter is referenced before being declared.
Declaring counter as global does not help either
Incidentally how does one nest comments in python.  I want to comment 
out part of a line.  Is it just Kate my editor which won't allow it

Thanks
Logesh

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] quicksort using list comprehension

2005-04-09 Thread Logesh Pillay
I'm trying to program quicksort using list comprehension.
The following gives me a type mismatch error for +.
def qsort (t):
if len (t)  1:
return qsort([x for x in t[1:] if x = t[0]]) + [t[0]] + qsort([x  
for x in t[1:] if x  t[0]])

I know this sounds strange but I have and idea it (or something similar)  
worked when I last tried Python a yr or so ago. An earlier version of  
Python?

Logesh Pillay
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor