diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-07-17 17:58:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-17 17:58:13 +0100 |
| commit | 783cee96dc5dd6ac008e5122b024ff46d56e85e5 (patch) | |
| tree | c1d3865237c23a8eef465fd1689bcabe4910923f | |
| parent | b63d8d5dee6a54d5429b7c200db0b06420de52c0 (diff) | |
| parent | 45f08a5e1866678d6ac9e42e30afd3a1d5d53fe6 (diff) | |
| download | perlweeklychallenge-club-783cee96dc5dd6ac008e5122b024ff46d56e85e5.tar.gz perlweeklychallenge-club-783cee96dc5dd6ac008e5122b024ff46d56e85e5.tar.bz2 perlweeklychallenge-club-783cee96dc5dd6ac008e5122b024ff46d56e85e5.zip | |
Merge pull request #6455 from waltman/branch-for-challenge-173-python
Branch for challenge 173 python
| -rw-r--r-- | challenge-173/walt-mankowski/README.md | 28 | ||||
| -rw-r--r-- | challenge-173/walt-mankowski/python/ch-1.py | 14 | ||||
| -rw-r--r-- | challenge-173/walt-mankowski/python/ch-2.py | 9 |
3 files changed, 51 insertions, 0 deletions
diff --git a/challenge-173/walt-mankowski/README.md b/challenge-173/walt-mankowski/README.md index b9e53c1a92..798ac25682 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. @@ -32,3 +40,23 @@ my @sylvester; push @sylvester, 1 + product @sylvester while @sylvester < 10; say for @sylvester; ``` + +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) +``` diff --git a/challenge-173/walt-mankowski/python/ch-1.py b/challenge-173/walt-mankowski/python/ch-1.py new file mode 100644 index 0000000000..f987ce2eff --- /dev/null +++ b/challenge-173/walt-mankowski/python/ch-1.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +import sys + +def is_esthetic(n): + d = [int(c) for c in str(n)] + for i in range(1, len(d)): + if abs(d[i-1]-d[i]) != 1: + return False + return True + +n = int(sys.argv[1]) +print(f'{n} {"is" if is_esthetic(n) else "is not"} an esthetic number') + + diff --git a/challenge-173/walt-mankowski/python/ch-2.py b/challenge-173/walt-mankowski/python/ch-2.py new file mode 100644 index 0000000000..e12d79a883 --- /dev/null +++ b/challenge-173/walt-mankowski/python/ch-2.py @@ -0,0 +1,9 @@ +#!/usr/bin/env 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) |
