[PLUG] Python Guru Here?
Rich Shepard
rshepard at appl-ecosys.com
Tue Jul 12 18:03:51 UTC 2005
On Tue, 12 Jul 2005, Russell Senior wrote:
> This is where an example of what you are putting-in/getting-out would be
> most helpful. There are a thousand ways you could be screwing up, please
> don't make us guess which.
I didn't think it appropriate for the full list, and thought that
plug-programming was dead. Anywho, this is the current error message:
[rshepard at salmo ~/data/fuzzyEI-assessor/code]$ ./scoping-pairs.py 10 2
Traceback (most recent call last):
File "./scoping-pairs.py", line 61, in ?
combos(m, n)
NameError: name 'm' is not defined
The source file is attached. I know it's a simple error of the head
slapping kind.
Rich
--
Dr. Richard B. Shepard, President | Author of "Quantifying Environmental
Applied Ecosystem Services, Inc. (TM) | Impact Assessments Using Fuzzy Logic"
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
-------------- next part --------------
#!/usr/bin/python
"""
Takes a list of candidate environmental components and creates an
output file of all combinations of components. Assumes input list
name of 'components.in' and produces 'components.out'.
"""
import sys
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
def klistcombs(s, k):
"""Returns list of combinations of S taken K at a time.
S must be a list.
K must be >= 0.
If K > len(S), an empty list is returned.
Else if K is 0, a singleton list containing an empty list
is returned.
The k-combinations are returned in lexicographic order
of their indices.
"""
if k < 0:
raise ValueError, "arg k == %d not >= 0" % k
if len(s) < k:
return []
if k == 0:
return [[]]
answer = []
indices = range(k)
indices[-1] = indices[-1] - 1
while 1:
limit = len(s) - 1
i = k - 1
while i >= 0 and indices[i] == limit:
i = i - 1
limit = limit - 1
if i < 0:
break
val = indices[i]
for i in xrange( i, k ):
val = val + 1
indices[i] = val
temp = []
for i in indices:
temp.append( s[i] )
answer.append( temp )
return answer
def combos(m, n):
num = factorial(m)
denom = factorial(n) * factorial(m-n)
c = num / denom
return c
if __name__ == '__main__':
combos(m, n)
print num, denom, c
More information about the PLUG
mailing list