recursive way to find if a string is valid - python

hey so i am trying to write a code that that tells me if a string is valid or not .
a valid string is a string that contains an equal number of "(" and ")" and each "(" must be closed by a ")"
for example
'((()()))' this is a valid string . this isn't ')( '
this is what i wrote so far :
def is_valid_paren(s, cnt=0):
if s == "":
return True
if "(" in s:
cnt += 1
if ")" in s:
cnt -= 1
return is_valid_paren(s[1:])
it doesn't give the correct output for
"(.(a)"
yet for
"p(()r((0)))"
it does
why does it sometimes work ?
oh one more thing this is to be solved only by recursion ( without the use of loops anywhere )

While I don't understand why you want to solve this problem with recursion (it's very unnatural in this case), here is a recursive solution:
def is_valid(s):
def _is_valid(s, idx):
if idx == len(s): return 0
if s[idx] == '(': return _is_valid(s, idx + 1) + 1
if s[idx] == ')': return _is_valid(s, idx + 1) - 1
return _is_valid(s, idx + 1)
return _is_valid(s, 0) == 0

You can pass down a count of pending apertures (i.e. number of unclosed parentheses) and check if the count goes below 0 (too many closures) as you go and if it ends back at zero at the end:
def validPar(s,count=0):
if count<0 : return False # too many closing
if not s: return count == 0 # must balance pending apertures
return validPar(s[1:],count+(-1,1)[s[0]=="("]) # pass down count +/- 1
print(validPar('((()()))')) # True

Recursion
Iteration is likely to be the best method of solving this, but recursion also works. To attack this problem recursively, we need a system of checking the count of each and if at any stage that count falls below zero, the parentheses will be invalid because there are more closing brackets than opening ones. This is where the tough section comes into play: we need some method of not only returning the count, but whether or not the past ones were valid, so we will have to return and save using variables like return count, valid and count, valid = is_valid_parentheses(s[1:]). The next thing we need is some over-arching function which looks at the end results and says: "is the count equal to zero, and were the parentheses valid to begin with". From there, it needs to return the result.

Related

Recursive function in python does not call itself out

The problem is formulated as follows:
Write a recursive function that, given a string, checks if the string
is formed by two halves equal to each other (i.e. s = s1 + s2, with s1
= s2), imposing the constraint that the equality operator == can only be applied to strings of length ≤1. If the length of the string is
odd, return an error.
I wrote this code in Python 2.7 that is correct (it gives me the right answer every time) but does not enter that recursive loop at all. So can I omit that call here?
def recursiveHalfString(s):
##param s: string
##return bool
if (len(s))%2==0: #verify if the rest of the division by 2 = 0 (even number)
if len(s)<=1: # case in which I can use the == operator
if s[0]==s[1]:
return True
else:
return False
if len(s)>1:
if s[0:len(s)/2] != s[len(s)/2:len(s)]: # here I used != instead of ==
if s!=0:
return False
else:
return recursiveHalfString(s[0:(len(s)/2)-1]+s[(len(s)/2)+1:len(s)]) # broken call
return True
else:
return "Error: odd string"
The expected results are True if the string is like "abbaabba"
or False when it's like anything else not similat to the pattern ("wordword")
This is a much simplified recursive version that actually uses the single char comparison to reduce the problem size:
def rhs(s):
half, rest = divmod(len(s), 2)
if rest: # odd length
raise ValueError # return 'error'
if half == 0: # simplest base case: empty string
return True
return s[0] == s[half] and rhs(s[1:half] + s[half+1:])
It has to be said though that, algorithmically, this problem does not lend itself well to a recursive approach, given the constraints.
Here is another recursive solution. A good rule of thumb when taking a recursive approach is to first think about your base case.
def recursiveHalfString(s):
# base case, if string is empty
if s == '':
return True
if (len(s))%2==0:
if s[0] != s[(len(s)/2)]:
return False
else:
left = s[1:len(s)/2] # the left half of the string without first char
right = s[(len(s)/2)+1: len(s)] # the right half without first char
return recursiveHalfString(left + right)
else:
return "Error: odd string"
print(recursiveHalfString('abbaabba')) # True
print(recursiveHalfString('fail')) # False
print(recursiveHalfString('oddstring')) # Error: odd string
This function will split the string into two halves, compare the first characters and recursively call itself with the two halves concatenated together without the leading characters.
However like stated in another answer, recursion is not necessarily an efficient solution in this case. This approach creates a lot of new strings and is in no way an optimal way to do this. It is for demonstration purposes only.
Another recursive solution that doesn't involve creating a bunch of new strings might look like:
def recursiveHalfString(s, offset=0):
half, odd = divmod(len(s), 2)
assert(not odd)
if not s or offset > half:
return True
if s[offset] != s[half + offset]:
return False
return recursiveHalfString(s, offset + 1)
However, as #schwobaseggl suggested, a recursive approach here is a bit clunkier than a simple iterative approach:
def recursiveHalfString(s, offset=0):
half, odd = divmod(len(s), 2)
assert(not odd)
for offset in range(half):
if s[offset] != s[half + offset]:
return False
return True

