Back
Close

Tokenize The Code

Statement

 Goal

Wikipedia provides an article about tokenizing (lexical analyzing): https://en.wikipedia.org/wiki/Lexical_analysis
Info (symbols enclosed in double quotes: "):
A comma is the symbol: ","
Space/whitespace is the empty character: " "
Decimal digits are: 0123456789, including a float (0.55 as an example)


Tokenize the numbers and symbols/operators using this reference:

[LIT:NUM, n], [OPR, o], [LPAREN | RPAREN]
n: a decimal
o: an operator, any of the following: + | - | / | * | ^ | %
note: LPAREN and RPAREN is equal to ( and )
Input
Variable string S: the string to tokenize
e.g. Input is
(5+3)*3
Output
String result: the result of the tokenizer
e.g. Output is
[LPAREN] [LIT:NUM, 5] [OPR, +] [LIT:NUM, 3] [RPAREN] [OPR, *] [LIT:NUM, 3]

-All tokens are made of square brackets- "[]" -that contains information: [token_type{s}, token_value]
-Token type(s) and value must be separated by a comma and a space (representation enclosed in double quotes): ", "
-Tokens are separated by a space: " "
- Return values are rounded to 2 decimal digits after the floating point if it's a decimal, rounded to no decimal digits if it's an integer. Examples:
5 -> [LIT:NUM, 5]
3.234 -> [LIT:NUM, 3.23]
7.9 -> [LIT:NUM, 7.90]
Constraints
Ignore whitespace
Example
Input
(5+3)*3
Output
[LPAREN] [LIT:NUM, 5] [OPR, +] [LIT:NUM, 3] [RPAREN] [OPR, *] [LIT:NUM, 3]

Tags
TokensLexical analysisLexersLexingTokenizing

Difficulty
Medium

Test cases
Example Test Test
Input
(5+3)*3
Output
[LPAREN] [LIT:NUM, 5] [OPR, +] [LIT:NUM, 3] [RPAREN] [OPR, *] [LIT:NUM, 3]

Example Validator Validator
Input
(3+8)*19
Output
[LPAREN] [LIT:NUM, 3] [OPR, +] [LIT:NUM, 8] [RPAREN] [OPR, *] [LIT:NUM, 19]

Small Test Test
Input
11*2^5
Output
[LIT:NUM, 11] [OPR, *] [LIT:NUM, 2] [OPR, ^] [LIT:NUM, 5]

Small Test Validator Validator
Input
58+(5*88)^9
Output
[LIT:NUM, 58] [OPR, +] [LPAREN] [LIT:NUM, 5] [OPR, *] [LIT:NUM, 88] [RPAREN] [OPR, ^] [LIT:NUM, 9]

Big Test Test
Input
5*(3+5+8)-2/9^(3%1.5)
Output
[LIT:NUM, 5] [OPR, *] [LPAREN] [LIT:NUM, 3] [OPR, +] [LIT:NUM, 5] [OPR, +] [LIT:NUM, 8] [RPAREN] [OPR, -] [LIT:NUM, 2] [OPR, /] [LIT:NUM, 9] [OPR, ^] [LPAREN] [LIT:NUM, 3] [OPR, %] [LIT:NUM, 1.50] [RPAREN]

Big Test Validator Validator
Input
15^(4*0.25)+13/(5^(3%3.5))
Output
[LIT:NUM, 15] [OPR, ^] [LPAREN] [LIT:NUM, 4] [OPR, *] [LIT:NUM, 0.25] [RPAREN] [OPR, +] [LIT:NUM, 13] [OPR, /] [LPAREN] [LIT:NUM, 5] [OPR, ^] [LPAREN] [LIT:NUM, 3] [OPR, %] [LIT:NUM, 3.50] [RPAREN] [RPAREN]

Big Number Test Test
Input
123834*49292^1813
Output
[LIT:NUM, 123834] [OPR, *] [LIT:NUM, 49292] [OPR, ^] [LIT:NUM, 1813]

Big Number Validator Validator
Input
35858+4810^2
Output
[LIT:NUM, 35858] [OPR, +] [LIT:NUM, 4810] [OPR, ^] [LIT:NUM, 2]

With Space Test Test
Input
55 * ( 3 +292) -5
Output
[LIT:NUM, 55] [OPR, *] [LPAREN] [LIT:NUM, 3] [OPR, +] [LIT:NUM, 292] [RPAREN] [OPR, -] [LIT:NUM, 5]

With Space Validator Validator
Input
68 * 5 + ( 28 +393) -69
Output
[LIT:NUM, 68] [OPR, *] [LIT:NUM, 5] [OPR, +] [LPAREN] [LIT:NUM, 28] [OPR, +] [LIT:NUM, 393] [RPAREN] [OPR, -] [LIT:NUM, 69]

Solution language

Solution

Stub generator input