2

I would like to check whether this "a" and "b" is in the userInput. For example: user were to enter "hello", It will say it is not a ab word. For example: if user were to type "bag", It will say that it is a ab word. For Example: If user were to type "age", it will say that it is not a ab word(because of the missing b).

I've tried many ways, which include using counter to count how many time the alphabet is inside the string. If the "a" and "b" is inside the string, it will print "inside", else if either one of them is not inside or both no inside, it will print "not inside"

Kindly help me out. I've tried but it can't seems to be working fine.

def tryword(x):        
    if x == '':
        return 0
    lastNum= x[-1]
    if (lastNum == "a" and "b"):
        return tryword(x[:-1]) + 1
    else:
        return tryword(x[:-1]) + 0

if recursiveWord >= 0: 
    print "inside"
else:
    print" not inside"

print tryword("abcdef")
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
stack
  • 414
  • 1
  • 7
  • 21

2 Answers2

2

You need to add two accumulator parameters to keep track of if you found each character. One boolean for each.

def trywordp(x, foundA, foundB):
  if foundA and foundB: return True # Positive base case: Both found!
  if x == "": return False # Negative base case: String exhausted
  if x[0] == 'a': return trywordp(x[1:], True, foundB) # First character in string is 'a', continue checking next character
  if x[0] == 'b': return trywordp(x[1:], foundA, True) # Ditto for 'b'
  return trywordp(x[1:], foundA, foundB) # Neither 'a' nor 'b', continue looking

def tryword(x):
  return trywordp(x, False, False) # We have not found any of them yet so start with foundA and foundB as False

if tryword("stringwithaandb"):
  print "inside"
else:
  print "not inside"
Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
  • if tryword("bella"): print "inside" else: print "not inside" if it's seperate, it will print that it is not inside. However, it should be inside.since "a" and "b" is both inside the string – stack Oct 08 '15 at 09:11
  • I forgot the last recursive call. Fixed the function now! – Emil Vikström Oct 08 '15 at 09:20
  • re you online? can help me to check somthing through discussion chat? – stack Oct 08 '15 at 11:39
0

Check your condition

if (lastNum == "a" and "b"):

your probably mean something like

if (lastNum == "a" or lastNum == "b"):

But this won't work either. You could use magic numbers.

def tryword(x):        
    if x == '':
        return 0
    lastNum= x[-1]
    if (lastNum == "a"):
        return tryword(x[:-1]) | 1
    if (lastNum == "b"):
        return tryword(x[:-1]) | 2
    return tryword(x[:-1])

 assert tryword("qwer") == 0
 assert tryword("a") == 1
 assert tryword("b") == 2
 assert tryword("bb") == 2
 assert tryword("ab") == 3
 assert tryword("abababab") == 3

 def check_inside(s):
      if tryword(s) == 3:
           print "inside"
      else:
           print "not inside"
AlexN
  • 1,613
  • 8
  • 21
  • That won't work: because 2 `a`'s will match as well. So you need to resolve other errors as well. – Willem Van Onsem Oct 08 '15 at 09:06
  • what I would like to achieve in my output is basically.. input: "bella" output: inside input: "bag" output: inside input: "nothing" output "not inside" – stack Oct 08 '15 at 09:19
  • just return the string? or what do mean by the term input + output? – AlexN Oct 08 '15 at 09:21