Returning True if the number of opening and closing parentheses are the same

I'm trying to create a function that will return true if the number of opening and closing parentheses in a string are equal. If they're not, I need it to return false. Currently this is not working for all strings. For example, paren_parity('p)(()ld))()(eog((h)k(j(m()(nc)fab)i)',) returns true as it should but paren_parity('d)p))mk)bc))j((eif(()ln)o)h((ag',) is returning True even though the number of open and closed parentheses are not the same.
def paren_parity(z):
stack = []
pushChars, popChars = "<({[", ">)}]"
for c in z :
if c in pushChars :
stack.append(c)
elif c in popChars :
if not len(stack) :
return False
else :
stackTop = stack.pop()
balancingBracket = pushChars[popChars.index(c)]
if stackTop != balancingBracket:
return False
else :
return True
return not len(stack)
You've got a couple choices. The first is really easy, just count the number of opening parens and closing and check for equality:
def paren_parity(z):
return z.count('(') == z.count(')')
But, this will pass strings like '))))((((' which you might not want. Otherwise, you can keep a count of the number of opened parens as you walk over the string. If the count ever goes negative, then return False:
def paren_parity(z):
open_count = 0
for c in z:
if c == '(':
open_count += 1
if c == ')':
open_count -= 1
if open_count < 0:
return False
return open_count == 0
Of course, this still doesn't actually work for parsing programming language text because it'll fail for things like a = 5 * ('yeah!)') (since I have a parenthesis embedded in a string). However, I'm going to assume that isn't an issue for your current problem ;-).
This is your problem:
else :
return True
If you encounter something that isn't a parenthesis, you always return True. So d( will return True as soon as you hit the d.
You can fix it by just removing the those lines.
This is easily resolved with a tail recursive function, including determining if there is a 'mismatch'.
def paren_parity(z, opening=0):
if len(z) == 0:
return opening == 0
head, *tail = z
if head == "(":
opening = opening + 1
elif head == ")":
if opening == 0:
return False
opening = opening - 1
return paren_parity(tail, opening)
opening acts as the stack you're pushing and popping from. Since you don't care as to the value, just whether it's a push or pop, holding an integer is enough.

First index appearance in a string recursive

Im trying to write a recursive function that gets as an input a string and a char. the function return the first index appearance of the char in the string. If the char doesnt appear it returns None.
I have a problem only with returning None. In my case when the char isnt in the string the function throws an error, any advice?
def char_first_index(s,c):
if len_rec(s)==0:
return None
if s[0]==c:
return 0
return 1+ char_first_index(s[1:],c)
You are creating a new slice at each iteration, and you have to add 1 for each recursion. Instead, recurse on the index:
def char_first_index(s, c, index = 0):
if len(s) == index:
return None
if s[index] == c:
return index
return char_first_index(s, c, index + 1)
If the character is not in the input, your function tries to perform 1+None, hence the error. Try this instead:
def char_first_index(s,c):
if len_rec(s)==0:
return None
if s[0]==c:
return 0
answer = char_first_index(s[1:],c)
if answer is not None:
return 1+answer
else:
return answer
Firstly I'm assuming len_rec is a recursive function that gets the length of the string; you haven't written it so I've just changed it to len() for testing.
Secondly, I'm not sure how this function is supposed to handle the character not being in the string, as that will mean trying to add None to a number.
Here is an amended function that still uses your count idea, but handles the case of a None being returned:
def char_first_index(s,c):
if len(s)==0:
return None
elif s[0]==c:
return 0
else:
count = char_first_index(s[1:], c)
if count != None:
return count + 1
else:
return None

