What I have is a string that looks for example like this {[]}{([])}()and I loop it through to find if it is an open or a close bracket and append it to the list.
What I wanna do now is to find which is the deepest nasting and print it out. So in this example i would print the middle {([ but I am confused how to do it. I can append the beginning of the open brackets and then reset it but how to compare couple of them though, and printing the biggest one
my code:
def is_nested(str):
stack = []
deepest =[]
index=1
open=0
for c in str:
if c == "{" or c == "(" or c == "[" or c =="<":
stack.append(c) # this is push
deepest.append(c)
open +=1
index +=1
elif c == "}":
x = stack.pop()
index +=1
if x != "{":
index -=1
x2=parens(x)
return "False: expected %s at character index %d, but received } instead." %(x2,index)
elif c == "]":
x = stack.pop()
index +=1
if x != "[":
index -=1
x2=parens(x)
return "False: expected %s at character index %d, but received ] instead." %(x2,index)
elif c == ">":
x = stack.pop()
index +=1
if x != "<":
index -=1
x2=parens(x)
return "False: expected %s at character index %d, but received > instead." %(x2,index)
elif c == ")":
x = stack.pop()
index +=1
if x != "(":
index -=1
x2=parens(x)
return "False: expected %s at character index %d, but received ) instead." %(x2,index)
check(str)
return True
def check(str):
deepest =[]
for c in str:
if c == "{" or c == "(" or c == "[" or c =="<":
deepest.append(c)
print deepest
def parens(x):
if x == "<":
return ">"
elif x =="(":
return ")"
elif x == "{":
return "}"
elif x == "[":
return "]"
print is_nested("{[()}")
print is_nested("([)")
print is_nested("{[]({})}")
print is_nested("<()>")
print is_nested("{(<)}")
Not sure if I understand your wishes correctly, but this finds the longest sequence of successive opening brackets:
In [20]: import re
In [21]: s = '{[]}{([])}()'
In [22]: max(re.findall("[\(\[\{]+",s),key=len)
Out[22]: '{(['
You should iterate and update the current number of open brackets and keep the max value that you had while looping. You can put all open brackets on a string that you use as a stack and update max with this string if the lenght is bigger than curren length of max.
OPEN = "<[({"
CLOSED = ">])}"
def is_nested(str):
stack = []
deepest =[]
for c in str:
if c in OPEN:
stack.append(c)
if len(stack)>len(deepest):
deepest.append(c)
elif c in CLOSED:
x = stack.pop()
if OPEN.index(x) != CLOSED.index(c):
return "Error"
return ''.join(deepest)
Something like this?
def is_nested(nested_str):
opening = ['(', '{', '[', '<']
closing = {'(':')','{':'}', '[':']', '<':'>'}
braces = []
depth = 0
max_depth = 0
max_depth_index = None
for index, char in enumerate(nested_str):
if char in opening:
braces.append(char)
depth += 1
elif char == closing[braces[-1]]:
braces.pop()
depth -= 1
else:
raise ValueError("This is not a valid str")
if depth > max_depth:
max_depth = depth
max_depth_index = index
return max_depth, max_depth_index
This function takes a string with only braces, and tells you the deepest level of nesting, and the index of the opening brace of the first instance of that level of nesting. As a bonus, it will raise an error if the string is miss-formed.
>>> is_nested('{[][]}')
(2, 1)
>>> is_nested('{[][<()()>]()}[]()')
(4, 5)
>>> is_nested('{(})')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "nesting.py", line 16, in is_nested
ValueError: This is not a valid str
I've defined any character other than a brace to be an error, but this could easily be changed by modifying the else condition.
def is_nested(in_str):
stack = []
deepest = tuple()
open_b = ('(','[','{','<')
close_b = (')',']','}','>')
for i in in_str:
print stack, i
if i in open_b:
stack.append(i)
if len(stack) > len(deepest):
deepest = tuple(stack)
else:
p = stack.pop()
print p
print open_b.index(p)
print close_b[open_b.index(p)]
if i != close_b[open_b.index(p)]:
raise Exception('Wrongly nested')
if len(stack) > 0:
raise Exception('Wrongly nested')
return deepest
Related
Given a string that contains only the following => ‘{‘, ‘}’, ‘(‘, ‘)’, ‘[’, ‘]’. At some places there is ‘X’ in place of any bracket. Determine whether by replacing all ‘X’s with appropriate bracket, is it possible to make a valid bracket sequence.
Examples:
Input : S = "{(X[X])}"
Output : Balanced
Input : S = "[{X}(X)]"
Output : Not balanced
I tried to work it out like this, and it works for examples above. But it doesn't work for all examples eg. (it should be balanced but it says it's not)
Input: S = "([X}])"
Output: Not balanced
I tried to work it out but i can't find a solution. Please help.
class Stack:
def __init__(self):
self.data = []
def insert(self, x):
self.data.append(x)
def empty(self):
return len(self.data) == 0
def remove(self):
if self.empty():
raise ValueError('Stack is empty.')
self.data.pop()
def top_element(self):
if self.empty():
raise ValueError('Stack is empty.')
return self.data[-1]
def is_matching(a, b):
if a == "(" and b == ")":
return True
elif a == "[" and b == "]":
return True
elif a == "{" and b == "}":
return True
elif a == "X":
return True
return False
def is_balanced(expression,elements=Stack(),ind=0):
if ind == len(expression):
return elements.empty()
pre_brackets = "([{"
post_brackets = ")]}"
char = expression[ind]
if char in pre_brackets:
elements.insert(char)
return is_balanced(expression,elements,ind+1)
elif char in post_brackets:
if elements.empty() :
return False
if not is_matching(elements.top_element(), char):
return False
elements.remove()
return is_balanced(expression,elements,ind+1)
elif char == "X":
temp = Stack()
temp.insert(char)
result = (is_balanced(expression,temp,ind+1))
if result:
return True
if elements.empty():
return False
elements.remove()
return is_balanced(expression,elements,ind+1)
expression = "([X}])"
if expression == "":
print("No brackets in expression!")
elif len(expression) % 2 != 0:
print("Not balanced")
elif is_balanced(expression):
print("Balanced")
else:
print("Not Balanced")
You can do it by recursively testing all possible replacements for an X:
def can_be_balanced(expr):
pairs = "{}[]()"
opening_brackets = pairs[::2]
closing_brackets = pairs[1::2]
closer = {o:c for o, c in zip(opening_brackets, closing_brackets)}
opener = {c:o for o, c in zip(opening_brackets, closing_brackets)}
stack = []
for item in expr:
if item in opening_brackets:
# we append opening brackets to the stack
stack.append(item)
elif item in closing_brackets:
if not stack or stack[-1] != opener[item]:
# the closing bracket doesn't match the top of the stack
return False
else:
# if it does, we remove the matching opening bracket
stack.pop()
elif item == 'X':
# X could be any of the opening brackets,
possible = list(opening_brackets)
if stack and stack[-1] in opening_brackets:
# or the closing bracket matching the top of the stack
possible.append(closer[stack[-1]])
for pos in possible:
# we replace this X, the first one remaining in expr
test_expr = expr.replace('X', pos, 1)
if can_be_balanced(test_expr):
# This is just in order to print the working solution we just found,
# you may remove these two lines
if not 'X' in test_expr:
print(test_expr)
return True
# None of the replacements for X gave a balanced expression
return False
else:
raise ValueError(f'Invalid item {item} in {expr}')
# The expression is balanced if and only if the stack ends up empty
return not stack
Testing on your sample data:
tests = [("[{X}(X)]", False),
("{(X[X])}", True),
("([X}])", True),
]
for test in tests:
print(test[0], ': should be', test[1])
print(can_be_balanced(test[0]))
print('-'*20)
correctly outputs (with the balanced expression in case it can be done):
[{X}(X)] : should be False
False
--------------------
{(X[X])} : should be True
{([[]])}
True
--------------------
([X}]) : should be True
([{}])
True
--------------------
Note that a major problem in your code is that you only test the end of the expression, starting at the position of the X. Beware also of the mutable default argument elements=Stack() that would leave you with the remnants of the previous call to the function instead of a fresh, empty Stack object.
I need to write a function that given a string with parenthesis and/or square brackets it is able to evaluate if they appear in the correct order. For example, in this string '([b])(aa)' you can see that every time a parenthesis or square bracket is open, it is closed in the correct position. However, a string like '[(a])' it is not closing the parenthesis or square brackets in the correct order as it should be '[(a)]'.
The function should return True or False depending on this correct position of both elements. I have tried the following code, but this logic seems to be infinite and it is not working if I have more than two parenthesis or square brackets opened.
def parenthesis(string):
for a in range(len(string)):
if string[a] == "(":
for b in range(a,len(string)):
if string[b] == "[":
for c in range(b,len(string)):
if string[c] == "]":
for d in range(c,len(string)):
if string[d] == ")":
return True
elif string[b] == ")":
return True
else:
return False
If I run the function over the string "([b])(aa)" it is returning false as output.
parenthesis("([b])(aa)")
How can I rewrite this function so it evaluates all the parenthesis and square brackets combinations properly?
If a right parenthesis is open before a left, you got -1 and return False
def is_balanced(string):
cnt = 0
for char in string:
if char == '(': cnt += 1
if char == ')': cnt -= 1
if cnt < 0: return False
return True if cnt == 0 else False
This is one of the stack implementations I know:
def is_balanced(s):
stack = []
for char in s:
if char == "(" or char == "{" or char == "[":
stack.append(char)
elif len(stack) <= 0:
return False
elif char == ")" and stack.pop() != "(":
return False
elif char == "]" and stack.pop() != "[":
return False
elif char == "}" and stack.pop() != "{":
return False
if len(stack) == 0:
return True
return False
This version is more DRY than the prior answer:
def is_balanced(parens: str) -> bool:
# Link: https://stackoverflow.com/a/73341167/
parens_map ={'(':')','{':'}','[':']'}
stack = []
for paren in parens:
if paren in parens_map: # is open
stack.append(paren)
elif paren in parens_map.values(): # is close
if (not stack) or (paren != parens_map[stack.pop()]):
return False
return not stack
I'm trying to write a function in similar manner as started, so that I will get what it's doing. I'm assuming this can be done with one line of code, with some fancy functions, but for the sake of practice and understanding I'm trying to come up with similar solution.
The task is the following: the function takes a text once it encounters enclosed square brackets [ word ] It should print out or return all words which are between square brackets. For example, if the text string would be "[a]n example[ string]", you are expected to print out "a string".
def string():
text = "some random text [and I need this bit of txt] but I don't know how to continue [to get this bit as well]"
for i in text:
for j in range(len(text)):
if text[j] == '[':
new = text.find(']')
return(text[j+1:new])
print(string())
Try this:
def extract(text, skip_chars=("\n", )):
output = ""
flag = False
for c in text:
if c == "]":
flag = False
if flag and not c in skip_chars:
output += c
if c == "[":
flag = True
return output
print(extract("""[a]n example[
stri
ng]"""))
# -> "a string"
def string():
result = []
text = "some random text [and I need this bit of txt] but I don't know how to continue [to get this bit as well]"
for i in text:
if i == '[':
new = text.find(']')
result.append(text[text.index(i) + 1:new])
return " ".join(result)
print(string())
def parse(source):
i = source.index("[") # throw an exception
result = ""
while i < len(source):
if s[i] == "[":
i += 1
while i < len(source):
temp = ""
if source[i] == "]":
result += temp
break;
temp += source[i]
i += 1
i += 1
return result
i'm working through a hackerrank problem: https://www.hackerrank.com/challenges/kaprekar-numbers/problem
and kept having to deal with this error:
ValueError: invalid literal for int() with base 10: ''
the line 12 of code in mine that churned out error, is in the working code too. So i don't understand why mine didn't work :( please help
this is my code:
a = input()
b = input()
l = []
for i in range(a, b + 1):
s = i*i
s = str(s)
if i == 1:
l.append(i)
length = len(s)
if length <= 1:
pass
temp = int(s[0:length/2]) + int(s[length/2:]) #error came from this line
if temp == i:
l.append(i)
else:
pass
if not l:
print "INVALID RANGE"
else:
print " ".join(map(str, l))
and this is the one that works:
def kaprekar(i):
if i == 1:
return True
s = i ** 2
s = str(s)
length = len(s)
if len(s) <= 1:
return False
temp = int(s[0:length/2]) + int(s[length/2:])
if temp == i:
return True
else:
return False
a = input()
b = input()
l = []
for i in range(a, b + 1):
if kaprekar(i):
l.append(i)
if not l:
print "INVALID RANGE"
else:
print " ".join(map(str, l))
if length <= 1:
pass
pass literally doesn't do anything, so this check doesn't do anything. Whether the length is or isn't <= 1, you're proceeding to int(s[length/2:]), which will produce said error in case the string is too short.
However, return False exits the function and the following code will not execute, so that check protects you from trying to process short strings.
I am trying to return one instead of true in python.
The code i am working on is:
delimiters = ( '()', '[]', '{}', "''", '""' )
esc = '\\'
def is_balanced(s, delimiters=delimiters, esc=esc):
stack = []
opening = tuple(str[0] for str in delimiters)
closing = tuple(str[1] for str in delimiters)
for i, c in enumerate(s):
if len(stack) and stack[-1] == -1:
stack.pop()
elif c in esc:
stack.append(-1)
elif c in opening and (not len(stack) or opening[stack[-1]] != closing[stack[-1]]):
stack.append(opening.index(c))
elif c in closing:
if len(stack) == 0 or closing.index(c) != stack[-1]:
return False
stack.pop()
return len(stack) == 0
num_cases = raw_input()
num_cases = int(num_cases)
for num in range(num_cases):
s = raw_input()
print is_balanced(s)
It basically checks whether the string typed is balanced or not. If balanced, should return 1 and if not 0.
I tried this:
1
Test string
True
It returns true. I would like it to return 1. How do i do it?
Alternatively you could cast your boolean to an int:
>>>myBoolean = True
>>>int(myBoolean)
1
>>>myBoolean = False
>>>int(myBoolean)
0
Huh? You change the code:
Instead of
return False
write
return 0
and instead of
return len(stack) == 0
write
if len(stack) == 0:
return 1
return 0
The latter 3-liner can be rewritten on a single line, but I chose the above for clarity.
return 1 if len(stack) == 0 else 0
This concisely changes the return value of is_balanced, and is equivalent to:
if len(stack) == 0:
return 1
else:
return 0
Of course you could keep is_balanced unchanged and print (in similar notation):
1 if is_balanced(s) else 0
Just use
print +is_balanced(s)
instead.