aboutsummaryrefslogtreecommitdiff
path: root/challenge-034/paulo-custodio/python
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-12-24 01:03:26 +0000
committerGitHub <noreply@github.com>2021-12-24 01:03:26 +0000
commitf13cc76081c7af1d173ee01166c8e725d3843f3a (patch)
treee0e79b1bdc232483dbb5f97a650f38b88700583b /challenge-034/paulo-custodio/python
parent776396430ae1e9e0aa85d2e78bb92e38fd1556f7 (diff)
parentb2bda687c42cfc1229440dfb0d392e5fbad8f1d1 (diff)
downloadperlweeklychallenge-club-f13cc76081c7af1d173ee01166c8e725d3843f3a.tar.gz
perlweeklychallenge-club-f13cc76081c7af1d173ee01166c8e725d3843f3a.tar.bz2
perlweeklychallenge-club-f13cc76081c7af1d173ee01166c8e725d3843f3a.zip
Merge pull request #5407 from pauloscustodio/devel
Add Perl and Python solutions to challenge 032
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]()