From 30ccbd6b2aa92693bd2cd6dce4bce71ae9d7dd5e Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Sat, 16 Jul 2022 20:41:38 -0400 Subject: Python solutions to challenge 173 --- challenge-173/walt-mankowski/python/ch-1.py | 14 ++++++++++++++ challenge-173/walt-mankowski/python/ch-2.py | 9 +++++++++ 2 files changed, 23 insertions(+) create mode 100644 challenge-173/walt-mankowski/python/ch-1.py create mode 100644 challenge-173/walt-mankowski/python/ch-2.py 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) -- cgit From 37698ededed833b0c0d49ac0e44d5d01025e8b0e Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Sun, 17 Jul 2022 10:43:57 -0400 Subject: added write-up for my Python solutions --- challenge-173/walt-mankowski/README.md | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) 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) +``` -- cgit