id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

peter@norvig.com pemdas > pemdas_tokenize
prev  |  next  |  chance

It is tedious to type in expressions like "[2, '*', 'x']", We'd like to use the more familiar "2*x". This exercise and the next few will get us there. Remember the PEMDAS rule (Parens first, then Exponentiation, then Multiplication and Division, then Addition and Subtraction)? We're going to use it, over the course of the next few exercises, to turn an expression written as a string of characters into an expression consisting of nested components. In this part, we do what is called tokenization: breaking up the string of characters into pieces called tokens: each is a variable name, a number, or an operator: +, -, *, /, or parentheses. The numbers should be actual numbers, not strings. That is, the string "2" should be transformed to the number 2. Everything else should stay as a string.


pemdas_tokenize('2*x') → [2, '*', 'x']
pemdas_tokenize('a*x^2 +b*x + c') → ['a', '*', 'x', '^', 2, '+', 'b', '*', 'x', '+', 'c']
pemdas_tokenize('3 + 4*5') → [3, '+', 4, '*', 5]

...Save, Compile, Run (ctrl-enter)

def pemdas_tokenize(string):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 533 Post-solution available

Copyright Nick Parlante 2017 - privacy