Skip to main content
added 1246 characters in body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102

Python 2, 290 272 268 250 243 238 bytes

Now finally shorter than the JS answer!

This is a full program which uses a basic implementation of the shunting yard algorithm. Input is given as a quoted string, and the result is printed to STDOUT.

import re O=[];B=[] for t in re.findall('\d+|\S',input()):exec("O=[t]+O","i=O.index('(');B+=O[:i];O=O[i+1:]","while O and'('<O[0]and(t in'*/')<=(O[0]in'*/'):B+=O.pop(0)\nO=[t]+O","B+=`int(t)`,")[(t>'/')+(t>')')+(t>'(')] print' '.join(B+O) 

###Try it online!


Explanation:

The first thing we need to do is convert the input into tokens. We do this using by finding all matches of the regex \d+|\S, roughly translated to "any group of digits, and any non-space character". This removes whitespace, parses adjacent digits as single tokens, and parses operators seperately.

For the shunting yard algorithm, there are 4 distinct token types we need to handle:

  • ( - Left parenthesis
  • ) - Right parenthesis
  • +-*/ - Operators
  • 9876543210 - Numeric literals

Thankfully, the ASCII codes of these are all grouped in the order shown, so we can use the expression (t>'/')+(t>')')+(t>'(') to calculate the token type. This results in 3 for digits, 2 for operators, 1 for a right parenthesis and 0 for a left parenthesis.

Using these values, we index into the large tuple after exec to get the corresponding snippet to execute, based on the token type. This is different for each token and is the backbone of the shunting yard algorithm. Two lists are used (as stacks): O (operation stack) and B (output buffer). After all the tokens have been run, the remaining operators on the O stack are concatenated with the output buffer, and the result is printed.

Python 2, 290 272 268 250 243 238 bytes

Now finally shorter than the JS answer!

This is a full program which uses a basic implementation of the shunting yard algorithm. Input is given as a quoted string, and the result is printed to STDOUT.

import re O=[];B=[] for t in re.findall('\d+|\S',input()):exec("O=[t]+O","i=O.index('(');B+=O[:i];O=O[i+1:]","while O and'('<O[0]and(t in'*/')<=(O[0]in'*/'):B+=O.pop(0)\nO=[t]+O","B+=`int(t)`,")[(t>'/')+(t>')')+(t>'(')] print' '.join(B+O) 

###Try it online!

Python 2, 290 272 268 250 243 238 bytes

Now finally shorter than the JS answer!

This is a full program which uses a basic implementation of the shunting yard algorithm. Input is given as a quoted string, and the result is printed to STDOUT.

import re O=[];B=[] for t in re.findall('\d+|\S',input()):exec("O=[t]+O","i=O.index('(');B+=O[:i];O=O[i+1:]","while O and'('<O[0]and(t in'*/')<=(O[0]in'*/'):B+=O.pop(0)\nO=[t]+O","B+=`int(t)`,")[(t>'/')+(t>')')+(t>'(')] print' '.join(B+O) 

###Try it online!


Explanation:

The first thing we need to do is convert the input into tokens. We do this using by finding all matches of the regex \d+|\S, roughly translated to "any group of digits, and any non-space character". This removes whitespace, parses adjacent digits as single tokens, and parses operators seperately.

For the shunting yard algorithm, there are 4 distinct token types we need to handle:

  • ( - Left parenthesis
  • ) - Right parenthesis
  • +-*/ - Operators
  • 9876543210 - Numeric literals

Thankfully, the ASCII codes of these are all grouped in the order shown, so we can use the expression (t>'/')+(t>')')+(t>'(') to calculate the token type. This results in 3 for digits, 2 for operators, 1 for a right parenthesis and 0 for a left parenthesis.

Using these values, we index into the large tuple after exec to get the corresponding snippet to execute, based on the token type. This is different for each token and is the backbone of the shunting yard algorithm. Two lists are used (as stacks): O (operation stack) and B (output buffer). After all the tokens have been run, the remaining operators on the O stack are concatenated with the output buffer, and the result is printed.

deleted 534 characters in body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102

Python 2, 290 272 268 250 243243 238 bytes

Now finally shorter than the JS answer!

This is a full program which uses a basic implementation of the shunting yard algorithm. Input is given as a quoted string, and the result is printed to STDOUT.

