diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-04-19 00:51:32 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-19 00:51:32 +0100 |
| commit | 22a20dc49fd8469f6e5b252e3adc9a91cc44ba83 (patch) | |
| tree | 1f630ed35e525c368f6b82bd43f08803c35f6e91 | |
| parent | 7496218cb65125d0203019e7cb82e548c23a1410 (diff) | |
| parent | f5f71b32f4408de022c27758a197d945ce96e55b (diff) | |
| download | perlweeklychallenge-club-22a20dc49fd8469f6e5b252e3adc9a91cc44ba83.tar.gz perlweeklychallenge-club-22a20dc49fd8469f6e5b252e3adc9a91cc44ba83.tar.bz2 perlweeklychallenge-club-22a20dc49fd8469f6e5b252e3adc9a91cc44ba83.zip | |
Merge pull request #3916 from dcw803/master
imported my attempts this week
| -rw-r--r-- | challenge-108/duncan-c-white/README | 85 | ||||
| -rwxr-xr-x | challenge-108/duncan-c-white/perl/ch-1.pl | 21 | ||||
| -rwxr-xr-x | challenge-108/duncan-c-white/perl/ch-2.pl | 90 |
3 files changed, 149 insertions, 47 deletions
diff --git a/challenge-108/duncan-c-white/README b/challenge-108/duncan-c-white/README index 4073520658..5b35941c97 100644 --- a/challenge-108/duncan-c-white/README +++ b/challenge-108/duncan-c-white/README @@ -1,65 +1,56 @@ -Task 1: "Self-descriptive Numbers +Task 1: "Locate Memory -Write a script to display the first three self-descriptive numbers. As per wikipedia, 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. - -For example: - - 1210 is a four-digit self-descriptive number: +Write a script to declare a variable or constant and print it's location in +the memory. +" - 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 +My notes: umm, does this just mean "use \ and %p", or something more subtle? -Output - 1210, 2020, 21200 +Task 2: "Bell Numbers -WARNING: I realised just before the launch this task was also part of the week -43 and contributed by Laurent Rosenfeld. It is too late to change now. Feel -free to share your previous solutions if you took part in the week 43 -already. I should have been more carefull, sorry. -" +Write a script to display top 10 Bell Numbers. Please refer to -My notes: well, as it happened I skipped task 43, so let's have a go. The -important thing is: number of digits == base, which puts extra constraints -on the digits. So try bases b = 2.. try all base b numbers for "self- -descriptiveness" and then and stop after finding the first N self-descriptive -numbers. Let's take N as a command line input for generality, default 3. +https://en.wikipedia.org/wiki/Bell_number +for more informations. -Task 2: "List Methods +Example: -Write a script to list methods of a package/class. +B0: 1 as you can only have one partition of zero element set -Example +B1: 1 as you can only have one partition of one element set {a}. -package Calc; +B2: 2 -use strict; -use warnings; + {a}{b} + {a,b} -sub new { bless {}, shift; } -sub add { } -sub mul { } -sub div { } +B3: 5 -1; + {a}{b}{c} + {a,b}{c} + {a}{b,c} + {a,c}{b} + {a,b,c} -Output +B4: 15 -BEGIN -mul -div -new -add + {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} " -My notes: hmm, introspection. I genuinely can't remember how to do this -without a but of googling. Isn't there a symbol table hash per package, -that Memoize manipulates? Ah yes: the stash containing typeglobs. +My notes: Bell's triangle has a simple algorithm; let's use that! diff --git a/challenge-108/duncan-c-white/perl/ch-1.pl b/challenge-108/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..1ee86d1354 --- /dev/null +++ b/challenge-108/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl +# +# Task 1: "Locate Memory +# +# Write a script to declare a variable or constant and print it's location in +# the memory. +# " +# +# My notes: umm, does this just mean "use \ and %p", or something more subtle? +# + +use strict; +use warnings; +use feature 'say'; +use Data::Dumper; + +die "Usage: locate-memory val\n" unless @ARGV==1; + +my $x = shift; +my $refx = \$x; +printf "address of $x is %p\n", $refx; diff --git a/challenge-108/duncan-c-white/perl/ch-2.pl b/challenge-108/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..ab4a11a674 --- /dev/null +++ b/challenge-108/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,90 @@ +#!/usr/bin/perl +# +# Task 2: "Bell Numbers +# +# Write a script to display top 10 Bell Numbers. Please refer to +# +# https://en.wikipedia.org/wiki/Bell_number +# +# for more informations. +# +# Example: +# +# B0: 1 as you can only have one partition of zero element set +# +# B1: 1 as you can only have one partition of one element set {a}. +# +# B2: 2 +# +# {a}{b} +# {a,b} +# +# B3: 5 +# +# {a}{b}{c} +# {a,b}{c} +# {a}{b,c} +# {a,c}{b} +# {a,b,c} +# +# B4: 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} +# " +# +# My notes: Bell's triangle has a simple algorithm; let's use that! +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +use Getopt::Long; +use Data::Dumper; + +my $debug = 0; +die "Usage: bells-numbers [--debug] N\n" + unless GetOptions("debug"=>\$debug) && @ARGV<2; + +my $n = shift // 10; + +my @bell = first_n_bell_nos( $n ); +say join( ',', @bell ); + +# +# my @bell = first_n_bell_nos( $n ); +# Generate and return the first $n bell numbers. +# +fun first_n_bell_nos( $n ) +{ + my @result; + my @currrow = (1); + push @result, $currrow[0]; + foreach my $i (1..$n-1) + { + my @nextrow; + $nextrow[0] = $currrow[-1]; + foreach my $j (1..$i) + { + $nextrow[$j] = $currrow[$j-1] + $nextrow[$j-1]; + } + say join( ' ', @nextrow ) if $debug; + push @result, $nextrow[0]; + @currrow = @nextrow; + } + return @result; +} |
