Skip to content

Instantly share code, notes, and snippets.

@wowoto9772
Last active October 19, 2016 10:41
Show Gist options
  • Save wowoto9772/40b2abbb306de1b11e5177a0a7edb0be to your computer and use it in GitHub Desktop.
Save wowoto9772/40b2abbb306de1b11e5177a0a7edb0be to your computer and use it in GitHub Desktop.
#-*- coding: utf-8 -*-
def chooseIngredients(source):
# erase style tag
# \displaystyle, \scriptstyle
remv = ["\displaystyle", "\scriptstyle"]
for rem in remv:
source = source.replace(rem, "")
source = source.replace(" ", " ")
source = source.replace("{ {", "{{")
source = source.replace("} }", "}}")
return source
def handle(ingredients):
# erase useless parentheses
ingredients = chooseIngredients(ingredients)
length = len(ingredients)
chk = [0] * length
stk = []
pairs = []
for i in xrange(0, length):
if ingredients[i] == '{':
stk.append(i)
elif ingredients[i] == '}':
l = stk.pop()
r = i
pairs.append([l,r])
plength = len(pairs)
for i in xrange(0, plength-1):
if pairs[i][0]-1 == pairs[i+1][0]:
if pairs[i][1]+1 == pairs[i+1][1]:
chk[pairs[i][0]] = 1
chk[pairs[i][1]] = 1
dessert = ""
for i in xrange(0, length):
if chk[i] == 0:
dessert += ingredients[i]
return dessert
# for test
#source = "{ {\displaystyle V^{5}+W^{{{5}}}+X^{{5}}+Y^{5}+Z^{5}=0.\,}}"
#source = "{\displaystyle s'(x)=\sum _{k=1}^{\infty }f_{k}'(x)}"
#source = "p_{n}(x)=\sum _{{k=0}}^{n}a_{{n,k}}x^{k}\ {\mbox{and}}\ q_{n}(x)=\sum _{{k=0}}^{n}b_{{n,k}}x^{k}."
#print(source)
#print(handle(source))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment