chevron_left chevron_right
Login Register invert_colors photo_library


Stay updated and chat with others! - Join the Discord!
Thread Rating:
  • 0 Vote(s) - 0 Average


Silver Challenge (2) Simple Equation Parser filter_list
Author
Message
Challenge (2) Simple Equation Parser #1
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

Reply

RE: Challenge (2) Simple Equation Parser #2
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.

[+] 1 user Likes Inori's post
Reply

RE: Challenge (2) Simple Equation Parser #3
(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
(This post was last modified: 12-21-2015, 06:13 AM by m0dem.)

Reply

RE: Challenge (2) Simple Equation Parser #4
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

[+] 1 user Likes emad.shaaban92's post
Reply

RE: Challenge (2) Simple Equation Parser #5
(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.
In the grayness of the world, i'm colorful.

Reply

RE: Challenge (2) Simple Equation Parser #6
(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

Reply

RE: Challenge (2) Simple Equation Parser #7
(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!

Reply






Users browsing this thread: 1 Guest(s)