aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-10-04 20:36:19 +0200
committerAbigail <abigail@abigail.be>2021-10-04 20:36:19 +0200
commit7214430149332afb05c2aba1ab7e8d7e5d1c2013 (patch)
tree8177969fdc208e4cc02c9c0a8b717cc2c8691f07
parenta20199a33b1f5599b8bc2bc42b6970d42bed00e6 (diff)
downloadperlweeklychallenge-club-7214430149332afb05c2aba1ab7e8d7e5d1c2013.tar.gz
perlweeklychallenge-club-7214430149332afb05c2aba1ab7e8d7e5d1c2013.tar.bz2
perlweeklychallenge-club-7214430149332afb05c2aba1ab7e8d7e5d1c2013.zip
Perl solutions for week 133
-rw-r--r--challenge-133/abigail/README.md7
-rw-r--r--challenge-133/abigail/perl/ch-1.pl42
-rw-r--r--challenge-133/abigail/perl/ch-2.pl38
3 files changed, 80 insertions, 7 deletions
diff --git a/challenge-133/abigail/README.md b/challenge-133/abigail/README.md
index 5eef101325..aa835b7f1e 100644
--- a/challenge-133/abigail/README.md
+++ b/challenge-133/abigail/README.md
@@ -2,14 +2,7 @@
## Part 1
-* [AWK](awk/ch-1.awk)
-* [Bash](bash/ch-1.sh)
-* [C](c/ch-1.c)
-* [Lua](lua/ch-1.lua)
-* [Node.js](node/ch-1.js)
* [Perl](perl/ch-1.pl)
-* [Python](python/ch-1.py)
-* [Ruby](ruby/ch-1.rb)
## Part 2
diff --git a/challenge-133/abigail/perl/ch-1.pl b/challenge-133/abigail/perl/ch-1.pl
new file mode 100644
index 0000000000..cefc525dc9
--- /dev/null
+++ b/challenge-133/abigail/perl/ch-1.pl
@@ -0,0 +1,42 @@
+#!/opt/perl/bin/perl
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+#
+# Run as: perl ch-1.pl < input-file
+#
+
+#
+# The challenge says:
+#
+# Please avoid using built-in function.
+#
+# That makes if very hard to do anything in Perl. There is the sqrtint
+# function in Math::Prime::Utils which does exactly what is asked (and
+# which obviously isn't a build-in), but that just returns a value.
+# To print the result, one would need print or say (or (sys)write),
+# but those are build-in functions as well.
+#
+# So, the solution we opted for is to branch out to AWK. Now, we cannot
+# use "exec" or "system" to call an AWK program, as both "exec" and
+# "system" are build-ins. Hence, we use backticks. But there is a catch,
+# backticks collect the output of the executed program. And we cannot
+# use print or say to print the return value of the backticks as explained
+# above. Therefore, we let the AWK program write its output to the terminal.
+#
+# There's one remaining snag we need to cover. When reading in a number,
+# it will have a trailing newline. AWK considers newlines to be statement
+# terminators (hurray for AWK), so we either have to escape the newline,
+# or get rid of it. Using chomp or chop is out, as they are build ins,
+# so we use s/[^0-9]+//gr to get rid of any non digits.
+#
+
+`awk 'BEGIN {print (int (sqrt (${\s/[^0-9]+//gr}))) > "/dev/tty"}'` for <>;
+
diff --git a/challenge-133/abigail/perl/ch-2.pl b/challenge-133/abigail/perl/ch-2.pl
new file mode 100644
index 0000000000..178c362ab3
--- /dev/null
+++ b/challenge-133/abigail/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/opt/perl/bin/perl
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+#
+# We've done factorization in previous challenges, so now I'll just
+# use a module to give me the prime factors of a number.
+#
+use Math::Prime::Util qw [factor];
+use List::Util qw [sum];
+
+my $COUNT = 10;
+
+#
+# Nothing smart going on. Just counting upwards from 1, check if it's
+# a Smith Number, and outputting it if it's a Smith Number.
+# Stop when we have 10 of them.
+#
+
+#
+# Return the sum of the digits of the set of given numbers.
+#
+sub digitsum (@n) {sum map {split //} @n}
+
+my $c = 0;
+my $n = 1;
+do {
+ my @factors = factor $n;
+ $c ++, say $n if @factors > 1 && digitsum ($n) == digitsum @factors;
+ $n ++;
+} until $c == $COUNT;