Counting a specific string in the list in Python

I want to count 'fizz' in the list and I wrote below code and it doesn't worked. Tell me why and provide a solution. I was doing this tutorial and I have to write code as their instruction but I am unable to do what they said or maybe I misunderstood them.
count = 0
def fizz_count(x):
for string in x:
if string == 'fizz':
count = count + 1
else:
count = count
return count
If you want to check if your answer is correct then you have to check it by interpreting it here. Note that I am not calling my own function. Instead, their is an built in feature in Codecademy tutorial it checks function by calling function with some argument.
You need to have the count variable inside for loop, and you dont require an else condition
def fizz_count(x):
count=0
for string in x:
if string == 'fizz':
count = count + 1
return count
You can also write your function as
def fizz_count(x):
return x.count('fizz')
Have a look at this below code. I made some correction in it. There were some syntactical error and some indentation errors which I fixed it. Have a look at it:
def fizz_count(x):
count = 0
for string in x:
if string == 'fizz':
count = count + 1
else:
count = count
return count
str1 = fizz_count(['fizzers','fizzerlss','fizz','fizz'])
print str1
Edit: Same with more explanation due to down votes
Considering your approach with a global variable count (global, because it is not within the function), you would need to do two adaptions:
You need to make count available locally with the keyword global
Since you use a global variable, you do not need to return it because it is available anyway
Code:
count = 0
def fizz_count(x):
global count # make count available for writing in the local scope
for string in x:
if string == 'fizz':
count = count + 1
else:
count = count
Now actually using a global variable especially for such kind of tasks is not recommended, because it takes more time to load, you have to take care of the current state and stuff like that, so you rather'd introduce a local variable and pass it via the return statement as you did.
Put count = 0 into function
Code
# we put count = 0 into the function
def fizz_count(x):
count = 0 # right here
for string in x:
if string == 'fizz':
count = count + 1
else:
count = count
return count
Now a new count will be initialized to zero within the function.
Now there are two more things here:
the else statement
and the increment
Since the else statement is outside the loop it will be only executed if the if condition was never True during the loop. That has some use cases. But you'd put it in the loop usually. However, in your case it basically does not change anything, thats why it can be removed completely.
Well and the second point is that the increment can be written as count += 1 which is considered more readable, because it is less clumsy and the second count is merged into the +=
Therefore finally the function could be written like so
def fizz_count(x):
count = 0
for string in x:
if string == 'fizz':
count += 1
return count
Check also the reasonable solutions of the other answers

Keeping count in recursive function

I'm trying to figure out how to write a recursive function (with only one parameter) that returns the number of times the substring “ou” appears in the string. Where I'm confused at is that I'm not allowed to use any built-in string functions other than len, or the string operators [] and [:] for indexing and splicing. So I can't use the find built-in find function
I remember seeing something like this, but it uses two parameters and it also uses the find() method
def count_it(target, key):
index = target.find(key)
if index >= 0:
return 1 + count_it(target[index+len(key):], key)
else:
return 0
Very inefficient, but should work:
def count_it(target):
if len(target) < 2:
return 0
else:
return (target[:2] == 'ou') + count_it(target[1:])
See it working online: ideone
It's basically the same idea as the code you posted, except that it moves only one character at a time through the string instead of using find to jump ahead to the next match.
Try this, it works for the general case (any value of key, not only 'ou'):
def count_it(target, key):
if len(target) < len(key):
return 0
found = True
for i in xrange(len(key)):
if target[i] != key[i]:
found = False
break
if found:
return 1 + count_it(target[len(key):], key)
else:
return count_it(target[1:], key)

Categories