diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-08-29 15:23:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-29 15:23:30 +0100 |
| commit | 70e6dabbe403db6aa11bed907e78e8712c39e194 (patch) | |
| tree | 79462f313e83801b15cea6899efaa8090c21affe | |
| parent | 1492576004c3db0d60409b79060814f9bb966e2f (diff) | |
| parent | c8826b12ae4835e1673a5783d519912aedf4216c (diff) | |
| download | perlweeklychallenge-club-70e6dabbe403db6aa11bed907e78e8712c39e194.tar.gz perlweeklychallenge-club-70e6dabbe403db6aa11bed907e78e8712c39e194.tar.bz2 perlweeklychallenge-club-70e6dabbe403db6aa11bed907e78e8712c39e194.zip | |
Merge pull request #569 from randyl/week23
Solutions for challenge 23 tasks
| -rw-r--r-- | challenge-023/randy-lauen/perl5/ch-1.pl | 30 | ||||
| -rw-r--r-- | challenge-023/randy-lauen/perl5/ch-2.pl | 50 | ||||
| -rw-r--r-- | challenge-023/randy-lauen/perl5/ch-3.pl | 44 | ||||
| -rw-r--r-- | challenge-023/randy-lauen/perl6/ch-1.p6 | 27 | ||||
| -rw-r--r-- | challenge-023/randy-lauen/perl6/ch-2.p6 | 59 | ||||
| -rw-r--r-- | challenge-023/randy-lauen/perl6/ch-3.p6 | 31 |
6 files changed, 241 insertions, 0 deletions
diff --git a/challenge-023/randy-lauen/perl5/ch-1.pl b/challenge-023/randy-lauen/perl5/ch-1.pl new file mode 100644 index 0000000000..2b8533c330 --- /dev/null +++ b/challenge-023/randy-lauen/perl5/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +=head1 SYNOPSIS + +Task: +Create a script that prints nth order forward difference series. You should be a +able to pass the list of numbers and order number as command line parameters. + +Usage: + $ perl ch-1.pl --order=1 5 9 2 8 1 6 + +=cut + +use strict; +use warnings; +use feature 'say'; + +use Getopt::Long; + +my $order; +GetOptions( 'order=i' => \$order ) or die; + +my @numbers = @ARGV; + +foreach my $order ( 1 .. $order ) { + @numbers = map { $numbers[ $_ ] - $numbers[ $_ - 1 ] } 1 .. $#numbers; + say "$order: " . join(', ', @numbers); + last if @numbers == 1; +} + diff --git a/challenge-023/randy-lauen/perl5/ch-2.pl b/challenge-023/randy-lauen/perl5/ch-2.pl new file mode 100644 index 0000000000..45aae0214e --- /dev/null +++ b/challenge-023/randy-lauen/perl5/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +=head1 SYNOPSIS + +Task: +Create a script that prints Prime Decomposition of a given number. The prime decomposition of a number is +defined as a list of prime numbers which when all multiplied together, are equal to that number. For example, +the Prime decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19. + +Usage: + $ perl ch-2.pl 228 + +Notes: +I used the algorithm described here: https://www.geeksforgeeks.org/print-all-prime-factors-of-a-given-number/ + +=cut + +use strict; +use warnings; +use feature 'say'; + +my $num = $ARGV[0] // ''; +die "Must pass an integer > 0 as the first argument\n" unless $num && $num =~ /^\d+$/; + +say $num == 1 + ? "No prime factors for 1" + : join(', ', prime_factors( $num ) ) +; + +exit 0; + +sub prime_factors { + my $n = shift; + + my @factors; + while ( $n % 2 == 0 ) { + push @factors, 2; + $n /= 2; + } + for ( my $i = 3; $i <= sqrt($n); $i += 2 ) { + while ( $n % $i == 0 ) { + push @factors, $i; + $n /= $i; + } + } + push @factors, $n if $n > 2; + + return @factors; +} + diff --git a/challenge-023/randy-lauen/perl5/ch-3.pl b/challenge-023/randy-lauen/perl5/ch-3.pl new file mode 100644 index 0000000000..d4fd4c6973 --- /dev/null +++ b/challenge-023/randy-lauen/perl5/ch-3.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +=head2 SYNOPSIS + +Task: +Write a script to use Random Poems API. + +Usage: + $ perl ch-3.pl + +Notes: +This script prints the shortest poem returned by the randompoems endpoint. +The Poemist API docs are located here: https://poemist.github.io/poemist-apidoc/ + +=cut + +use v5.28; +use strict; +use warnings; + +use JSON qw( decode_json ); +use List::UtilsBy qw( nsort_by ); +use Net::Curl::Simple (); + +binmode(STDOUT, "encoding(UTF-8)"); + +my $curl = Net::Curl::Simple->new(); +$curl->get( 'https://www.poemist.com/api/v1/randompoems' ); + +my ($poem) = + nsort_by { length( $_->{content} ) } + map { $_->@* } + decode_json( $curl->content ) +; + +say qq["$poem->{title}" by $poem->{poet}->{name}]; +say ''; +say $poem->{content}; +say ''; +say $poem->{url}; + +exit 0; + + diff --git a/challenge-023/randy-lauen/perl6/ch-1.p6 b/challenge-023/randy-lauen/perl6/ch-1.p6 new file mode 100644 index 0000000000..a239b28586 --- /dev/null +++ b/challenge-023/randy-lauen/perl6/ch-1.p6 @@ -0,0 +1,27 @@ +#!/usr/bin/env perl6 + +=begin SYNOPSIS + +Task: +Create a script that prints nth order forward difference series. You should be a +able to pass the list of numbers and order number as command line parameters. + +Usage: + $ perl6 ch-1.p6 --order=1 5 9 2 8 1 6 + +Notes: +I don't really understand all the intricacies of the << hyper operator and what +the different pointy directions mean, especially when the lists are different +sizes. But, after fiddling with it in the perl6 REPL, I managed to find the +behavior I was looking for. + +=end SYNOPSIS + +sub MAIN( Int :$order!, *@numbers where *.elems > 0 ) { + for 1 .. $order -> $i { + @numbers = @numbers.tail(*-1) >>->> @numbers; + say "$i: @numbers.join(', ')"; + last if @numbers.elems == 1; + } +} + diff --git a/challenge-023/randy-lauen/perl6/ch-2.p6 b/challenge-023/randy-lauen/perl6/ch-2.p6 new file mode 100644 index 0000000000..c5a5c1bd5c --- /dev/null +++ b/challenge-023/randy-lauen/perl6/ch-2.p6 @@ -0,0 +1,59 @@ +#!/usr/bin/env perl6 + +=begin SYNOPSIS + +Task: +Create a script that prints Prime Decomposition of a given number. The prime decomposition of a number is +defined as a list of prime numbers which when all multiplied together, are equal to that number. For example, +the Prime decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19. + +Usage: + $ perl6 ch-2.p6 228 # print out prime factors + $ perl6 ch-2.p6 --test # test results against Prime::Factor + +Notes: +I used the algorithm described here: https://www.geeksforgeeks.org/print-all-prime-factors-of-a-given-number/ + +=end SYNOPSIS + +sub my-prime-factors( Int $n is copy ) { + my @factors; + while $n %% 2 { + @factors.push: 2; + $n = ($n / 2).Int; + } + for 3, 5 ... sqrt($n) -> $i { + while $n %% $i { + @factors.push: $i; + $n = ($n / $i).Int; + } + } + @factors.push: $n if $n > 2; + + return @factors; +} + + +multi MAIN( Int $input where * > 0 ) { + my @factors = my-prime-factors( $input ); + say @factors.elems + ?? @factors.join(', ') + !! "No prime factors for: $input"; + ; +} + + +multi MAIN( Bool :$test! ) { + use Test; + use Prime::Factor; + + plan 1; + + my $max = 10_000; + my %mine = hyper for 1 .. $max { $_ => my-prime-factors($_).List }; + my %expected = hyper for 1 .. $max { $_ => prime-factors($_).List }; + + is-deeply( %mine, %expected, "all prime factors up to $max match" ); +} + + diff --git a/challenge-023/randy-lauen/perl6/ch-3.p6 b/challenge-023/randy-lauen/perl6/ch-3.p6 new file mode 100644 index 0000000000..7ca7213c0a --- /dev/null +++ b/challenge-023/randy-lauen/perl6/ch-3.p6 @@ -0,0 +1,31 @@ +#!/usr/bin/env perl6 + +=begin SYNOPSIS + +Task: +Write a script to use Random Poems API. + +Usage: + $ perl6 ch-3.p6 + +Notes: +This script prints the shortest poem returned by the randompoems endpoint. +The Poemist API docs are located here: https://poemist.github.io/poemist-apidoc/ + +=end SYNOPSIS + +use Cro::HTTP::Client; + +my $response = await Cro::HTTP::Client.get( "https://www.poemist.com/api/v1/randompoems" ); +my $json = await $response.body; +my $poem = $json.sort({ .<content>.chars }).first; + +say qq["$poem<title>" by $poem<poet><name>]; +say ''; +say $poem<content>; +say ''; +say $poem<url>; + +exit; + + |