import re O=[];B=[] for t in re.findall('\d+|.''\d+|\S',input()): if'/'<t:B+=`intexec(t)`"O=[t]+O", elif'"i=O.index('(')'<t;B+=O[: whilei];O=O[i+1:]","while O and"and'("<O[0]and'<O[0]and(t in'*/')<=(O[0]in'*/'):B+=O.pop(0) O=[t]+O O=[t]*\nO=[t]+O","B+=`int(t)`,")[(t>'/')+('==tt>')+O if'')'==t:i=O.index+('t>'(');B+=O[:i];O=O[i+1:] print' '.join(B+O) 

###Try it online!

Some interesting features of this answer:Try it online!

  • re.findall('\d+|.',input()) this uses regex to tokenize the input - matching any group of consecutive digits as one token, and any other character as a 1-byte operator token. Although this also parses whitespace as tokens, they have no effect in the main loop.
  • Later on, to check whether a token is an integer, we do '/'<t. As all other math symbols in this challenge have ASCII code-points below '/', this will only evaluate to True for integers.

Python 2, 290 272 268 250 243 bytes

Now finally shorter than the JS answer!

This is a full program which uses a basic implementation of the shunting yard algorithm. Input is given as a quoted string, and the result is printed to STDOUT.

import re O=[];B=[] for t in re.findall('\d+|.',input()): if'/'<t:B+=`int(t)`, elif')'<t: while O and"("<O[0]and(t in'*/')<=(O[0]in'*/'):B+=O.pop(0) O=[t]+O O=[t]*('('==t)+O if')'==t:i=O.index('(');B+=O[:i];O=O[i+1:] print' '.join(B+O) 

###Try it online!

Some interesting features of this answer:

  • re.findall('\d+|.',input()) this uses regex to tokenize the input - matching any group of consecutive digits as one token, and any other character as a 1-byte operator token. Although this also parses whitespace as tokens, they have no effect in the main loop.
  • Later on, to check whether a token is an integer, we do '/'<t. As all other math symbols in this challenge have ASCII code-points below '/', this will only evaluate to True for integers.

Python 2, 290 272 268 250 243 238 bytes

Now finally shorter than the JS answer!

This is a full program which uses a basic implementation of the shunting yard algorithm. Input is given as a quoted string, and the result is printed to STDOUT.

import re O=[];B=[] for t in re.findall('\d+|\S',input()):exec("O=[t]+O","i=O.index('(');B+=O[:i];O=O[i+1:]","while O and'('<O[0]and(t in'*/')<=(O[0]in'*/'):B+=O.pop(0)\nO=[t]+O","B+=`int(t)`,")[(t>'/')+(t>')')+(t>'(')] print' '.join(B+O) 

###Try it online!

edited body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102

Python 2, 290 272 268 250 243 bytes

Now finally shorter than the JS answer!

This is a full program which uses a basic implementation of the shunting yard algorithm. Input is given as a quoted string, and the result is printed to STDOUT.

import re O=[];B=[] for t in re.findall('\d+|.',input()): if'/'<t:B+=`int(t)`, elif')'<t: while O and"("<O[0]and(t in'*/')<=(O[0]in'*/'):B+=O.pop(0) O=[t]+O if'O=[t]*('('==t:O=[t]+O)+O if')'==t:i=O.index('(');B+=O[:i];O=O[i+1:] print' '.join(B+O) 

###Try it online!Try it online!

Some interesting features of this answer:

  • re.findall('\d+|.',input()) this uses regex to tokenize the input - matching any group of consecutive digits as one token, and any other character as a 1-byte operator token. Although this also parses whitespace as tokens, they have no effect in the main loop.
  • Later on, to check whether a token is an integer, we do '/'<t. As all other math symbols in this challenge have ASCII code-points below '/', this will only evaluate to True for integers.

Python 2, 290 272 268 250 243 bytes

Now finally shorter than the JS answer!

This is a full program which uses a basic implementation of the shunting yard algorithm. Input is given as a quoted string, and the result is printed to STDOUT.

import re O=[];B=[] for t in re.findall('\d+|.',input()): if'/'<t:B+=`int(t)`, elif')'<t: while O and"("<O[0]and(t in'*/')<=(O[0]in'*/'):B+=O.pop(0) O=[t]+O if'('==t:O=[t]+O if')'==t:i=O.index('(');B+=O[:i];O=O[i+1:] print' '.join(B+O) 

###Try it online!

Some interesting features of this answer:

  • re.findall('\d+|.',input()) this uses regex to tokenize the input - matching any group of consecutive digits as one token, and any other character as a 1-byte operator token. Although this also parses whitespace as tokens, they have no effect in the main loop.
  • Later on, to check whether a token is an integer, we do '/'<t. As all other math symbols in this challenge have ASCII code-points below '/', this will only evaluate to True for integers.

Python 2, 290 272 268 250 243 bytes

Now finally shorter than the JS answer!

This is a full program which uses a basic implementation of the shunting yard algorithm. Input is given as a quoted string, and the result is printed to STDOUT.

import re O=[];B=[] for t in re.findall('\d+|.',input()): if'/'<t:B+=`int(t)`, elif')'<t: while O and"("<O[0]and(t in'*/')<=(O[0]in'*/'):B+=O.pop(0) O=[t]+O O=[t]*('('==t)+O if')'==t:i=O.index('(');B+=O[:i];O=O[i+1:] print' '.join(B+O) 

###Try it online!

Some interesting features of this answer:

  • re.findall('\d+|.',input()) this uses regex to tokenize the input - matching any group of consecutive digits as one token, and any other character as a 1-byte operator token. Although this also parses whitespace as tokens, they have no effect in the main loop.
  • Later on, to check whether a token is an integer, we do '/'<t. As all other math symbols in this challenge have ASCII code-points below '/', this will only evaluate to True for integers.
added 26 characters in body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102
Loading
deleted 2 characters in body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102
Loading
deleted 6 characters in body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102
Loading
deleted 296 characters in body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102
Loading
edited body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102
Loading
added 7 characters in body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102
Loading
edited body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102
Loading
deleted 5 characters in body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102
Loading
deleted 5 characters in body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102
Loading
deleted 2 characters in body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102
Loading
deleted 1 character in body
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102
Loading
Source Link
FlipTack
  • 14.7k
  • 3
  • 56
  • 102
Loading