Skip to content

Instantly share code, notes, and snippets.

@Himan10
Created April 23, 2021 09:47
Show Gist options
  • Save Himan10/c217ef4798e5fd54316cd22deb0523f8 to your computer and use it in GitHub Desktop.
Save Himan10/c217ef4798e5fd54316cd22deb0523f8 to your computer and use it in GitHub Desktop.
get the data stored in all the curly braces pairs
def get_data_from_file(file_path):
# Read File
with open(file_path, 'r') as file:
data = file.read()
return data
def check_paranthesis(data: str):
""" Check if the string contains valid opening/closing tag """
stack = []
for i in data:
if i in ['{', '(', '[', '<']:
stack.append(i)
elif i in ['}', ')', ']', '>']:
if not stack:
return False
tag = stack.pop()
if (
tag == '{' and i != '}' or
tag == '(' and i != ')' or
tag == '[' and i != ']' or
tag == '<' and i != '>'
):
return False
if stack:
return False
return True
def get_curly_data(data: str):
""" Get the data in every {} pairs """
collection = []
def helper(i, j, data, start):
text = ''
paren = start
while j < len(data):
if data[j] == '{':
paren = True
k = helper(j, j+1, data, paren)
text = data[i+1:k+1]
j = k
elif data[j] == '}':
collection.append(text)
text = ''
return j
else:
if paren:
text += data[j]
j += 1
if check_paranthesis(data):
helper(0, 0, data, False)
return collection
def main():
file_content = get_data_from_file('./discordProblem.txt')
result = get_curly_data(file_content)
for i in result:
print(i.strip(), end='\n------\n')
state={
id=1
name="STATE_1" # Corsica
manpower = 322900
state_category = town
history={
owner = FRA
victory_points = { 3838 1 }
buildings = {
infrastructure = 4
industrial_complex = 1
air_base = 1
3838 = {
naval_base = 3
}
}
add_core_of = FRA
}
provinces={
3838 9851 11804
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment