aboutsummaryrefslogtreecommitdiff
path: root/challenge-107
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-04-11 22:29:40 +0100
committerGitHub <noreply@github.com>2021-04-11 22:29:40 +0100
commit92b4ab576d15ee8065313cf4286940edee2ffe85 (patch)
tree4ad4be78d9b431885758206b442605ea2d56adb2 /challenge-107
parentdc6456df0ccabd3342034ac5418decc67ec0ba66 (diff)
parentb275456d468b7b5037e4309f44a2b85349d89b09 (diff)
downloadperlweeklychallenge-club-92b4ab576d15ee8065313cf4286940edee2ffe85.tar.gz
perlweeklychallenge-club-92b4ab576d15ee8065313cf4286940edee2ffe85.tar.bz2
perlweeklychallenge-club-92b4ab576d15ee8065313cf4286940edee2ffe85.zip
Merge pull request #3869 from adamcrussell/challenge-107
solutions for challenge 107
Diffstat (limited to 'challenge-107')
-rw-r--r--challenge-107/adam-russell/blog.txt1
-rw-r--r--challenge-107/adam-russell/perl/ch-1.pl56
-rw-r--r--challenge-107/adam-russell/perl/ch-2.pl30
3 files changed, 87 insertions, 0 deletions
diff --git a/challenge-107/adam-russell/blog.txt b/challenge-107/adam-russell/blog.txt
new file mode 100644
index 0000000000..0063c220a2
--- /dev/null
+++ b/challenge-107/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/04/11
diff --git a/challenge-107/adam-russell/perl/ch-1.pl b/challenge-107/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..29195d8ea9
--- /dev/null
+++ b/challenge-107/adam-russell/perl/ch-1.pl
@@ -0,0 +1,56 @@
+use strict;
+use warnings;
+##
+# Write a script to generate self-descriptive numbers.
+##
+use Thread;
+use boolean;
+use constant SDN_COUNT => 3;
+use constant THREAD_COUNT => 4;
+use constant RANGE_SIZE => 10_000;
+
+sub self_describing{
+ my($i) = @_;
+ my @digits = split(//, $i);
+ for my $x (0 .. @digits - 1){
+ my $count = 0;
+ for my $j (0 .. @digits - 1){
+ $count++ if($digits[$j] == $x);
+ return false if($count > $digits[$x]);
+ }
+ return false if($count != $digits[$x]);
+ }
+ return true;
+}
+
+sub self_describing_number{
+ my($start, $end) = @_;
+ my @r = ();
+ for(my $i = $start; $i < $end; $i++){
+ push @r, [length($i), $i] if(self_describing($i));
+ }
+ return \@r;
+}
+
+MAIN:{
+ my @threads;
+ my $count = 0;
+ my $lower = 1;
+ my $upper = RANGE_SIZE;
+ do{
+ for(0..(THREAD_COUNT - 1)){
+ my $t = Thread->new(\&self_describing_number, ($lower, $upper));
+ push @threads, $t;
+ $lower = $upper + 1;
+ $upper = $lower + RANGE_SIZE;
+ }
+ foreach my $t (@threads){
+ my $sdns = $t->join();
+ foreach my $sdn (@{$sdns}){
+ print "Base " . $sdn->[0] . ":" . $sdn->[1] . "\n" if $count < SDN_COUNT;
+ $count++;
+ }
+ }
+ @threads = ();
+ } while($count < SDN_COUNT);
+}
diff --git a/challenge-107/adam-russell/perl/ch-2.pl b/challenge-107/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..826c26cb66
--- /dev/null
+++ b/challenge-107/adam-russell/perl/ch-2.pl
@@ -0,0 +1,30 @@
+use strict;
+use warnings;
+##
+# Write a script to list methods of a package/class.
+##
+sub analyze{
+ my($file) = @_;
+ my @subs;
+ my @uses;
+ my @subroutines;
+ my $subs = `perlanalyst $file --analysis Sub`;
+ $subs =~ s/$file://;
+ @subs = split(/\n/, $subs);
+ my $uses = `perlanalyst $file --analysis Use`;
+ $uses =~ s/$file://;
+ @uses = split(/\n/, $uses);
+ for my $s (@subs){
+ $s =~ s/\s+//;
+ my @fields = split(/:/, $s);
+ push @subroutines, $fields[1] if(length($s) > 0);
+ }
+ push @subroutines, "BEGIN" if(@uses);
+ return @subroutines;
+}
+
+MAIN:{
+ my $FILE = $ARGV[0];
+ my @subroutines = analyze($FILE);
+ print join("\n", sort {$a cmp $b} @subroutines) . "\n";
+}