aboutsummaryrefslogtreecommitdiff
path: root/challenge-107
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-04-07 19:57:42 +0200
committerAbigail <abigail@abigail.be>2021-04-07 19:57:42 +0200
commite69195920db440fcc88761e9c902de7f185c078e (patch)
tree03170eda209946296b0f126e3be2d21cd316405a /challenge-107
parentd9e6a738049b9f95eee9daec882f04e60e82a609 (diff)
downloadperlweeklychallenge-club-e69195920db440fcc88761e9c902de7f185c078e.tar.gz
perlweeklychallenge-club-e69195920db440fcc88761e9c902de7f185c078e.tar.bz2
perlweeklychallenge-club-e69195920db440fcc88761e9c902de7f185c078e.zip
Notes for week 107, part 1.
Diffstat (limited to 'challenge-107')
-rw-r--r--challenge-107/abigail/README.md61
-rw-r--r--challenge-107/abigail/perl/ch-1.pl105
2 files changed, 116 insertions, 50 deletions
diff --git a/challenge-107/abigail/README.md b/challenge-107/abigail/README.md
index ffb6203bbc..c753fe6fbd 100644
--- a/challenge-107/abigail/README.md
+++ b/challenge-107/abigail/README.md
@@ -26,6 +26,67 @@ the definition of Self-descriptive Number is
1210, 2020, 21200
~~~~
+### Notes
+
+This is a trivial exercise -- as all exercises are which do not
+take any input, and which have a fixed output. Fixed output
+challenges are boring -- unless there's another condition (golf,
+for instance).
+
+This exercise is so trivial, we don't even have to head to the OEIS
+to download the wanted numbers, as the expected output is stated
+in the exercise.
+
+So, all we need to do is print three numbers, separated by commas.
+
+The easiest way would be to just do what the challenge demands
+from us, and print the output as given.
+
+A slightly less easy way would be to head over the given
+[Wikipedia page](https://en.wikipedia.org/wiki/Self-descriptive_number)
+(or the [OEIS](https://oeis.org) for that matter), copy the first
+three numbers, and print those out.
+
+But those solutions no doubt will cause scorn in two weeks,
+when the review comes out. It's all "advice about the code is the thing".
+
+But that raises the question, what is the code which is wanted?
+You could generate all the numbers of length `b` in base `b`, while
+increasing `b`, test them for being self-descriptive, and print
+the first three numbers found.
+
+My advice about brute force code when there is a more efficient way:
+Don't ever do that.
+
+If we just imagine the Wikipedia page didn't list any self-descriptive
+numbers, and Neil Sloane has forgotten to pay the fee for the OEIS
+domain, so it was taken off-line, then it's still easy to determine
+the first three self-descriptive numbers -- no code required.
+
+Given the following observations for a self-descriptive number `N` in base `b`:
+* `N` has `b` digits, and does not start with a `0`.
+* The sum of the digits of `N` is `b`.
+* No digit of `N` equals `b - 1`.
+* The last digit of `N` is `0`.
+* If `b > 4`, then `N` does not start with a `1`.
+* If `b > 4`, then `N` does not start with `b - 2`.
+
+From that, it's easy to determine that:
+* There are no self-descriptive numbers in any base below `4`.
+* A self-descriptive number in base `4` must start with a `1` or `2`. And
+ end with a `0`. If it starts with a `1`, the middle digits are `1` and `2`.
+ If it starts with a `2`, the middle digits are `0` and `2`. Both `1210`,
+ and `2020"`are self-descriptive numbers.
+* A self-descriptive number in base `5` must start with a `2`, and end
+ with a `0`. The three middle digits must be `0`, `1`, and `2`. `21200`
+ is a self-descriptive number.
+
+(For a more detailed derivation, with all the details filled in, see [the blog
+post](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-107-1.html))
+
+But this still makes this challenge a glorified `Hello, World!`
+program, as there is no useful code to write to generate the numbers.
+
### Solutions
* [AWK](awk/ch-1.awk)
* [Bash](bash/ch-1.awk)
diff --git a/challenge-107/abigail/perl/ch-1.pl b/challenge-107/abigail/perl/ch-1.pl
index e747f40ef8..763a0c54fa 100644
--- a/challenge-107/abigail/perl/ch-1.pl
+++ b/challenge-107/abigail/perl/ch-1.pl
@@ -19,58 +19,63 @@ use experimental 'lexical_subs';
#
# This is a trivial exercise -- as all exercises are which do not
-# take any input, and which have a fixed output.
+# take any input, and which have a fixed output. Fixed output
+# challenges are boring -- unless there's another condition (golf,
+# for instance).
#
# This exercise is so trivial, we don't even have to head to the OEIS
-# to download the wanted numbers. We can easily derive what the
-# first numbers are.
-#
-# Note the following observations for self-descriptive numbers in
-# a given base b:
-#
-# 1) The number has exactly b digits (we don't count leading 0s)
-# 2) For all digits d, 0 <= d < b.
-# 3) The last digit must be a 0
-# 4) The leading digit must be greater than 0.
-# 5) No digit can be b - 1.
-# 6) The sum of all the digits is b.
-#
-# From 2) it follows there is no self-descriptive number in base 1.
-# From 3) and 4) it follows that if there is a self-descriptive
-# number in base 2 it must be "10". But 5) prohibits this.
-# From 1) - 5) it follows that any self-descriptive number in base 3
-# has be of the form "1.0". From 6), it follows that such a number
-# must be "120", but since we don't have two 1's in the number, it
-# is not self-descriptive.
-#
-# For base 4, the leading digit must be a 1 or a 2. If the first digit
-# is a 1, the number is of the form "1ab0", where a != 0, b != 0,
-# and a + b == 3. a cannot be 1, as that would imply the number
-# contains just one 1, but "1120" contains two 1s. "1210", however,
-# is a self-descriptive number, and this is the first self-descriptive
-# number. If the first digit is a 2, out of the other three digits,
-# two have to be 0 (of which one is the last one), and hence, the
-# other has to be 2. The pen-ultimate digit cannot be 0 (as the number
-# has at least one 2), leaving us with "2020". And this is self-descriptive.
-#
-# For base 5, possible leading digits are 1, 2, and 3.
-# If the leading digit is a 1, there can only be one 0, and we know
-# this must be the last digit. Which means the next three digits are
-# all non-zero, and sum to 4. Which means the next three digits are
-# 1, 1, and 2. But that means the number contains four 1s, but the
-# number does not contain a 4.
-# If the leading digit is a 3, there is only one non-zero in the next
-# digits, and this must be a 2. But that leaves no way to describe
-# the number of 3s.
-# If the leading digits is a 2, of the next four numbers, two are 0
-# (including the last one), and two are non-zero. Those two non-zero
-# numbers must add to 3, which means they are 1 and 2. This means the
-# third digit (describing the number of 2s) must be 2, and the second
-# digit (describing the number of 1s), must be 1. And "21200" is a
-# self-describing number.
-#
-# Which means, we now have the first three self-describing numbers:
-# "1210", "2020", "21200".
+# to download the wanted numbers, as the expected output is stated
+# in the exercise.
+#
+# So, all we need to do is print three numbers, separated by commas.
+#
+# The easiest way would be to just do what the challenge demands
+# from us, and print the output as given.
+#
+# A slightly less easy way would be to head over the given Wikipedia
+# page (or the OEIS for that matter), copy the first three numbers,
+# and print those out.
+#
+# But those solutions no doubt will cause scorn in two weeks,
+# when the review comes out. It's all "advice about the code is the thing".
+#
+# But that raises the question, what is the code which is wanted?
+# You could generate all the numbers of length b in base b, while
+# increasing b, test them for being self-descriptive, and print
+# the first three numbers found.
+#
+# My advice about brute force code when there is a more efficient way:
+# Don't ever do that.
+#
+# If we just imagine the Wikipedia page didn't list any self-descriptive
+# numbers, and Neil Sloane has forgotten to pay the fee for the OEIS
+# domain, so it was taken off-line, then it's still easy to determine
+# the first three self-descriptive numbers -- no code required.
+#
+# Given the following observations for a self-descriptive number N in base b:
+# - N has b digits, and does not start with a 0.
+# - The sum of the digits of N is b.
+# - No digit of N equals b - 1.
+# - The last digit of N is 0.
+# - If b > 4, then N does not start with a 1.
+# - If b > 4, then N does not start with b - 2.
+#
+# From that, it's easy to determine that:
+# - There are no self-descriptive numbers in any base below 4.
+# - A self-descriptive number in base 4 must start with a 1 or 2. And
+# end with a 0. If it starts with a 1, the middle digits are 1 and 2.
+# If it starts with a 2, the middle digits are 0 and 2. Both "1210",
+# and "2020" are self-descriptive numbers.
+# - A self-descriptive number in base 5 must start with a 2, and end
+# with a 0. The three middle digits must be 0, 1, and 2. "21200"
+# is a self-descriptive number.
+#
+# (For a more detailed derivation, with all the details filled in,
+# see the blog post at
+# https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-107-1.html)
+#
+# But this still makes this challenge a glorified "Hello, World!"
+# program, as there is no useful code to write to generate the numbers.
#
say "1210, 2020, 21200";