f = open("summatest.01.sis", "rt")
n = (f.readline())

jada_elemendid = f.readline().strip().split(' ')
otstav_summa = f.readline().strip()
f.close()

# Valjund
vfal = file("sum.val", "w")

lmargid = []

def otsi(k):
  if (k < n):
    print n
    k = k + 1
    lmargid.append("+")
    if otsi(k):
      return True
    lmargid.append("-")
    print lmargid
  else:
    z = jada_elemendid[0]
    for i in range(n + 1):
      if lmargid.index(i) == "+":
        z = z + jada_elemendid.index(i)
      else:
        z = z - jada_elemendid.index(i)
    if z == otstav_summa:
      vfal.write(jada_elemendid[0])
      for ii in xrange(n + 1):
        vfal.write(jada_elemendid(ii))
      vfal.write("=" + z) 
      return True
  return False    
     
if not otsi(0):
  vfal.write("EI OLE")
File "summa.py", line 17, in otsi
    if (k < n):
RuntimeError: maximum recursion depth exceeded in cmp

Input:
summa.sis
3
15 25 30
10
Output:
summa.val
15+25-30=10

You are comparing the generator n to integer instead of reading values from it, I do not understand. Mitä häh?

Ok, that was not generator, but unnecessarry parenthesis at line 2. Still strange:

f = open("summatest.01.sis", "rt")
n = (f.readline())

jada_elemendid = f.readline().strip().split(' ')
otstav_summa = f.readline().strip()
f.close()

# Valjund
vfal = file("sum.val", "w")

lmargid = []

def otsi(k):
  if (k < n): # n is the first line of file f, 
    print n # n is after k in alphabetic order or
    ## allways true if k is number and n is string ('number' < 'string')
    k = k + 1 # ??? k is number?
    lmargid.append("+")
    if otsi(k): # recursion grown k, 
      return True
    lmargid.append("-")
    print lmargid
  else: # 
    z = jada_elemendid[0]
    for i in range(n + 1):
      if lmargid.index(i) == "+":
        z = z + jada_elemendid.index(i)
      else:
        z = z - jada_elemendid.index(i)
    if z == otstav_summa:
      vfal.write(jada_elemendid[0])
      for ii in xrange(n + 1):
        vfal.write(jada_elemendid(ii))
      vfal.write("=" + z) 
      return True
  return False    
     
if not otsi(0):
  vfal.write("EI OLE")

Debug with lines 19 and 20 commented out. Kommentoi pois rivit 19 ja 20 ja etsi virhe.

Edited 6 Years Ago by pyTony: n/a

Assuming k is an integer, it will never be less than "n" because "n" is a string, as already stated. Print both "k" and "n" to test.

def otsi(k):
  if (k < n): 
    print "k < ", k, n 
#
#     instead of recursion you should be using a while loop
#     recursion is rarely, if ever, used in actual code
def otsi(k):
##  while (k < n):
##  or even better
    for k in range(n):
        print k, n
        lmargid.append("+")
        lmargid.append("-")
        print lmargid
#
# or even, even better
lmargid.append("+-"*n)

Edited 6 Years Ago by woooee: n/a

This article has been dead for over six months. Start a new discussion instead.