![]() |
|
Silver Challenge (2) Simple Equation Parser - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: Silver Challenge (2) Simple Equation Parser (/Thread-Silver-Challenge-2-Simple-Equation-Parser--63960) |
Challenge (2) Simple Equation Parser - m0dem - 12-19-2015 Welcome! and Merry Christmas! ![]() <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> Today you will be challenged to create a simple parser. So, your parser should take in a "simple equation" (e.g. 3+3) and output a map (Python terminology) (e.g. (in Python syntax): Code: {"+": ["3", "3"]}MORE ADVANCED VERSION: Create the same parser... but, instead, it should be able to handle an arbitrary number of operators and operands. So, for example, if you input 5+3*8 into the parser, it should output something like: Code: {"+": {"5", "*": {"3", "8"}}}<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> I will complete this challenge when I get some time! ... (away from the xBox)
RE: Challenge (2) Simple Equation Parser - Inori - 12-20-2015 Onelined it. (accepts string or list, if the input was already tokenized) Code: def parse(s): return '' if len(s)!=3 else {str(s[1]): [int(s[0]),int(s[2])]}I think it'd be better to these challenges in the coding subforum, rather than python, so people fluent in other programming languages can solve them without being off-topic I fucking hate recursive methods, and after failing at the advanced version for over an hour, I can justifiably say I'm not doing it. RE: Challenge (2) Simple Equation Parser - m0dem - 12-21-2015 (12-20-2015, 03:50 AM)Nevermore Wrote: Onelined it. (accepts string or list, if the input was already tokenized) Eww... I hate one-liners. Nice code.I should put these in the main coding subforum... I actually first started to create it there, but then I changed to the Python section for some reason. This challenge (not) sucks... it's MUCH too easy, I must formulate a more advanced version... @'Nevermore', I updated the post with a more advanced challenge.
RE: Challenge (2) Simple Equation Parser - emad.shaaban92 - 02-16-2016 Code: def parse(equation) :
operators = ["^", "*", "/", "%", "+", "-"]
result = {}
for op in operators[::-1] :
if op in equation :
result[op] = [
parse(equation[:equation.find(op)]),
parse(equation[equation.find(op)+1:])
]
return result
return equation.strip()
print parse('3 + 4 * 5')You can support more operators by adding them to the list also it respect the mathematical order of precedence RE: Challenge (2) Simple Equation Parser - Misha- - 02-16-2016 (12-20-2015, 03:50 AM)Chitoge Wrote: Onelined it. (accepts string or list, if the input was already tokenized) Your script can't solve 20+20 for example. That is still a simple equation, it just has more than 2 digits. RE: Challenge (2) Simple Equation Parser - Inori - 02-16-2016 (02-16-2016, 09:15 PM)Misha- Wrote: Your script can't solve 20+20 for example. That is still a simple equation, it just has more than 2 digits. I meant to implement regex, but I got lazy and didn't fix it RE: Challenge (2) Simple Equation Parser - m0dem - 02-17-2016 (02-16-2016, 05:45 PM)emad.shaaban92 Wrote: Sweet! |