aboutsummaryrefslogtreecommitdiff
path: root/challenge-173
diff options
context:
space:
mode:
authorWalt Mankowski <waltman@pobox.com>2022-07-17 10:43:57 -0400
committerWalt Mankowski <waltman@pobox.com>2022-07-17 10:43:57 -0400
commit37698ededed833b0c0d49ac0e44d5d01025e8b0e (patch)
tree8b57033ad450bb12c3d3109d358f0c2ba92ab4be /challenge-173
parent30ccbd6b2aa92693bd2cd6dce4bce71ae9d7dd5e (diff)
downloadperlweeklychallenge-club-37698ededed833b0c0d49ac0e44d5d01025e8b0e.tar.gz
perlweeklychallenge-club-37698ededed833b0c0d49ac0e44d5d01025e8b0e.tar.bz2
perlweeklychallenge-club-37698ededed833b0c0d49ac0e44d5d01025e8b0e.zip
added write-up for my Python solutions
Diffstat (limited to 'challenge-173')
-rw-r--r--challenge-173/walt-mankowski/README.md30
1 files changed, 29 insertions, 1 deletions
diff --git a/challenge-173/walt-mankowski/README.md b/challenge-173/walt-mankowski/README.md
index 7a5af58509..ef2f23343d 100644
--- a/challenge-173/walt-mankowski/README.md
+++ b/challenge-173/walt-mankowski/README.md
@@ -2,6 +2,8 @@ Solutions by Walt Mankowski.
# Perl Weekly Challenge #173: Esthetic Numbers and Sylvester's Sequence
+I did this week's challenge in Perl and Python.
+
## Task #1: Esthetic Numbers
For this task we're given a number _n_ and need to determine if it's an Esthetic Number. An **Esthetic Number** is a positive integer where every pair of adjacent digits differ in value by exactly 1.
@@ -18,6 +20,12 @@ sub is_esthetic($n) {
}
```
+Python is more of a stickler about types than Perl, so to turn it into an array of digits I had to first turn it into a string. Then you can treat it as an array, and I turned each character back into an integer. That sounds like a lot of work, but it's just a one-liner with a linst comprehension:
+
+```python
+d = [int(c) for c in str(n)]
+```
+
## Task 2: Sylvester's Sequence
For this task we need to generate the first 10 terms of (Sylvester's Sequence)[https://en.wikipedia.org/wiki/Sylvester%27s_sequence]. **Sylvester's Sequence** is an integer sequence where every term is the product of all the previous terms, plus one.
@@ -31,4 +39,24 @@ use List::Util qw(product);
my @sylvester;
push @sylvester, 1 + product @sylvester while @sylvester < 10;
say for @sylvester;
-``` \ No newline at end of file
+```
+
+Python has the equivalent of `bigint` built-in, so I didn't need to do anything special there. Python has `sum` as a built-in function but it doesn't have `prod`. There are a few different ways to do write it; I chose to use the `reduce` function from the `functools` package:
+
+```python
+def product(lst):
+ return reduce(lambda x,y: x*y, lst, 1)
+```
+
+But it's so short that I decided to just write it inline. Here's my full program:
+
+```python
+from functools import reduce
+
+sylvester = []
+while len(sylvester) < 10:
+ sylvester.append(reduce(lambda x,y: x*y, sylvester, 1) + 1)
+
+for syl in sylvester:
+ print(syl)
+```