diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-02-13 13:58:08 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-13 13:58:08 +0000 |
| commit | 41500d3d00d5406b7a847653b9f58edfe80282db (patch) | |
| tree | 67260a9a5955667fe77869c90f95c8d9dc5c58cc | |
| parent | 3e636c509d00cb678cbd592966e633846633c569 (diff) | |
| parent | 03c9ad84da90164d80b567341bd68e51eeaee862 (diff) | |
| download | perlweeklychallenge-club-41500d3d00d5406b7a847653b9f58edfe80282db.tar.gz perlweeklychallenge-club-41500d3d00d5406b7a847653b9f58edfe80282db.tar.bz2 perlweeklychallenge-club-41500d3d00d5406b7a847653b9f58edfe80282db.zip | |
Merge pull request #1243 from jacoby/master
The fun part is counting up from 100
| -rw-r--r-- | challenge-047/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-047/dave-jacoby/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-047/dave-jacoby/perl/ch-2.pl | 93 |
3 files changed, 126 insertions, 0 deletions
diff --git a/challenge-047/dave-jacoby/blog.txt b/challenge-047/dave-jacoby/blog.txt new file mode 100644 index 0000000000..1eacb51b53 --- /dev/null +++ b/challenge-047/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2020/02/12/counting-from-100-is-the-fun-part.html
\ No newline at end of file diff --git a/challenge-047/dave-jacoby/perl/ch-1.pl b/challenge-047/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..bb0d151256 --- /dev/null +++ b/challenge-047/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use utf8; +use feature qw{ postderef say signatures state switch }; +no warnings + qw{ experimental::postderef experimental::smartmatch experimental::signatures }; + +use Roman; + +my %operators = map { $_ => 1 } qw{ + - / * }; + +if ( scalar @ARGV > 2 ) { + my ( $r1, $op, $r2 ) = @ARGV; + if ( !$operators{$op} ) { + say 'not an operator'; + exit; + } + if ( !isroman($r1) ) { say qq{"$r1" is not a roman numeral}; exit; } + if ( !isroman($r2) ) { say qq{"$r2" is not a roman numeral}; exit; } + my $a1 = arabic($r1); + my $a2 = arabic($r2); + my $a3 = 0; + if ( $op eq '+' ) { $a3 = $a1 + $a2 } + if ( $op eq '-' ) { $a3 = $a1 - $a2 } + if ( $op eq '*' ) { $a3 = $a1 * $a2 } + if ( $op eq '/' ) { $a3 = $a1 / $a2 } + my $r3 = uc roman($a3); + say qq{ $r1 $op $r2 = $r3 }; +} +else { say 'We need an operator and two roman numbers' } diff --git a/challenge-047/dave-jacoby/perl/ch-2.pl b/challenge-047/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..1e3904653f --- /dev/null +++ b/challenge-047/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,93 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use utf8; +use feature qw{ postderef say signatures state switch }; +no warnings qw{ experimental::postderef experimental::smartmatch experimental::signatures }; + +# Write a script to print first 20 Gapful Numbers +# greater than or equal to 100. + +# a Gapful Number is a number where +# a second number is formed by the first and last digit of the number +# which is a factor in that number. +# for example, 100 forms 10 and 100/10 = 10 +# while 101 forms 11 and 101/11 = 9.181818... + +# When I'm trying to be readable, +# I declare an array to push things into +# I use a while loop based on the size of that array +# I iterate through numbers with ++ +# I split the number into an array with the easiest regex +# and I join with an empty string +# I push to the array if $n % $i == 0 + +my @x; +my $n = 100; +while ( scalar @x < 20 ) { + my @n = split //, $n; + my $i = join '', $n[0], $n[-1]; + push @x, $n if $n % $i == 0; + $n++; +} +say join "\n", @x; + +say '-' x 30; + +# When I'm trying to show off a little more +# I use a for loop ending at an abstractly large point +# I name the for loop +# I use state so the count variable only exists within the loop +# I use the named last to break the loop + +LOOP: for my $n ( 100 .. 1000 ) { + state $c = 0; + my @n = split //, $n; + my $i = join '', $n[0], $n[-1]; + if ( 0 == $n % $i ) { + $c++; + say $n; + } + last LOOP if $c >= 20; +} + +say '-' x 30; + +# When I'm trying to be way-cool functional dev +# I start with a range of 100..1000 +# I use join to stringify the output +# I use a grep and a state variable to limit to 20 +# I use substr to pull the first and last digit from the number +# and only create one variable in the second grep + +say join "\n", grep { state $c = 0; $c++ < 20 } + grep { my $i = join '', substr( $_, 0, 1 ), substr( $_, -1 ); $_ % $i == 0 }100 .. 1000; + +say '-' x 30; + +my $next = make_iterator(100); + +while ( my $n = $next->() ) { + state $c = 0; + my $i = my $i = join '', substr( $n, 0, 1 ), substr( $n, -1 ); + if ( 0==$n%$i){ + say $n; + $c++; + } + last if $c >19; +} + +sub make_iterator ( $start ) { + return sub { + state $i = $start; + return $i++; + } +} + +# the right answers: +# 100, 105, 108, 110, 120, +# 121, 130, 132, 135, 140, +# 143, 150, 154, 160, 165, +# 170, 176, 180, 187, 190 + |
