diff options
45 files changed, 643 insertions, 77 deletions
diff --git a/challenge-107/abigail/README.md b/challenge-107/abigail/README.md index 3b4bc6fcf4..67392c9e54 100644 --- a/challenge-107/abigail/README.md +++ b/challenge-107/abigail/README.md @@ -1,108 +1,157 @@ # Solution by Abigail -## [Maximum Gap](https://perlweeklychallenge.org/blog/perl-weekly-challenge-106/#TASK1) +## [Self-descriptive Numbers](https://perlweeklychallenge.org/blog/perl-weekly-challenge-107/#TASK1) -You are given an array of integers `@N`. +Write a script to display the first three self-descriptive numbers. +As per [wikipedia](https://en.wikipedia.org/wiki/Self-descriptive_number), +the definition of Self-descriptive Number is -Write a script to display the maximum difference between two -successive elements once the array is sorted. +> In mathematics, a self-descriptive number is an integer `m` that in a +> given base `b` is `b` digits long in which each digit `d` at position `n` +> (the most significant digit being at position 0 and the least +> significant at position `b - 1`) counts how many instances of +> digit `n` are in `m`. -If the array contains only `1` element then display `0`. - -### Examples +### Example ~~~~ -Input: @N = (2, 9, 3, 5) -Output: 4 + 1210 is a four-digit self-descriptive number: -Input: @N = (1, 3, 8, 2, 0) -Output: 5 + position 0 has value 1 i.e. there is only one 0 in the number + position 1 has value 2 i.e. there are two 1 in the number + position 2 has value 1 i.e. there is only one 2 in the number + position 3 has value 0 i.e. there is no 3 in the number +~~~~ -Input: @N = (5) -Output: 0 +### Output ~~~~ + 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 -* [GNU AWK](awk/ch-1.gawk) -* [Bash](perl/ch-1.sh) +* [AWK](awk/ch-1.awk) +* [Bash](bash/ch-1.awk) +* [BASIC](basic/ch-1.bas) +* [bc](bc/ch-1.bc) +* [Befunge-93](befunge-93/ch-1.bf93) * [C](c/ch-1.c) +* [Cobol](cobol/ch-1.cb) +* [Csh](csh/ch-1.csh) +* [Erlang](erlang/ch-1.erl) +* [Forth](forth/ch-1.fs) +* [Fortran](fortran/ch-1.fs) +* [Go](go/ch-1.go) +* [Java](java/ch-1.java) * [Lua](lua/ch-1.lua) -* [Node.js](node/ch-1.js) +* [m4](m4/ch-1.m4) +* [Node.js](lua/ch-1.js) +* [OCaml](ocaml/ch-1.ml) +* [Pascal](pascal/ch-1.pl) * [Perl](perl/ch-1.pl) +* [PHP](php/ch-1.pl) +* [PostScript](postscript/ch-1.ps) * [Python](python/ch-1.py) +* [R](r/ch-1.r) +* [Rexx](rexx/ch-1.rexx) * [Ruby](ruby/ch-1.rb) +* [Scheme](scheme/ch-1.scm) +* [sed](sed/ch-1.sed) +* [SQL](sql/ch-1.sql) +* [Tcl](tcl/ch-1.tcl) ### Blog -[Perl Weekly Challenge 106: Maximum Gap](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-106-1.html) +[Perl Weekly Challenge 107: Self-descriptive Numbers](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-107-1.html) -## [Decimal String](https://perlweeklychallenge.org/blog/perl-weekly-challenge-106/#TASK2) +## [List Methods](https://perlweeklychallenge.org/blog/perl-weekly-challenge-107/#TASK2) -You are given numerator and denominator i.e. `$N` and `$D`. +Write a script to list methods of a package/class. -Write a script to convert the fraction into decimal string. If the -fractional part is recurring then put it in parenthesis. - -### Examples +### Example +Given the package: ~~~~ -Input: $N = 1, $D = 3 -Output: "0.(3)" +package Calc; -Input: $N = 1, $D = 2 -Output: "0.5" +use strict; +use warnings; -Input: $N = 5, $D = 66 -Output: "0.0(75)" -~~~~ - -### Notes - -We are assuming the numerator is non-negative, and the denominator -is positive. Dealing with signs is left as an exercise to the reader. - -We're creation the decimal expansion of the fraction `$N / $D` -by performing long division. - -First, we calculate the part before the decimal point, by -doing integer division of `$N / $D`. -We're then left to do division of `$N' / $D`, where `$N'` initially -is `$N % $D`. -We then repeatedly find new digits by calculating the integer -division of `(10 * $N' / $D)` (which gives us a new digit in the -decimal expansion), and then setting `$N' = 10 * $N' % $D`. -The fraction will have a finite decimal expansion if during the -process `$N'` becomes `0`. Otherwise, it repeats, and it repeats -as soon as have a `$N'` which we've already seen. By the pidgeon -hole principle, this cannot take more then $D steps. -To calculate the repeating part, we keep track of how far we -were in calculating the expansion for which `$N'`. +sub new { bless {}, shift; } +sub add { } +sub mul { } +sub div { } +1; ~~~~ - 22/7 \0.318 - 0 int (7 / 22) == 0, so 0 before decimal point - -- - 7 N = N % D - 66 3 * D - -- - 4 N = (10 * N) % D <--+ - 22 1 * D | - -- | Same, so '18' - 18 N = (10 * N) % D | is the repeating - 176 8 * D | part - --- | - 4 N = (10 * N) % D <--+ +Output: +~~~~ +BEGIN +mul +div +new +add ~~~~ - -This implementation is based on the [one given on -Wikipedia](https://en.wikipedia.org/wiki/Repeating_decimal). - ### Solutions -* [AWK](perl/ch-2.awk) -* [Bash](bash/ch-2.sh) -* [C](c/ch-2.c) -* [Lua](lua/ch-2.lua) -* [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) -* [Python](python/ch-2.py) -* [Ruby](ruby/ch-2.rb) ### Blog -[Perl Weekly Challenge 106: Decimal String](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-106-2.html) +[Perl Weekly Challenge 107: List Methods](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-107-2.html) + diff --git a/challenge-107/abigail/awk/ch-1.awk b/challenge-107/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..cd8a87a7dc --- /dev/null +++ b/challenge-107/abigail/awk/ch-1.awk @@ -0,0 +1,13 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk +# + +BEGIN { + print "1210, 2020, 21200" +} diff --git a/challenge-107/abigail/bash/ch-1.sh b/challenge-107/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..133a606068 --- /dev/null +++ b/challenge-107/abigail/bash/ch-1.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +echo "1210, 2020, 21200" diff --git a/challenge-107/abigail/basic/ch-1.bas b/challenge-107/abigail/basic/ch-1.bas new file mode 100644 index 0000000000..6f0a11aabb --- /dev/null +++ b/challenge-107/abigail/basic/ch-1.bas @@ -0,0 +1,9 @@ +010 REM +020 REM See ../README.md +030 REM + +040 REM +050 REM Run as: basic ch-1.bas +060 REM + +100 PRINT "1210, 2020, 21200" diff --git a/challenge-107/abigail/bc/ch-1.bc b/challenge-107/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..258f8a522d --- /dev/null +++ b/challenge-107/abigail/bc/ch-1.bc @@ -0,0 +1,10 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc +# +"1210, 2020, 21200 +" +quit diff --git a/challenge-107/abigail/befunge-93/ch-1.bf93 b/challenge-107/abigail/befunge-93/ch-1.bf93 new file mode 100644 index 0000000000..67043917cf --- /dev/null +++ b/challenge-107/abigail/befunge-93/ch-1.bf93 @@ -0,0 +1,2 @@ +<v_@#:<"1210, 2020, 21200"+55 + >, ^ diff --git a/challenge-107/abigail/blog.txt b/challenge-107/abigail/blog.txt new file mode 100644 index 0000000000..d0209f073a --- /dev/null +++ b/challenge-107/abigail/blog.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-107-1.html diff --git a/challenge-107/abigail/blog1.txt b/challenge-107/abigail/blog1.txt new file mode 100644 index 0000000000..ce4fb4cb33 --- /dev/null +++ b/challenge-107/abigail/blog1.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-107-2.html diff --git a/challenge-107/abigail/c/ch-1.c b/challenge-107/abigail/c/ch-1.c new file mode 100644 index 0000000000..edda83dced --- /dev/null +++ b/challenge-107/abigail/c/ch-1.c @@ -0,0 +1,15 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o + */ + +int main (void) { + printf ("%s\n", "1210, 2020, 21200"); +} diff --git a/challenge-107/abigail/cobol/ch-1.cb b/challenge-107/abigail/cobol/ch-1.cb new file mode 100644 index 0000000000..c3be4e79a8 --- /dev/null +++ b/challenge-107/abigail/cobol/ch-1.cb @@ -0,0 +1,14 @@ +IDENTIFICATION DIVISION. +PROGRAM-ID. XXXX. + +*> +*> See ../README.md +*> + +*> +*> Run as: cobc -xF -o ch-1.o ch-1.cb; ./ch-1.o +*> + +PROCEDURE DIVISION. + DISPLAY "1210, 2020, 21200". + STOP RUN. diff --git a/challenge-107/abigail/csh/ch-1.csh b/challenge-107/abigail/csh/ch-1.csh new file mode 100644 index 0000000000..510f8e2ae4 --- /dev/null +++ b/challenge-107/abigail/csh/ch-1.csh @@ -0,0 +1,11 @@ +#!/bin/csh + +# +# See ../README.md +# + +# +# Run as: csh ch-1.csh +# + +echo "1210, 2020, 21200" diff --git a/challenge-107/abigail/erlang/ch-1.erl b/challenge-107/abigail/erlang/ch-1.erl new file mode 100644 index 0000000000..17abba2e3c --- /dev/null +++ b/challenge-107/abigail/erlang/ch-1.erl @@ -0,0 +1,15 @@ +% +% See ../README.md +% + +% +% Run as: ln ch-1.erl ch1.erl +% erl -compile ch1 +% erl -noshell -s ch1 main -s init stop +% + +-module (ch1). +-export ([main/0]). + +main () -> + io:fwrite ("1210, 2020, 21200\n"). diff --git a/challenge-107/abigail/forth/ch-1.fs b/challenge-107/abigail/forth/ch-1.fs new file mode 100644 index 0000000000..022fe0f6cc --- /dev/null +++ b/challenge-107/abigail/forth/ch-1.fs @@ -0,0 +1,5 @@ +\ +\ See ../README.md +\ + +.( 1210, 2020, 21200) diff --git a/challenge-107/abigail/fortran/ch-1.f90 b/challenge-107/abigail/fortran/ch-1.f90 new file mode 100644 index 0000000000..70a7ad8707 --- /dev/null +++ b/challenge-107/abigail/fortran/ch-1.f90 @@ -0,0 +1,12 @@ +! +! See ../README.md +! + +! +! Run as: gfortran -o ch-1.o ch-1.f90; ./ch-1.o +! + +program ch1 + implicit none + write(*, *) "1210, 2020, 21200" +end diff --git a/challenge-107/abigail/go/ch-1.go b/challenge-107/abigail/go/ch-1.go new file mode 100644 index 0000000000..9a75bbff46 --- /dev/null +++ b/challenge-107/abigail/go/ch-1.go @@ -0,0 +1,15 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import "fmt" + +func main () { + fmt . Print ("1210, 2020, 21200\n") +} diff --git a/challenge-107/abigail/java/ch-1.java b/challenge-107/abigail/java/ch-1.java new file mode 100644 index 0000000000..40fe82dccb --- /dev/null +++ b/challenge-107/abigail/java/ch-1.java @@ -0,0 +1,13 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1; java ch1 +// + +public class ch1 { + public static void main (String [] args) { + System . out . print ("1210, 2020, 21200\n"); + } +} diff --git a/challenge-107/abigail/lua/ch-1.lua b/challenge-107/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..42017becfb --- /dev/null +++ b/challenge-107/abigail/lua/ch-1.lua @@ -0,0 +1,11 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua +-- + +print ("1210, 2020, 21200") diff --git a/challenge-107/abigail/m4/ch-1.m4 b/challenge-107/abigail/m4/ch-1.m4 new file mode 100644 index 0000000000..0de25823a2 --- /dev/null +++ b/challenge-107/abigail/m4/ch-1.m4 @@ -0,0 +1 @@ +1210, 2020, 21200 diff --git a/challenge-107/abigail/node/ch-1.js b/challenge-107/abigail/node/ch-1.js new file mode 100644 index 0000000000..6216ffe749 --- /dev/null +++ b/challenge-107/abigail/node/ch-1.js @@ -0,0 +1,11 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js +// + +console . log ("1210, 2020, 21200") diff --git a/challenge-107/abigail/ocaml/ch-1.ml b/challenge-107/abigail/ocaml/ch-1.ml new file mode 100644 index 0000000000..53a9124d9a --- /dev/null +++ b/challenge-107/abigail/ocaml/ch-1.ml @@ -0,0 +1,9 @@ +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: ocaml ch-1.ml *) +(* *) + +print_endline "1210, 2020, 21200"; diff --git a/challenge-107/abigail/pascal/ch-1.p b/challenge-107/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..b24ebeab57 --- /dev/null +++ b/challenge-107/abigail/pascal/ch-1.p @@ -0,0 +1,13 @@ +Program SelfDescribing; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out *) +(* *) + +begin + writeln ('1210, 2020, 21200'); +end. diff --git a/challenge-107/abigail/perl/ch-1.pl b/challenge-107/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..763a0c54fa --- /dev/null +++ b/challenge-107/abigail/perl/ch-1.pl @@ -0,0 +1,81 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl +# + +# +# 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 (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"; diff --git a/challenge-107/abigail/perl/ch-2.pl b/challenge-107/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..e87f8aef71 --- /dev/null +++ b/challenge-107/abigail/perl/ch-2.pl @@ -0,0 +1,70 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl module +# +# where 'module' is the name of the module to be inspected, and +# this module is to be found in a file named 'module.pm' (with :: +# replaced by '/'), somewhere in @INC. +# + +# +# Get the module name from the command line. +# +my $module = shift; + +# +# Load the module. +# +eval "use $module; 1" or die "Failed to load $module.pm: $@"; + +# +# Create a reference to the symbol table of the module. +# +my $symbol_table = do {no strict 'refs'; \%{$module . "::"}}; + +# +# Iterate over the symbols in the name space, and print out +# which symbols have a code ref entry in the type glob. +# (The symbol table maps symbols to type globs). +# +# Note that this only gives use the global methods, no lexical +# subroutines are found that way. Which is ok, as lexical +# subroutines are bound to their lexical scope - not the package. +# +foreach my $symbol (keys %$symbol_table) { + say $symbol if *{$$symbol_table {$symbol}} {CODE}; +} + +# +# Print BEGIN. This is not a method. While there is a entry +# named "BEGIN" in the symbol table, the type glob does not +# have CODE entry. $module -> can ("BEGIN") fails as well. +# +# We only print "BEGIN" so we pass the one test given for +# this exercise. +# +# We will *NOT* do the same for END, INIT, CHECK, nor UNITCHECK. +# One piece of madness is enough. +# +say "BEGIN" if $$symbol_table {BEGIN} && !*{$$symbol_table {BEGIN}} {CODE}; + +# +# Note that if the module contains an AUTOLOAD method, one could argue +# the module contains any possible method. We decide to not do that. +# + +__END__ diff --git a/challenge-107/abigail/php/ch-1.php b/challenge-107/abigail/php/ch-1.php new file mode 100644 index 0000000000..52646348a5 --- /dev/null +++ b/challenge-107/abigail/php/ch-1.php @@ -0,0 +1,11 @@ +<?php + // + // See ../README.md + // + + // + // Run as: php ch-1.php + // + + echo "1210, 2020, 21200\n"; +?> diff --git a/challenge-107/abigail/postscript/ch-1.ps b/challenge-107/abigail/postscript/ch-1.ps new file mode 100644 index 0000000000..3ad53dd1f7 --- /dev/null +++ b/challenge-107/abigail/postscript/ch-1.ps @@ -0,0 +1,10 @@ +%!PS +% +% See ../README.md +% + +% +% Run as: ps2ascii ch-1.ps +% + +(1210, 2020, 21200) = diff --git a/challenge-107/abigail/python/ch-1.py b/challenge-107/abigail/python/ch-1.py new file mode 100644 index 0000000000..c413e9f903 --- /dev/null +++ b/challenge-107/abigail/python/ch-1.py @@ -0,0 +1,11 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py +# + +print ("1210, 2020, 21200") diff --git a/challenge-107/abigail/r/ch-1.r b/challenge-107/abigail/r/ch-1.r new file mode 100644 index 0000000000..b14a917727 --- /dev/null +++ b/challenge-107/abigail/r/ch-1.r @@ -0,0 +1,9 @@ |
