Last active
August 1, 2022 00:54
-
-
Save fadhilaf/02b561e5693f512781a70704d937ad66 to your computer and use it in GitHub Desktop.
My approach to question "Regular Bracket Sequences" codeforces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def incrementNextRightSlotAlgorithm(n): | |
if n == 1: | |
yield [1] | |
elif n == 0: | |
yield [] | |
else: | |
for x in range(n,0,-1): | |
for y in incrementNextRightSlotAlgorithm(n-x): | |
yield([x]+y) | |
def toBracket(n): | |
if n == 1: | |
yield '()' | |
else: | |
for x in bracketPatternMaker(n-1): | |
yield('('+ x +')') | |
def slotObjects(array): | |
objK = [toBracket(n) for n in array] | |
#the reason we dont iterate the first slotObject is | |
#because the slotIterator method will do the | |
#iteration to the object at the first index | |
valK = [objK[0]] + [next(a) for a in objK[1:]] | |
outputHistory = [ [] ] + [ [valK[n]] for n in range(1, len(valK)) ] | |
#because the slot other than the first slot already iterated, | |
#the pointer for the slot other tan the first get index 1. | |
#and the first get index 0 | |
outputHistoryPointer = [ 0 ] + [1 for n in range(1, len(outputHistory))] | |
for x in slotIterator(objK,valK,outputHistory,outputHistoryPointer): | |
yield ''.join(x) | |
#remember, passing list in python in pass by reference, | |
#so we don't need to bother returning the changed value | |
#of the list too | |
def slotIterator(obj,val,outputHistory,outputHistoryPointer,n=0): | |
while not n == len(obj): | |
try: | |
try: | |
val[n] = outputHistory[n][outputHistoryPointer[n]] | |
except IndexError: | |
val[n] = next(obj[n]) | |
outputHistory[n].append(val[n]) | |
#need to add the pointer before the yield | |
#if not, the pointer won't change | |
outputHistoryPointer[n] += 1 | |
yield val | |
except StopIteration: | |
outputHistoryPointer[n] = 0 | |
val[n] = outputHistory[n][0] | |
try: | |
#after the other slot finished gives all possible pattern, | |
#this slot have to move to the | |
outputHistoryPointer[n] += 1 | |
yield next(slotIterator(obj,val,outputHistory,outputHistoryPointer,n+1)) | |
except StopIteration: | |
break | |
def bracketPatternMaker(n): | |
for x in incrementNextRightSlotAlgorithm(n): | |
for y in slotObjects(x): | |
yield y | |
#input | |
print("Input the number of brackets: ") | |
data = dict() | |
while True: | |
x = int(input()) | |
if not x in data: | |
data[x] = bracketPatternMaker(x) | |
try: | |
n = 0; | |
while True: | |
pattern = next(data[x]) | |
print(pattern) | |
n += 1 | |
except StopIteration: | |
data[x] = bracketPatternMaker(x) | |
print("The total of mathematically valid pattern for ", x, " bracket is :", n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def incrementNextRightSlotAlgorithm(n): | |
if n == 1: | |
yield [1] | |
elif n == 0: | |
yield [] | |
else: | |
for x in range(n,0,-1): | |
for y in incrementNextRightSlotAlgorithm(n-x): | |
yield([x]+y) | |
def toBracket(n): | |
if n == 1: | |
yield '()' | |
else: | |
for x in bracketPatternMaker(n-1): | |
yield('('+ x +')') | |
def slotObjects(array): | |
objK = [toBracket(n) for n in array] | |
#the reason we dont iterate the first slotObject is | |
#because the slotIterator method will do the | |
#iteration to the object at the first index | |
valK = [objK[0]] + [next(a) for a in objK[1:]] | |
for x in slotIterator(objK,valK,array): | |
yield ''.join(x) | |
def slotIterator(obj,val,lis,n = 0): | |
while not n == len(obj): | |
try: | |
val[n] = next(obj[n]) | |
yield val | |
except StopIteration: | |
obj[n] = toBracket(lis[n]) | |
val[n] = next(obj[n]) | |
try: | |
yield next(slotIterator(obj,val,lis,n+1)) | |
except StopIteration: | |
break | |
def bracketPatternMaker(n): | |
for x in incrementNextRightSlotAlgorithm(n): | |
for y in slotObjects(x): | |
yield y | |
#input | |
print("Input the number of brackets: ") | |
data = dict() | |
while True: | |
x = int(input()) | |
if not x in data: | |
data[x] = bracketPatternMaker(x) | |
try: | |
n = 0; | |
while True: | |
print(next(data[x])) | |
n += 1 | |
except StopIteration: | |
data[x] = bracketPatternMaker(x) | |
print("The total of mathematically valid pattern for ", x, " bracket is :", n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment