Summary
Code golf is good. Pie is good. When you put the two together, only good stuff can happen.
Specifications
In this challenge you will manage a pie shop. The user will be able to input five different commands: list
, count
, buy
, sell
, and exit
. Here are the specifications for each:
list
Print a list of all the pies owned, and how many. Separate with
|
and pad with a space on either side.|
s must be aligned. Pie amount may be negative (that means you owe pie to someone:(
). For example:| apple | 500 | | blueberry | 2 | | cherry | -30 |
count [type]
Print how many
{{type}}
pies there are. Print "There is no{{type}}
pie!" if there is none.{{type}}
will always match the regex\w+
(i.e, it will always be a single word). For example, if I had the amount of pies shown in the above example list, then> count apple 500 > count peach There is no peach pie!
buy [n] [type]
Add
{{n}}
to the count of{{type}}
pie, and print it. Create{{type}}
pie if it does not exist.{{n}}
will always match the regex[0-9]+
(i.e, it will always be a number). Here's another example (with the same pie inventory as the previous examples):> count blueberry 2 > buy 8 blueberry 10
sell [n] [type]
Subtract
{{n}}
from the count of{{type}}
pie, and print it. Create{{type}}
pie if it does not exist. Pie can be negative (oh no, that would mean you owe someone pie!).> sell 15 blueberry -5 > buy 5 blueberry 0
exit
Print "The pie store has closed!" and exit the program.
> exit The pie store has closed!
Further clarifications
- If a non-existing function is called (the first word), then print "That's not a valid command."
- If an existing function is called with invalid arguments (the words after the first word), how your program behaves doesn't matter. "Invalid arguments" includes too many arguments, too little arguments,
{{n}}
not being a number, etc. - Pie is good.
- Your input must be distinguished from your output. If you are running the program on the command line/terminal/shell/other text-based thing, you must prefix input with "
> "
(a "greater than" sign and a space) or some other shell input prefix thing. - Pie is good.
If all of these clarifications are not good enough, here's some sample output:
> list > buy 10 apple 10 > sell 10 blueberry -10 > list | apple | 10 | | blueberry | -10 | > count apple 10 > count peach There is no peach pie! > exit The pie store has closed!
If you buy/sell pie and the net count becomes
0
, you can either keep it in thelist
or not, and you can either return0
orThere is no {{type}} pie!
when youcount
it.- This is code-golf; shortest code wins.
- Did I mention that pie is good?
buy 1 apple
andsell 1 apple
. And would it then be valid forcount apple
to return0
instead ofThere is no apple pie!
? \$\endgroup\$