From 679bb10fda9f1e6eaa1eba9f3910cbcc57dd8ce8 Mon Sep 17 00:00:00 2001 From: B Date: Thu, 31 Oct 2019 15:08:02 -0400 Subject: [PATCH 1/6] Fix subset checks --- README.md | 10 +++++----- rpn.py | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5ad6894..57e6940 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ The program takes inputs that are separated by space (or return in interactive m ### Stack pushes -Funcions that pushes something into the stack. +Functions that pushes something into the stack. |Operation|Syntax|Description| |---|---|---| @@ -25,7 +25,7 @@ Funcions that pushes something into the stack. |π|`pi`|| |Euler's number|`e`|| -### System funcions +### System functions |Operation|Syntax|Description| |---|---|---| @@ -50,7 +50,7 @@ Functions that pops off the *x* and *y* buffers and pushes the product based on |---|---|---| |Add|`a`, `+`, `add`, `plus`|| |Subtract|`s`, `-`, `sub`, `subtact`, `minus`| -|Muliply|`m`, `*`, `mul`, `multiply`, `times`|| +|Multiply|`m`, `*`, `mul`, `multiply`, `times`|| |Divide|`d`, `/`, `:`, `div`, `divide`, `over`|| |Power|`p`, `^`, `**`, `pow`, `raised`, `expo`, `exponent`|| |Modulus|`mod`, `modulus`|| @@ -60,13 +60,13 @@ Functions that pops off the *x* and *y* buffers and pushes the product based on ### *x* buffer change -Funcions that takes what is stored in the *x* buffer and changes it. Trigonometric funcions returns number in radians. +Functions that takes what is stored in the *x* buffer and changes it. Trigonometric functions returns number in radians. |Operation|Syntax|Description| |---|---|---| |Ceil|`cel`, `ceil`|| |Floor|`flr`,`floor`|| -|Round|`rnd`, `round`|Rounds to nearest ingeger| +|Round|`rnd`, `round`|Rounds to nearest integer| |Invert|`inv`, `inverse`, `invert`|1 / x| |Absolute value|`abs`, `absolute`|| |Factorial|`fac`, `factorial`|| diff --git a/rpn.py b/rpn.py index 56b2422..e0df372 100755 --- a/rpn.py +++ b/rpn.py @@ -162,20 +162,20 @@ def exe(self, raw): print(item[0] + ': ' + InteractiveMode().prettify(item[1])) # functions - elif cmd == ['swp', '<>', '><', 'swap']: + elif cmd in ['swp', '<>', '><', 'swap']: self.stack['x'], self.stack['y'] = self.stack['y'], self.stack['x'] elif cmd in ['rld', 'roll', 'rolld', 'rolldown']: self.stack.roll() elif cmd in ['rlu', 'rollu', 'rollup']: self.stack.roll(1) - elif cmd == ['sto', 'store']: + elif cmd in ['sto', 'store']: self.regtable[next(ln)] = self.stack['x'] - elif cmd == ['rcl', 'recall']: + elif cmd in ['rcl', 'recall']: self.stack.push(self.regtable[next(ln)]) elif cmd in ['clr', 'clear', 'clean', 'erase']: self.stack = Stack() - elif cmd == '': + elif cmd in '': pass else: From 9b713f9741b7665745bffc9ff8ff8d1cabc367ae Mon Sep 17 00:00:00 2001 From: B Date: Thu, 31 Oct 2019 15:30:41 -0400 Subject: [PATCH 2/6] Add option to show full stack at each prompt --- README.md | 1 + rpn.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 57e6940..8a0677a 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ Functions that pushes something into the stack. |---|---|---| |Quit|`q`, `quit`, `exit`|Quits the program| |Print|`print`, `ls`|Prints the stack| +|Stack show|`ss`, `ts`|Toggles the option to print the full stack at the interactive prompt| ### Stack manipulation and memory diff --git a/rpn.py b/rpn.py index e0df372..cfa3c56 100755 --- a/rpn.py +++ b/rpn.py @@ -4,6 +4,9 @@ import sys import random +show_full_stack=False +showed_stack=False + class Stack(): def __init__(self): self.list = [] @@ -81,6 +84,7 @@ def setdefault(self): self.angle = 'deg' def exe(self, raw): + global showed_stack, show_full_stack ln = iter(raw.split(' ')) for cmd in ln: if re.search('^-?(\d+\.?\d*|\.\d+)$', cmd): @@ -160,6 +164,9 @@ def exe(self, raw): elif cmd in ['print', 'ls']: for item in self.stack.gettable(): print(item[0] + ': ' + InteractiveMode().prettify(item[1])) + showed_stack=1 + elif cmd in ['ss', 'ts']: + show_full_stack = not show_full_stack # functions elif cmd in ['swp', '<>', '><', 'swap']: @@ -188,8 +195,14 @@ def __init__(self): self.rpn = RPN() def run(self): + global showed_stack, show_full_stack while True: - print('\x1b[2m x: \x1b[0m' + self.prettify(self.rpn.stack['x'])) + if not showed_stack: + if show_full_stack: + self.rpn.exe('ls') + else: + print('\x1b[2m x: \x1b[0m' + self.prettify(self.rpn.stack['x'])) + showed_stack=False try: ret = self.rpn.exe(input(' > ')) except Exception as e: From 282b90880ed6110c2dba54fb1e8c8004d1583aa7 Mon Sep 17 00:00:00 2001 From: B Date: Fri, 1 Nov 2019 02:09:38 -0400 Subject: [PATCH 3/6] Add last x --- README.md | 3 ++- rpn.py | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8a0677a..afa4db4 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,8 @@ Functions that pushes something into the stack. |Roll down|`rld`, `roll`, `rolld`, `rolldown`|Rolls the stack down| |Roll up|`rlu`, `rollu`, `rollup`|Rolls the stack up| |Store|`sto`, `store`|Takes one argument that sets the storage location| -|Recall|`rcl`, `recall`|Pushes the numbes stored in a argument position| +|Recall|`rcl`, `recall`|Pushes the numbers stored in an argument position| +|Last X|`lastx`, `lx`|Pushes the value of the *x* buffer before the last calculation| ### Stack drop diff --git a/rpn.py b/rpn.py index cfa3c56..b135310 100755 --- a/rpn.py +++ b/rpn.py @@ -10,6 +10,7 @@ class Stack(): def __init__(self): self.list = [] + self.lastx = float(0) def push(self, item): if len(self.list) < 1 and item == 0: return @@ -22,6 +23,7 @@ def pop(self): return retval def drop(self, func): x = self.pop() + self.lastx = x y = self.pop() try: self.push(func(x, y)) @@ -30,6 +32,7 @@ def drop(self, func): self.push(x) self.push(y) def change(self, func): + self.lastx = self.list[0] self.push(func(self.pop())) def __getitem__(self, key): if type(key) == slice: raise IndexError('No stack slicing') @@ -181,7 +184,9 @@ def exe(self, raw): self.stack.push(self.regtable[next(ln)]) elif cmd in ['clr', 'clear', 'clean', 'erase']: self.stack = Stack() - + elif cmd in ['lastx', 'lx']: + last=self.stack.lastx + self.stack.push(last) elif cmd in '': pass else: From 9a562caded10d41a75b5cea5bb79bf9d0cc7ef39 Mon Sep 17 00:00:00 2001 From: B Date: Tue, 18 Aug 2020 19:17:53 -0400 Subject: [PATCH 4/6] Add a nice-to-have dup command --- rpn.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rpn.py b/rpn.py index b135310..227fa7a 100755 --- a/rpn.py +++ b/rpn.py @@ -184,6 +184,10 @@ def exe(self, raw): self.stack.push(self.regtable[next(ln)]) elif cmd in ['clr', 'clear', 'clean', 'erase']: self.stack = Stack() + elif cmd in ['dup']: + x=self.stack.pop() + self.stack.push(x) + self.stack.push(x) elif cmd in ['lastx', 'lx']: last=self.stack.lastx self.stack.push(last) From 9c6f476c831347ee5cc758bdaa54ebcbac29b7af Mon Sep 17 00:00:00 2001 From: B Date: Fri, 24 Jun 2022 17:30:01 -0400 Subject: [PATCH 5/6] =?UTF-8?q?Add=20pct=20=CE=94=20command.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + rpn.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index afa4db4..9d42762 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ Functions that pops off the *x* and *y* buffers and pushes the product based on |Greatest common divider|`gcd`|| |Logarithm|`log`, `logarithm`|The *x* buffer is the base| |Root|`r`, `root`|The *x* buffer is the root| +|Root|`pct`|Percent change from *x* to *y*: (y-x)/x| ### *x* buffer change diff --git a/rpn.py b/rpn.py index 227fa7a..0d2ec5a 100755 --- a/rpn.py +++ b/rpn.py @@ -112,6 +112,8 @@ def exe(self, raw): self.stack.drop(lambda x, y: math.log(y, x)) elif cmd in ['r', 'root']: self.stack.drop(lambda x, y: math.pow(y, 1/x)) + elif cmd in ['pct']: + self.stack.drop(lambda x, y: (y-x)/x) # change elif cmd in ['cel', 'ceil']: From 25eaa69be8e7934324c133365ef9588f9d1f8fa0 Mon Sep 17 00:00:00 2001 From: b-k Date: Fri, 24 Jun 2022 18:08:33 -0400 Subject: [PATCH 6/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9d42762..59e72de 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ Functions that pops off the *x* and *y* buffers and pushes the product based on |Greatest common divider|`gcd`|| |Logarithm|`log`, `logarithm`|The *x* buffer is the base| |Root|`r`, `root`|The *x* buffer is the root| -|Root|`pct`|Percent change from *x* to *y*: (y-x)/x| +|Percent change|`pct`|Percent change from *x* to *y*: (y-x)/x| ### *x* buffer change