# 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  ## 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:  #### Calculating the length of a new array. If we add an element, +1, if we perform an operation, -1  #### 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.  #### 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.
author | maxg |
---|---|
permlink | learn-data-structures-the-insane-way-reverse-polish-notation-calculator-in-google-sheets |
category | programming |
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"} |
created | 2017-12-22 02:36:30 |
last_update | 2017-12-22 02:36:30 |
depth | 0 |
children | 3 |
last_payout | 2017-12-29 02:36:30 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 13.704 HBD |
curator_payout_value | 4.284 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 4,058 |
author_reputation | 5,859,582,764,121 |
root_title | "Learn Data Structures the Insane Way - Reverse Polish Notation Calculator in Google Sheets" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 24,622,390 |
net_rshares | 2,928,381,335,504 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
pharesim | 0 | 106,532,230,235 | 0.03% | ||
lafona-miner | 0 | 161,664,503,938 | 5% | ||
hr1 | 0 | 50,444,350,797 | 0.07% | ||
kushed | 0 | 4,784,758,025 | 2% | ||
xeroc | 0 | 883,232,062 | 0.6% | ||
steem-id | 0 | 6,458,031,449 | 0.6% | ||
blakemiles84 | 0 | 1,482,124,545 | 0.6% | ||
justtryme90 | 0 | 214,473,936,932 | 8.5% | ||
g-dubs | 0 | 10,817,914,375 | 8% | ||
anwenbaumeister | 0 | 51,046,398,966 | 2% | ||
phenom | 0 | 534,281,022 | 0.6% | ||
bitcoiner | 0 | 3,316,421,659 | 0.6% | ||
cryptoninja | 0 | 85,021,226 | 1% | ||
toxichan | 0 | 451,677,833 | 2% | ||
timsaid | 0 | 7,195,345,668 | 2% | ||
kyriacos | 0 | 20,136,435,395 | 5% | ||
lemouth | 0 | 13,459,425,251 | 5% | ||
rjbauer85 | 0 | 333,276,142 | 10% | ||
lamouthe | 0 | 1,236,473,615 | 10% | ||
dan-bn | 0 | 4,533,667,185 | 0.6% | ||
steemedia | 0 | 213,319,003 | 2% | ||
curie | 0 | 100,318,830,857 | 2% | ||
cebymaster | 0 | 264,763,389 | 2% | ||
hendrikdegrote | 0 | 1,080,678,433,384 | 2% | ||
steemstem | 0 | 108,228,465,151 | 10% | ||
cotidiana | 0 | 237,487,749 | 2% | ||
foundation | 0 | 917,702,054 | 10% | ||
the-devil | 0 | 2,247,149,053 | 10% | ||
dna-replication | 0 | 2,768,099,139 | 10% | ||
pacokam8 | 0 | 71,257,767 | 0.4% | ||
awesomianist | 0 | 525,574,700 | 2% | ||
jamhuery | 0 | 2,771,783,764 | 10% | ||
kingyus | 0 | 115,328,723 | 2.5% | ||
mobbs | 0 | 15,345,445,633 | 10% | ||
shahzadnisar | 0 | 205,851,784 | 2% | ||
bp423 | 0 | 596,686,120 | 2% | ||
diggerdugg | 0 | 187,198,404 | 2% | ||
dber | 0 | 8,253,109,861 | 10% | ||
kerriknox | 0 | 31,215,016,871 | 10% | ||
alexander.alexis | 0 | 685,187,794 | 3% | ||
sieses | 0 | 216,818,867 | 2% | ||
erangvee | 0 | 1,212,597,638 | 2% | ||
rockeynayak | 0 | 65,064,460 | 10% | ||
nitesh9 | 0 | 2,170,322,749 | 10% | ||
himal | 0 | 436,205,627 | 10% | ||
ewuoso | 0 | 306,194,750 | 3% | ||
ratticus | 0 | 397,410,461 | 1.6% | ||
ovij | 0 | 1,353,688,023 | 10% | ||
gamesjoyce | 0 | 347,209,476 | 2% | ||
azirgraff | 0 | 4,227,554,960 | 4% | ||
somethingburger | 0 | 427,684,201 | 10% | ||
pearlumie | 0 | 2,359,517,298 | 10% | ||
webresultat | 0 | 148,233,887 | 2% | ||
buildawhale | 0 | 816,697,878,451 | 0.56% | ||
imamalkimas | 0 | 123,837,993 | 10% | ||
rachelsmantra | 0 | 515,962,256 | 10% | ||
gra | 0 | 3,383,398,823 | 10% | ||
kedi | 0 | 2,608,961,713 | 10% | ||
jasimg | 0 | 3,352,687,367 | 2% | ||
mindhacks | 0 | 347,446,461 | 10% | ||
raci | 0 | 105,386,656 | 2% | ||
ongoingwow | 0 | 396,410,542 | 8% | ||
xanderslee | 0 | 140,946,123 | 2% | ||
amavi | 0 | 1,322,681,260 | 2% | ||
geekpowered | 0 | 4,240,829,952 | 2% | ||
mrswhale | 0 | 58,575,043,793 | 15.38% | ||
melanie00 | 0 | 270,502,175 | 2% | ||
gentleshaid | 0 | 2,146,778,502 | 10% | ||
manupadillaph | 0 | 66,678,065 | 2% | ||
maxg | 0 | 3,259,485,978 | 100% | ||
mhelmstetter | 0 | 422,196,880 | 100% | ||
oldbutgold | 0 | 1,019,522,597 | 100% |
<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>
author | buildawhale |
---|---|
permlink | re-maxg-learn-data-structures-the-insane-way-reverse-polish-notation-calculator-in-google-sheets-20171222t045901279z |
category | programming |
json_metadata | {"tags":["programming"],"app":"drotto/0.0.2d"} |
created | 2017-12-22 04:59:18 |
last_update | 2017-12-22 04:59:18 |
depth | 1 |
children | 0 |
last_payout | 2017-12-29 04:59:18 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 380 |
author_reputation | 25,326,953,051,701,791 |
root_title | "Learn Data Structures the Insane Way - Reverse Polish Notation Calculator in Google Sheets" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 24,637,427 |
net_rshares | 0 |
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
author | mrswhale |
---|---|
permlink | 20171222t043030683z |
category | programming |
json_metadata | {"tags":["thanks"],"app":"steemjs/test"} |
created | 2017-12-22 04:30:33 |
last_update | 2017-12-22 04:30:33 |
depth | 1 |
children | 0 |
last_payout | 2017-12-29 04:30:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 178 |
author_reputation | 821,454,268,798 |
root_title | "Learn Data Structures the Insane Way - Reverse Polish Notation Calculator in Google Sheets" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 24,634,456 |
net_rshares | 0 |
<center></center>
author | steemstem |
---|---|
permlink | re-maxg-learn-data-structures-the-insane-way-reverse-polish-notation-calculator-in-google-sheets-20171222t085900116z |
category | programming |
json_metadata | {"tags":["programming"],"image":["https://s18.postimg.org/kazxz8qsp/steem_up.jpg"],"app":"steemit/0.1"} |
created | 2017-12-22 08:59:00 |
last_update | 2017-12-22 08:59:00 |
depth | 1 |
children | 0 |
last_payout | 2017-12-29 08:59:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 68 |
author_reputation | 262,017,435,115,313 |
root_title | "Learn Data Structures the Insane Way - Reverse Polish Notation Calculator in Google Sheets" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 24,663,103 |
net_rshares | 0 |