Reputation:
I'm solving a hackerrank
problem where I can modify and write function code only but I can't modify drivers code (taking input and printing output). I wrote the code but it prints each character of a string in a new line as output. the problem is : to check balanced parenthesis and print "YES"
or "NO"
as output. How can I return the word where I can only modify the function code .
def braces(values):
open_tup = tuple('({[')
close_tup = tuple(')}]')
map = dict(zip(open_tup, close_tup))
queue = []
for i in values:
if i in open_tup:
queue.append(map[i])
elif i in close_tup:
if not queue or i != queue.pop():
return "NO"
return "YES"
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
values_count = int(input())
values = []
for _ in range(values_count):
values_item = input()
values.append(values_item)
res = braces(values)
fptr.write('\n'.join(res))
fptr.write('\n')
fptr.close()
output:
Y
E
S
Upvotes: 0
Views: 348
Reputation: 43189
The problem is your join
call.
Instead of
fptr.write('\n'.join(res))
you need to write
fptr.write(''.join(res))
Additionally, consider using with
instead:
with open(os.environ['OUTPUT_PATH'], 'w') as fptr:
fptr.write(''.join(res))
Upvotes: 0