diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-04-19 03:12:28 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-19 03:12:28 +0100 |
| commit | a95e8f06e3854dfa1401c2e1619434c38d5255e5 (patch) | |
| tree | 85dcc12a058e6f7d194c53ab698a3b3631feea6d /challenge-108 | |
| parent | a32b581203d9a24b302aa233ab52f0efb0e2de2d (diff) | |
| parent | cb14e653f808efab54343ccde87436a78e9e5f3b (diff) | |
| download | perlweeklychallenge-club-a95e8f06e3854dfa1401c2e1619434c38d5255e5.tar.gz perlweeklychallenge-club-a95e8f06e3854dfa1401c2e1619434c38d5255e5.tar.bz2 perlweeklychallenge-club-a95e8f06e3854dfa1401c2e1619434c38d5255e5.zip | |
Merge pull request #3913 from Abigail/abigail/week-108
Abigail/week 108
Diffstat (limited to 'challenge-108')
51 files changed, 805 insertions, 136 deletions
diff --git a/challenge-108/abigail/README.md b/challenge-108/abigail/README.md index 67392c9e54..32dc7d1d9d 100644 --- a/challenge-108/abigail/README.md +++ b/challenge-108/abigail/README.md @@ -1,157 +1,70 @@ -# Solution by Abigail -## [Self-descriptive Numbers](https://perlweeklychallenge.org/blog/perl-weekly-challenge-107/#TASK1) +# Solutions by Asbigail +## [Locate Memory](https://perlweeklychallenge.org/blog/perl-weekly-challenge-108/#TASK1) -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 - -> 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`. - -### Example -~~~~ - 1210 is a four-digit self-descriptive number: - - 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 -~~~~ - -### 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. +Write a script to declare a variable or constant and print it's +location in the memory. ### Solutions -* [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) +* [Fortran](fortran/ch-1.f90) * [Go](go/ch-1.go) -* [Java](java/ch-1.java) -* [Lua](lua/ch-1.lua) -* [m4](m4/ch-1.m4) -* [Node.js](lua/ch-1.js) -* [OCaml](ocaml/ch-1.ml) -* [Pascal](pascal/ch-1.pl) +* [Pascal](pascal/ch-1.p) * [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 107: Self-descriptive Numbers](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-107-1.html) - -## [List Methods](https://perlweeklychallenge.org/blog/perl-weekly-challenge-107/#TASK2) +[Locate Memory](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-108-1.html) -Write a script to list methods of a package/class. -### Example -Given the package: -~~~~ -package Calc; +## [Bell Numbers](https://perlweeklychallenge.org/blog/perl-weekly-challenge-108/#TASK2) -use strict; -use warnings; +Write a script to display top 10 Bell Numbers. Please refer to +[wikipedia page](https://en.wikipedia.org/wiki/Bell_number) for +more informations. -sub new { bless {}, shift; } -sub add { } -sub mul { } -sub div { } - -1; -~~~~ -Output: -~~~~ -BEGIN -mul -div -new -add -~~~~ +### Example +* `B_0 = 1`, as you can only have one partition of zero element set +* `B_1 = 1`, as you can only have one partition of one element set {a}. +* `B_2 = 2`, `{a}{b}`, `{a,b}`. +* `B_3 = 5`, `{a}{b}{c}`, `{a,b}{c}`, `{a}{b,c}`, `{a,c}{b}`, `{a,b,c}`. +* `B_4 = 15`, `{a}{b}{c}{d}`, `{a,b,c,d}`, `{a,b}{c,d}`, `{a,c}{b,d}`, + `{a,d}{b,c}`, `{a,b}{c}{d}`, `{a,c}{b}{d}`, `{a,d}{b}{c}`, + `{b,c}{a}{d}`, `{b,d}{a}{c}`, `{c,d}{a}{b}`, `{a}{b,c,d}`, + `{b}{a,c,d}`, `{c}{a,b,d}`, `{d}{a,b,c}` ### Solutions +* [AWK](awk/ch-2.awk) +* [Bash](bash/ch-2.sh) +* [BASIC](basic/ch-2.bas) +* [bc](bc/ch-2.bc) +* [Befunge-93][befunge-93/ch-2.bf93] +* [C](c/ch-2.c) +* [Cobol](cobol/ch-2.cb) +* [Csh](csh/ch-2.csh) +* [Erlang](erlang/ch-2.erl) +* [Forth](forth/ch-2.fs) +* [Fortran](fortran/ch-2.f90) +* [Go](go/ch-2.go) +* [Java](java/ch-2.java) +* [Lua](lua/ch-2.lua) +* [m4](m4/ch-2.m4) +* [Node.js](node/ch-2.js) +* [Ocaml](ocaml/ch-2.ml) +* [Pascal](pascal/ch-2.p) * [Perl](perl/ch-2.pl) +* [PHP](php/ch-2.php) +* [PostScript](postscript/ch-2.ps) +* [Python](python/ch-2.py) +* [R](r/ch-2.r) +* [Rexx](rexx/ch-2.rexx) +* [Ruby](ruby/ch-2.rb) +* [Scheme](scheme/ch-2.scm) +* [sed](sed/ch-2.sed) +* [SQL](sql/ch-2.sql) +* [Tcl](tcl/ch-2.tcl) ### Blog -[Perl Weekly Challenge 107: List Methods](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-107-2.html) +[Bell Numbers](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-108-2.html) diff --git a/challenge-108/abigail/awk/ch-2.awk b/challenge-108/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..96344e739e --- /dev/null +++ b/challenge-108/abigail/awk/ch-2.awk @@ -0,0 +1,32 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk [plain | compute] +# + +BEGIN { + COUNT = 10 + if (!ARGV [1] || ARGV [1] != "compute") { + printf "%s\n", "1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147" + } + else { + bell [1, 1] = 1 + for (x = 2; x < COUNT; x ++) { + bell [x, 1] = bell [x - 1, x - 1] + for (y = 2; y <= x; y ++) { + bell [x, y] = bell [x, y - 1] + bell [x - 1, y - 1] + } + } + printf "1" + for (x = 1; x < COUNT; x ++) { + printf ", %d", bell [x, x] + } + printf "\n" + } + delete ARGV +} + diff --git a/challenge-108/abigail/bash/ch-2.sh b/challenge-108/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..c74aff4bbe --- /dev/null +++ b/challenge-108/abigail/bash/ch-2.sh @@ -0,0 +1,43 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh +# + +set -f + +COUNT=10 + +function index () { + local x=$1 + local y=$2 + idx=$((COUNT * x + y)) +} + + +if [ "X$1" != "Xcompute" ] +then echo "1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147" +else bell[0]=1 + for ((x = 1; x < COUNT - 1; x ++)) + do index $x 0; i1=$idx + index $((x - 1)) $((x - 1)); i2=$idx + bell[$i1]=${bell[$i2]} + for ((y = 1; y <= x; y ++)) + do index $x $y; i1=$idx + index $x $((y - 1)); i2=$idx + index $((x - 1)) $((y - 1)); i3=$idx + bell[$i1]=$((bell[i2] + bell[i3])) + done + done + + printf "1" + for ((x = 0; x < COUNT - 1; x ++)) + do index $x $x; + printf ", %d" ${bell[$idx]} + done + echo +fi diff --git a/challenge-108/abigail/basic/ch-2.bas b/challenge-108/abigail/basic/ch-2.bas new file mode 100644 index 0000000000..2882082e94 --- /dev/null +++ b/challenge-108/abigail/basic/ch-2.bas @@ -0,0 +1,9 @@ +010 REM +020 REM See ../README.md +030 REM + +040 REM +050 REM Run as: basic ch-2.bas +060 REM + +100 PRINT "1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147" diff --git a/challenge-108/abigail/bc/ch-2.bc b/challenge-108/abigail/bc/ch-2.bc new file mode 100644 index 0000000000..ba95c81011 --- /dev/null +++ b/challenge-108/abigail/bc/ch-2.bc @@ -0,0 +1,10 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-2.bc +# +"1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147 +" +quit diff --git a/challenge-108/abigail/befunge-93/ch-2.bf93 b/challenge-108/abigail/befunge-93/ch-2.bf93 new file mode 100644 index 0000000000..b212d72728 --- /dev/null +++ b/challenge-108/abigail/befunge-93/ch-2.bf93 @@ -0,0 +1,2 @@ +< v,_@#:< "1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147" +55 + > ^ diff --git a/challenge-108/abigail/blog.txt b/challenge-108/abigail/blog.txt new file mode 100644 index 0000000000..ce18567b5c --- /dev/null +++ b/challenge-108/abigail/blog.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-108-1.html diff --git a/challenge-108/abigail/blog1.txt b/challenge-108/abigail/blog1.txt new file mode 100644 index 0000000000..575bff46c6 --- /dev/null +++ b/challenge-108/abigail/blog1.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-108-2.html diff --git a/challenge-108/abigail/c/ch-1.c b/challenge-108/abigail/c/ch-1.c new file mode 100644 index 0000000000..0d1ad208bf --- /dev/null +++ b/challenge-108/abigail/c/ch-1.c @@ -0,0 +1,21 @@ +# 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) { + int i; + printf ("%lld\n", (long long) &i); /* %p is specific for printing */ + /* memory addresses, but does this */ + /* using hex numbers. So we cast */ + /* the address to long long and */ + /* print that. */ + return (0); +} diff --git a/challenge-108/abigail/c/ch-2.c b/challenge-108/abigail/c/ch-2.c new file mode 100644 index 0000000000..7d8352507b --- /dev/null +++ b/challenge-108/abigail/c/ch-2.c @@ -0,0 +1,70 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o + */ + +# define COUNT 10 + +# define PLAIN 0 +# define COMPUTE 2 + +typedef int number; /* Change if we want large numbers */ +char * fmt = "%d"; /* Should match typedef */ + +int main (int argc, char * argv []) { + int type = PLAIN; + if (argc > 1) { + if (strncmp (argv [1], "compute", 8) == 0) { + type = COMPUTE; + } + } + + if (type == PLAIN) { + printf ("1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147\n"); + } + if (type == COMPUTE) { + /* + * Calculate the Bell triangle + */ + number ** bell; + if ((bell = (number **) malloc ((COUNT - 1) * sizeof (number *))) + == NULL) { + perror ("Mallocing bell failed"); + exit (1); + } + if ((bell [0] = (number *) malloc (sizeof (number))) == NULL) { + perror ("Mallocing row failed"); + exit (1); + } + bell [0] [0] = 1; + for (int x = 1; x < COUNT - 1; x ++) { + if ((bell [x] = (number *) malloc ((x + 1) * sizeof (number))) + == NULL) { + perror ("Mallocing row failed"); + exit (1); + } + bell [x] [0] = bell [x - 1] [x - 1]; + for (int y = 1; y <= x; y ++) { + bell [x] [y] = bell [x] [y - 1] + bell [x - 1] [y - 1]; + } + } + + /* + * Print the right diagonal + */ + printf (fmt, 1); + for (int x = 0; x < COUNT - 1; x ++) { + printf (", "); + printf (fmt, bell [x] [x]); + } + printf ("\n"); + } + exit (0); +} diff --git a/challenge-108/abigail/cobol/ch-2.cb b/challenge-108/abigail/cobol/ch-2.cb new file mode 100644 index 0000000000..c5aff7fdf8 --- /dev/null +++ b/challenge-108/abigail/cobol/ch-2.cb @@ -0,0 +1,14 @@ +IDENTIFICATION DIVISION. +PROGRAM-ID. BELL. + +*> +*> See ../README.md +*> + +*> +*> Run as: cobc -xF -o ch-2.o ch-2.cb; ./ch-2.o +*> + +PROCEDURE DIVISION. + DISPLAY "1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147". + STOP RUN. diff --git a/challenge-108/abigail/csh/ch-2.csh b/challenge-108/abigail/csh/ch-2.csh new file mode 100644 index 0000000000..ea424ce6b9 --- /dev/null +++ b/challenge-108/abigail/csh/ch-2.csh @@ -0,0 +1,11 @@ +#!/bin/csh + +# +# See ../README.md +# + +# +# Run as: csh ch-2.csh +# + +echo "1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147" diff --git a/challenge-108/abigail/erlang/ch-2.erl b/challenge-108/abigail/erlang/ch-2.erl new file mode 100644 index 0000000000..f407e66fde --- /dev/null +++ b/challenge-108/abigail/erlang/ch-2.erl @@ -0,0 +1,15 @@ +% +% See ../README.md +% + +% +% Run as: ln ch-2.erl ch2.erl +% erl -compile ch2 +% erl -noshell -s ch1 main -s init stop +% + +-module (ch2). +-export ([main/0]). + +main () -> + io:fwrite ("1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147\n"). diff --git a/challenge-108/abigail/forth/ch-2.fs b/challenge-108/abigail/forth/ch-2.fs new file mode 100644 index 0000000000..8b234c922e --- /dev/null +++ b/challenge-108/abigail/forth/ch-2.fs @@ -0,0 +1,5 @@ +\ +\ See ../README.md +\ + +.( 1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147) diff --git a/challenge-108/abigail/fortran/ch-1.f90 b/challenge-108/abigail/fortran/ch-1.f90 new file mode 100644 index 0000000000..c55b81745e --- /dev/null +++ b/challenge-108/abigail/fortran/ch-1.f90 @@ -0,0 +1,13 @@ +! +! See ../README.md +! + +! +! Run as: gfortran -o ch-1.o ch-1.f90; ./ch-1.o +! + +program ch1 + implicit none + integer :: i + print *, loc(i) +end diff --git a/challenge-108/abigail/fortran/ch-2.f90 b/challenge-108/abigail/fortran/ch-2.f90 new file mode 100644 index 0000000000..112f6f2cd0 --- /dev/null +++ b/challenge-108/abigail/fortran/ch-2.f90 @@ -0,0 +1,12 @@ +! +! See ../README.md +! + +! +! Run as: gfortran -o ch-2.o ch-2.f90; ./ch-2.o +! + +program ch2 + implicit none + write (*, *) "1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147" +end diff --git a/challenge-108/abigail/go/ch-1.go b/challenge-108/abigail/go/ch-1.go new file mode 100644 index 0000000000..fb3673f5f3 --- /dev/null +++ b/challenge-108/abigail/go/ch-1.go @@ -0,0 +1,16 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import "fmt" + +func main () { + x := 0 + fmt . Printf ("%d\n", &x) +} diff --git a/challenge-108/abigail/go/ch-2.go b/challenge-108/abigail/go/ch-2.go new file mode 100644 index 0000000000..ef6ebae1a6 --- /dev/null +++ b/challenge-108/abigail/go/ch-2.go @@ -0,0 +1,15 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import "fmt" + +func main () { + fmt . Print ("1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147\n") +} diff --git a/challenge-108/abigail/java/ch-2.java b/challenge-108/abigail/java/ch-2.java new file mode 100644 index 0000000000..71c3980ce9 --- /dev/null +++ b/challenge-108/abigail/java/ch-2.java @@ -0,0 +1,13 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-2.java ch2.java; javac ch2; java ch2 +// + +public class ch2 { + public static void main (String [] args) { + System . out . print ("1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147\n"); + } +} diff --git a/challenge-108/abigail/lua/ch-2.lua b/challenge-108/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..37f067c465 --- /dev/null +++ b/challenge-108/abigail/lua/ch-2.lua @@ -0,0 +1,41 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua +-- + +local COUNT = 10 +local PLAIN = 0 +local COMPUTE = 1 + +local type = PLAIN +if #arg >= 1 and arg [1] == "compute" +then type = COMPUTE +end + +if type == PLAIN +then print ("1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147") +end + +if type == COMPUTE +then local bell = {} + bell [0] = {} + bell [0] [0] = 1 + for x = 1, COUNT - 2 + do bell [x] = {} + bell [x] [0] = bell [x - 1] [x - 1] + for y = 1, x + do bell [x] [y] = bell [x] [y - 1] + bell [x - 1] [y - 1] + end + end + + io . write (1) + for x = 0, COUNT - 2 + do io . write (", " .. bell [x] [x]) + end + io . write ("\n") +end diff --git a/challenge-108/abigail/m4/ch-2.m4 b/challenge-108/abigail/m4/ch-2.m4 new file mode 100644 index 0000000000..52ad7f9d63 --- /dev/null +++ b/challenge-108/abigail/m4/ch-2.m4 @@ -0,0 +1 @@ +1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147 diff --git a/challenge-108/abigail/node/ch-2.js b/challenge-108/abigail/node/ch-2.js n |
