-
Notifications
You must be signed in to change notification settings - Fork 0
/
150 Evaluate Reverse Polish Notation .rtf
69 lines (66 loc) · 5.71 KB
/
150 Evaluate Reverse Polish Notation .rtf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{\rtf1\ansi\ansicpg1252\cocoartf2706
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fnil\fcharset0 HelveticaNeue-Medium;\f1\fnil\fcharset0 HelveticaNeue;\f2\fnil\fcharset0 Menlo-Regular;
}
{\colortbl;\red255\green255\blue255;\red29\green29\blue29;\red255\green255\blue255;\red29\green29\blue29;
\red12\green99\blue153;\red0\green0\blue0;\red210\green50\blue85;\red135\green135\blue135;\red85\green138\blue3;
\red135\green91\blue44;\red255\green255\blue255;\red93\green108\blue125;\red133\green0\blue67;}
{\*\expandedcolortbl;;\cssrgb\c14902\c14902\c14902;\cssrgb\c100000\c100000\c100000;\cssrgb\c14902\c14902\c14902;
\cssrgb\c0\c46667\c66667;\cssrgb\c0\c0\c0;\cssrgb\c86667\c29020\c40784;\cssrgb\c60000\c60000\c60000;\cssrgb\c40000\c60000\c0;
\cssrgb\c60392\c43137\c22745;\cssrgb\c100000\c100000\c100000\c50196;\cssrgb\c43922\c50196\c56471;\cssrgb\c60000\c0\c33333;}
\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\deftab720
\pard\pardeftab720\partightenfactor0
\f0\fs30 \cf2 \cb3 \expnd0\expndtw0\kerning0
\outl0\strokewidth0 \strokec2 150\'a0Evaluate Reverse Polish Notation
\f1\fs26 \cf0 \cb1 \kerning1\expnd0\expndtw0 \outl0\strokewidth0 \
\pard\pardeftab720\partightenfactor0
\f2 \cf5 \expnd0\expndtw0\kerning0
\outl0\strokewidth0 \strokec5 class\cf0 \strokec6 \cf7 \strokec7 Solution\cf8 \strokec8 :\cf0 \strokec6 \
\cf5 \strokec5 def\cf0 \strokec6 \cf7 \strokec7 evalRPN\cf8 \strokec8 (\cf0 \strokec6 self\cf8 \strokec8 ,\cf0 \strokec6 tokens\cf8 \strokec8 :\cf0 \strokec6 List\cf8 \strokec8 [\cf9 \strokec9 str\cf8 \strokec8 ])\cf0 \strokec6 \cf10 \cb11 \strokec10 ->\cf0 \cb1 \strokec6 \cf9 \strokec9 int\cf8 \strokec8 :\cf0 \strokec6 \
\cf12 \strokec12 #Neetcode easy implmentation\cf0 \strokec6 \
stack \cf10 \cb11 \strokec10 =\cf0 \cb1 \strokec6 \cf8 \strokec8 []\cf0 \strokec6 \
\
\cf5 \strokec5 for\cf0 \strokec6 n \cf5 \strokec5 in\cf0 \strokec6 tokens\cf8 \strokec8 :\cf0 \strokec6 \
\cf5 \strokec5 if\cf0 \strokec6 n \cf10 \cb11 \strokec10 ==\cf0 \cb1 \strokec6 \cf9 \strokec9 '+'\cf8 \strokec8 :\cf0 \strokec6 \
stack\cf8 \strokec8 .\cf0 \strokec6 append\cf8 \strokec8 (\cf0 \strokec6 stack\cf8 \strokec8 .\cf0 \strokec6 pop\cf8 \strokec8 ()\cf0 \strokec6 \cf10 \cb11 \strokec10 +\cf0 \cb1 \strokec6 stack\cf8 \strokec8 .\cf0 \strokec6 pop\cf8 \strokec8 ())\cf0 \strokec6 \
\cf5 \strokec5 elif\cf0 \strokec6 n \cf10 \cb11 \strokec10 ==\cf0 \cb1 \strokec6 \cf9 \strokec9 '-'\cf8 \strokec8 :\cf0 \strokec6 \
a\cf8 \strokec8 ,\cf0 \strokec6 b \cf10 \cb11 \strokec10 =\cf0 \cb1 \strokec6 stack\cf8 \strokec8 .\cf0 \strokec6 pop\cf8 \strokec8 (),\cf0 \strokec6 stack\cf8 \strokec8 .\cf0 \strokec6 pop\cf8 \strokec8 ()\cf0 \strokec6 \
stack\cf8 \strokec8 .\cf0 \strokec6 append\cf8 \strokec8 (\cf0 \strokec6 b\cf10 \cb11 \strokec10 -\cf0 \cb1 \strokec6 a\cf8 \strokec8 )\cf0 \strokec6 \
\cf5 \strokec5 elif\cf0 \strokec6 n \cf10 \cb11 \strokec10 ==\cf0 \cb1 \strokec6 \cf9 \strokec9 '*'\cf8 \strokec8 :\cf0 \strokec6 \
stack\cf8 \strokec8 .\cf0 \strokec6 append\cf8 \strokec8 (\cf0 \strokec6 stack\cf8 \strokec8 .\cf0 \strokec6 pop\cf8 \strokec8 ()\cf0 \strokec6 \cf10 \cb11 \strokec10 *\cf0 \cb1 \strokec6 stack\cf8 \strokec8 .\cf0 \strokec6 pop\cf8 \strokec8 ())\cf0 \strokec6 \
\cf5 \strokec5 elif\cf0 \strokec6 n \cf10 \cb11 \strokec10 ==\cf0 \cb1 \strokec6 \cf9 \strokec9 '/'\cf8 \strokec8 :\cf0 \strokec6 \
a\cf8 \strokec8 ,\cf0 \strokec6 b \cf10 \cb11 \strokec10 =\cf0 \cb1 \strokec6 stack\cf8 \strokec8 .\cf0 \strokec6 pop\cf8 \strokec8 (),\cf0 \strokec6 stack\cf8 \strokec8 .\cf0 \strokec6 pop\cf8 \strokec8 ()\cf0 \strokec6 \
stack\cf8 \strokec8 .\cf0 \strokec6 append\cf8 \strokec8 (\cf9 \strokec9 int\cf8 \strokec8 (\cf0 \strokec6 b\cf10 \cb11 \strokec10 /\cf0 \cb1 \strokec6 a\cf8 \strokec8 ))\cf0 \strokec6 \
\cf5 \strokec5 else\cf8 \strokec8 :\cf0 \strokec6 \
stack\cf8 \strokec8 .\cf0 \strokec6 append\cf8 \strokec8 (\cf9 \strokec9 int\cf8 \strokec8 (\cf0 \strokec6 n\cf8 \strokec8 ))\cf0 \strokec6 \
\cf5 \strokec5 return\cf0 \strokec6 stack\cf8 \strokec8 [\cf13 \strokec13 0\cf8 \strokec8 ]\cf0 \strokec6 \
\
\cf12 \strokec12 # stack = []\cf0 \strokec6 \
\cf12 \strokec12 # if len(tokens) == 1 and not in ['+','-','*','/']:\cf0 \strokec6 \
\cf12 \strokec12 # return int(tokens[0])\cf0 \strokec6 \
\cf12 \strokec12 # for n in tokens:\cf0 \strokec6 \
\cf12 \strokec12 # if n in ['+','-','*','/']:\cf0 \strokec6 \
\cf12 \strokec12 # op1 = int(stack.pop())\cf0 \strokec6 \
\cf12 \strokec12 # op2 = int(stack.pop())\cf0 \strokec6 \
\cf12 \strokec12 # if n == '+':\cf0 \strokec6 \
\cf12 \strokec12 # newNum = op2 + op1 \cf0 \strokec6 \
\cf12 \strokec12 # elif n == '-':\cf0 \strokec6 \
\cf12 \strokec12 # newNum = op2 - op1\cf0 \strokec6 \
\cf12 \strokec12 # elif n == '*':\cf0 \strokec6 \
\cf12 \strokec12 # newNum = op2 * op1\cf0 \strokec6 \
\cf12 \strokec12 # elif n == '/':\cf0 \strokec6 \
\cf12 \strokec12 # newNum = int(op2 / op1)\cf0 \strokec6 \
\cf12 \strokec12 # stack.append(newNum)\cf0 \strokec6 \
\
\cf12 \strokec12 # else:\cf0 \strokec6 \
\cf12 \strokec12 # stack.append(n)\cf0 \strokec6 \
\
\
\cf12 \strokec12 # ans = stack.pop()\cf0 \strokec6 \
\cf12 \strokec12 # return ans\cf0 \strokec6 \
\pard\pardeftab720\partightenfactor0
\cf0 \outl0\strokewidth0 \
\
\pard\pardeftab560\slleading20\partightenfactor0
\f1 \cf0 \kerning1\expnd0\expndtw0 \
}