diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-04-19 00:55:48 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-19 00:55:48 +0100 |
| commit | 4bd9fb8b3130075183ef4f89d9e10a7f5ab94555 (patch) | |
| tree | 8f2764ce50137b35ef958067b35f8d14138578f7 | |
| parent | 2f279f279297fdbbd065c4aee9c55c8ee732bddf (diff) | |
| parent | 73ac94d5d09bc2b71c8e4de7a808eda5c7194796 (diff) | |
| download | perlweeklychallenge-club-4bd9fb8b3130075183ef4f89d9e10a7f5ab94555.tar.gz perlweeklychallenge-club-4bd9fb8b3130075183ef4f89d9e10a7f5ab94555.tar.bz2 perlweeklychallenge-club-4bd9fb8b3130075183ef4f89d9e10a7f5ab94555.zip | |
Merge pull request #3917 from mimosinnet/branch-for-challenge-108
Solutions for challenge 108
| -rw-r--r-- | challenge-108/mimosinnet/raku/ch-1.raku | 21 | ||||
| -rw-r--r-- | challenge-108/mimosinnet/raku/ch-2.raku | 63 |
2 files changed, 84 insertions, 0 deletions
diff --git a/challenge-108/mimosinnet/raku/ch-1.raku b/challenge-108/mimosinnet/raku/ch-1.raku new file mode 100644 index 0000000000..1f5492caa2 --- /dev/null +++ b/challenge-108/mimosinnet/raku/ch-1.raku @@ -0,0 +1,21 @@ +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-108/ +# Inspired by Luca Ferrari + +sub challenge( $var ) { + $var.WHERE; +} + +multi sub MAIN( $var ) { + my $mem = challenge($var); + say sprintf " Var: Value: \"%s\"; Type: %s \n Memory: Bin:%#b; Oct:%#o; Dec:%d, Hex:%#x", $var, $var.^name, $mem, $mem, $mem, $mem; +} + +multi sub MAIN( 'test' ) is hidden-from-USAGE { + use Test; + + my $var = 'This is a test'; + is challenge($var), $var.WHERE; + + done-testing; + +} diff --git a/challenge-108/mimosinnet/raku/ch-2.raku b/challenge-108/mimosinnet/raku/ch-2.raku new file mode 100644 index 0000000000..a469d1a6da --- /dev/null +++ b/challenge-108/mimosinnet/raku/ch-2.raku @@ -0,0 +1,63 @@ +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-108 Task 2 + +# I could not solve this challenge so I have looked at the other solutions. +# I have noticed that most of the solutions follow the Triangle scheme for calculations [1] +# except Aaron Smith [2]. +# +# I have been rewriting Aaron Smith code to see if I could understand it! +# Very impressive to see the translation of the Bell formula to Raku code! +# (specially when coming from social siences. Really nice!) +# +# [1] https://en.wikipedia.org/wiki/Bell_number#Triangle_scheme_for_calculations +# [2] https://aaronreidsmith.github.io/blog/perl-weekly-challenge-108/ + +my $l = Lock.new; +my %bell-cached; +sub challenge( $number ) { + return 1 if $number == 0|1; + + my $previous-bell-number = $number - 1; + my @partitions = gather { + for (0..$previous-bell-number) -> $k { + take ( + (0..^$previous-bell-number).combinations($k).elems * ( %bell-cached{$k} //= challenge($k) ) + ); + } + } + return @partitions.sum; +} + +multi sub MAIN( $number ) { + say "The number of partitions of a set of size $number is: ",challenge($number); +} + +multi sub MAIN( 'challenge' ) { + for (1..10) -> $number { + MAIN($number); + } +} + +multi sub MAIN( 'test' ) is hidden-from-USAGE { + use Test; + + my @tests = ( + (0, 1), + (1, 1), + (2, 2), + (3, 5), + (4, 15), + (5, 52), + (6, 203), + (7, 877), + (8, 4140), + (9, 21147), + (10, 115975) + ); + + for @tests -> ($n, $bell-number) { + is(challenge($n), $bell-number); + } + + done-testing; + +} |
