0
def acceptable(s=' ')
   b = s.count('x')
   c = s.count('o')
   if b == c:
       return True
   else:
       return False

def HT(n, s='', result=[]):
    if n == 0:
        result.append(s)
    elif n > 0:
        for c in [ 'x', 'o' ]:
            result = HT(n-1, s+c, result)
    return result

def main():
    n = 4
    for s in HT(n):
        print s

main()'

i'm having a problem of where to call my first function in the second function. The program is supposed to display xxxx, xxxo, xxoo (ect until all have possibilities have been exhausted). acceptable is supposed to only make it display the the ones where there are equal amount of x's and o's. But since my professor used recursion in the code he gave us in the second function, I don't know where to place the call for the first function to not display certain lines of code, because wherever I put it, it doesn't work.

Thanks in advance for any help.

Kayla
  • 87
  • 9
  • 1
    Aside: it's bad practice to use mutable objects (like lists) as default values, as in `result=[]`-- if your professor wrote that function, he should [know better](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument). – DSM Oct 27 '14 at 02:45
  • yeah he probably should. so what can I do? – Kayla Oct 27 '14 at 02:47
  • @DSM : Normally, it's a Bad Thing™, but in this case it's ok. But as I mention in my answer it's a good idea to comment code like that so people reading the code know that you know what you're doing. :) – PM 2Ring Oct 27 '14 at 07:20

1 Answers1

0

Just use it to test each string in the list returned by HT():

#! /usr/bin/env python

def acceptable(s=' '):
    b = s.count('x')
    c = s.count('o')
    return b == c

def HT(n, s='', result=[]):
    if n == 0:
        result.append(s)
    elif n > 0:
        for c in [ 'x', 'o' ]:
            result = HT(n-1, s+c, result)
    return result

def main():
    n = 4
    for s in HT(n):
        if acceptable(s):
            print s

if __name__ == '__main__':
    main()

output

xxoo
xoxo
xoox
oxxo
oxox
ooxx

You will see that I've simplified acceptable() slightly (and put the missing : on the end of its def statement). Doing

if someCondition:
    return True
else:
    return False

Is overly verbose & cluttered compared to

return someCondition

FWIW, we can simplify acceptable() even further:

def acceptable(s):
    return 2 * s.count('x') == len(s)

...

On your professor's use of result=[] in the definition of HT(): Yes, that sort of thing can lead to errors, but in this situation it's actually a useful way to give this recursive function a list where it can accumulate its results. But because this technique can be dangerous it's good practice to comment the code so that people will know that you are intentionally using this Python "feature".

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • You don't need to have a default value of `[]` to give it a list recursively. The usual idiom is to write `result=None` and then `if result is None: result = []` inside. – DSM Oct 29 '14 at 19:12
  • @DSM : Good point. And I usually do that in my programs... :mumble: :) – PM 2Ring Oct 30 '14 at 01:39