create account

Learn Data Structures the Insane Way - Reverse Polish Notation Calculator in Google Sheets by maxg

View this thread on: hive.blogpeakd.comecency.com
· @maxg ·
$17.99
Learn Data Structures the Insane Way - Reverse Polish Notation Calculator in Google Sheets
# Learn Data Structures the Insane Way - Reverse Polish Notation Calculator in Google Sheets

People joke all the time about how tools like Google Sheets and Excel are usable for functional programming. Unfortunately, I've never seen more than a toy example. I'd like to change that. I'm going to release a series of functional programming posts using only Google Sheets. If you'd like to follow along, hit the follow button and check in every few days!

This is the second post in a series. The first one can be found [here](https://steemit.com/programming/@maxg/learn-data-structures-the-insane-way-implementing-a-stack-in-google-sheets)

## Show Me The Code

Before we get started, the finished Reverse Polish Notation Calculator implementation can be found [here](https://docs.google.com/spreadsheets/d/16g1iz1B9QvnRfKb-KVtJMLVUerg_21SprrDol7WhKNI/edit?usp=sharing). Feel free to create your own copy, mess around, etc.

What is Reverse Polish Notation?

Reverse Polish Notation is a way of writing algebraic equations so that you don't need parenthesis to determine order of operations. This is done by placing the operator after the operand. Instead of `1 + 1` you would write `1 1 +`. Then, read left to right and whenever you find an operand (`+ - / *`), evaluate it with the two numbers to the left. After evaluating a partial expression, the result replaces the partial expression. An example:
```
3 4 - 5 +  
(3 4 -) 5 +
-1 5 + 
4
```

The pseudocode for evaluating any Reverse Polish Notation expression is as follows: 
```
for each token in the postfix expression:
  if token is an operator:
    operand_2 ← pop from the stack
    operand_1 ← pop from the stack
    result ← evaluate token with operand_1 and operand_2
    push result back onto the stack
  else if token is an operand:
    push token onto the stack
result ← pop from the stack
```
Taken from Wikipedia [src](https://en.wikipedia.org/wiki/Reverse_Polish_notation#Postfix_evaluation_algorithm)

Well, last week I implemented a stack in Google Sheets, and it looks like that's all this program really needs!

## The finished Sheet
![](https://steemitimages.com/DQmV3U68YPG16FhjeGJWNBhf8YKPDycsrMUrErXwkdjvGGe/image.png)

## The main idea

Each column represents one cycle of the for loop, and we require one column for every character in the postfix expression. We decide if we are adding a new element to the new array or cutting two elements off and adding the result of performing the operation. At the end we will have an array of length one where the only element is our answer. 

## Code Snippets:
![](https://steemitimages.com/DQmShAB7gBAqTCGCwTZuR6rmNncgivXtcEtE9fPTQFWecRY/image.png)
#### Calculating the length of a new array. 
If we add an element, +1, if we perform an operation, -1


![](https://steemitimages.com/DQmcRgDJ3tg9LACpW8vnwV6oX2cfHjv4HSVAPvrDt9T1sdu/image.png)
#### Calculating the New Element
```
=IF(D8,
    IF(EXACT(D7,"-"),
       D11-D10,
       IF(EXACT(D7,"+"),
          D10+D11,
          IF(EXACT(D7,"/"),
             D11/D10,
             D10*D11)
          )
       ),
    D7)
```
Unfortunately, Google Sheets doesn't have case statements, so we end up with this chain of `IF`s. All it does is string compare the input to the string literals `"+" "-" "*" "/"` and perform the appropriate calculation, or return the input number if not an operand.


![](https://steemitimages.com/DQmfH8DqH9b7AePu1rHrkryxkN1n3zoLTepSaesPvhrkA51/image.png)
#### Calculating the new array
We cut the old array down to the appropriate length and concat it with the value generated above. Pretty straightforward stuff, but it still feels cool watching it all come together in a spreadsheet tool.

## Thanks for reading!
If you've got any ideas for posts in the future, let me know in the comments!
Play around with the calculator yourself [here](https://docs.google.com/spreadsheets/d/16g1iz1B9QvnRfKb-KVtJMLVUerg_21SprrDol7WhKNI/edit?usp=sharing)
If you have a better idea for how to share Google Sheets code, let me know. I'm not a fan of the screenshot approach.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 8 others
properties (23)
authormaxg
permlinklearn-data-structures-the-insane-way-reverse-polish-notation-calculator-in-google-sheets
categoryprogramming
json_metadata{"tags":["programming","steemstem","developer","tech","learning"],"image":["https://steemitimages.com/DQmV3U68YPG16FhjeGJWNBhf8YKPDycsrMUrErXwkdjvGGe/image.png","https://steemitimages.com/DQmShAB7gBAqTCGCwTZuR6rmNncgivXtcEtE9fPTQFWecRY/image.png","https://steemitimages.com/DQmcRgDJ3tg9LACpW8vnwV6oX2cfHjv4HSVAPvrDt9T1sdu/image.png","https://steemitimages.com/DQmfH8DqH9b7AePu1rHrkryxkN1n3zoLTepSaesPvhrkA51/image.png"],"links":["https://steemit.com/programming/@maxg/learn-data-structures-the-insane-way-implementing-a-stack-in-google-sheets","https://docs.google.com/spreadsheets/d/16g1iz1B9QvnRfKb-KVtJMLVUerg_21SprrDol7WhKNI/edit?usp=sharing","https://en.wikipedia.org/wiki/Reverse_Polish_notation#Postfix_evaluation_algorithm"],"app":"steemit/0.1","format":"markdown"}
created2017-12-22 02:36:30
last_update2017-12-22 02:36:30
depth0
children3
last_payout2017-12-29 02:36:30
cashout_time1969-12-31 23:59:59
total_payout_value13.704 HBD
curator_payout_value4.284 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,058
author_reputation5,859,582,764,121
root_title"Learn Data Structures the Insane Way - Reverse Polish Notation Calculator in Google Sheets"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id24,622,390
net_rshares2,928,381,335,504
author_curate_reward""
vote details (72)
@buildawhale ·
<p>This post has received a 0.57 % upvote from @buildawhale thanks to: @maxg.
Send at least 1 SBD to @buildawhale with a post link in the memo field for a portion of the next vote.</p>

<p>To support our daily curation initiative, please vote on my owner, @themarkymark, as a <a href="https://steemit.com/witness-category/@themarkymark/witness-themarkymark">Steem Witness</a></p>
properties (22)
authorbuildawhale
permlinkre-maxg-learn-data-structures-the-insane-way-reverse-polish-notation-calculator-in-google-sheets-20171222t045901279z
categoryprogramming
json_metadata{"tags":["programming"],"app":"drotto/0.0.2d"}
created2017-12-22 04:59:18
last_update2017-12-22 04:59:18
depth1
children0
last_payout2017-12-29 04:59:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length380
author_reputation25,326,953,051,701,791
root_title"Learn Data Structures the Insane Way - Reverse Polish Notation Calculator in Google Sheets"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id24,637,427
net_rshares0
@mrswhale ·
This wonderful post has received a @maxg 15.38% upvote from @mrswhale. Discord Channel: https://discord.gg/XG4y3mg Be sure to vote for witnesses at https://steemit.com/~witnesses
properties (22)
authormrswhale
permlink20171222t043030683z
categoryprogramming
json_metadata{"tags":["thanks"],"app":"steemjs/test"}
created2017-12-22 04:30:33
last_update2017-12-22 04:30:33
depth1
children0
last_payout2017-12-29 04:30:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length178
author_reputation821,454,268,798
root_title"Learn Data Structures the Insane Way - Reverse Polish Notation Calculator in Google Sheets"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id24,634,456
net_rshares0
@steemstem ·
<center>![](https://s18.postimg.org/kazxz8qsp/steem_up.jpg)</center>
properties (22)
authorsteemstem
permlinkre-maxg-learn-data-structures-the-insane-way-reverse-polish-notation-calculator-in-google-sheets-20171222t085900116z
categoryprogramming
json_metadata{"tags":["programming"],"image":["https://s18.postimg.org/kazxz8qsp/steem_up.jpg"],"app":"steemit/0.1"}
created2017-12-22 08:59:00
last_update2017-12-22 08:59:00
depth1
children0
last_payout2017-12-29 08:59:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length68
author_reputation262,017,435,115,313
root_title"Learn Data Structures the Insane Way - Reverse Polish Notation Calculator in Google Sheets"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id24,663,103
net_rshares0