Sinisterly
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! Smile

<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>

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"]}
A "simple equation" -- an equation having only one operator and two operands. (e.g. 5*2 ... not 5*2+1) <- (JUST MY DEFINITION FOR THIS ARTICLE)

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"}}}
Order of operations is not required.

<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>

I will complete this challenge when I get some time! Wink2 ... (away from the xBox) xD


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)

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

Eww... I hate one-liners. Wink2 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. Wink


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)

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.

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:
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

Sweet!