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

 

peter@norvig.com > pemdas_parens
prev  |  next  |  chance

Now we're going to take that list of tokens and start whipping it into shape: we want to transform it into a nested expression. But this is a good time to start being careful about what we are dealing with: different functions expect different things as their arguments. We can distinguish the data types as follows: we start with the string data data, like "2*(x+1)". Then the pemdas_tokenize function transforms a string into a list of tokens: [2, '*', '(', 'x', '+', 1, ')']. Now, we will define pemdas_parens(tokens) to take a list of tokens and return a list of what we will call items: some of them will be tokens, and some of them will be lists of items, corresponding to what's inside a pair of parentheses. In other words, we take all the tokens from '(' up to and including ')', and replace them by a single item: in this case, the list ['x', '+', 1]. (In the next exercise, we will further transform the resulting item list into a full algebraic expression; in this exercise we are only handling the P of PEMDAS.)


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

def pemdas_parens(tokens):

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: 534 Post-solution available

Copyright Nick Parlante 2017 - privacy