aboutsummaryrefslogtreecommitdiff
path: root/challenge-034/paulo-custodio/python
diff options
context:
space:
mode:
authorConor Hoekstra <codereport@outlook.com>2021-12-24 22:16:15 -0500
committerConor Hoekstra <codereport@outlook.com>2021-12-24 22:16:15 -0500
commitc804b128cf740d95b244cd3fe15e86d0820ab51e (patch)
treeb9db7c3a5f93e6a83640f56bfd5decb9bbe8dbfe /challenge-034/paulo-custodio/python
parent116add27d0d74360c7d4ad26b12d972657e51afa (diff)
parent6f518c687f743b68d3eeddedcf3d831aca20d4ec (diff)
downloadperlweeklychallenge-club-c804b128cf740d95b244cd3fe15e86d0820ab51e.tar.gz
perlweeklychallenge-club-c804b128cf740d95b244cd3fe15e86d0820ab51e.tar.bz2
perlweeklychallenge-club-c804b128cf740d95b244cd3fe15e86d0820ab51e.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-034/paulo-custodio/python')
-rw-r--r--challenge-034/paulo-custodio/python/ch-1.py11
-rw-r--r--challenge-034/paulo-custodio/python/ch-2.py45
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-034/paulo-custodio/python/ch-1.py b/challenge-034/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..d7855da4e7
--- /dev/null
+++ b/challenge-034/paulo-custodio/python/ch-1.py
@@ -0,0 +1,11 @@
+#!/usr/bin/python3
+
+# Challenge 034
+#
+# Task #1
+# Contributed by Dave Cross
+# Write a program that demonstrates using hash slices and/or array slices.
+
+data = {'oranges':3, 'pears':2, 'apples':4}
+values = [data[x] for x in 'oranges apples'.split()]
+print(*values, sep=", ")
diff --git a/challenge-034/paulo-custodio/python/ch-2.py b/challenge-034/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..87134c16dc
--- /dev/null
+++ b/challenge-034/paulo-custodio/python/ch-2.py
@@ -0,0 +1,45 @@
+#!/usr/bin/python3
+
+# Challenge 034
+#
+# Task #2
+# Contributed by Dave Cross
+# Write a program that demonstrates a dispatch table.
+
+import sys
+
+# simple rpn calculator
+stack = []
+def add():
+ b = stack.pop()
+ a = stack.pop()
+ stack.append(a+b)
+
+def sub():
+ b = stack.pop()
+ a = stack.pop()
+ stack.append(a-b)
+
+def mul():
+ b = stack.pop()
+ a = stack.pop()
+ stack.append(a*b)
+
+def div():
+ b = stack.pop()
+ a = stack.pop()
+ stack.append(a/b)
+
+def prt():
+ print(stack.pop())
+
+dispatch = {'+': add, '-':sub, '*':mul, '/':div, '.':prt}
+
+prog = "".join(sys.argv[1:])
+for c in prog:
+ if c.isspace():
+ pass
+ elif c.isdigit():
+ stack.append(int(c))
+ else:
+ dispatch[c]()