diff options
| author | Doomtrain14 <yet.ebreo@gmail.com> | 2019-08-31 22:47:23 +0800 |
|---|---|---|
| committer | Doomtrain14 <yet.ebreo@gmail.com> | 2019-08-31 22:47:23 +0800 |
| commit | 7525469d9065c775bd3ef46218e5b7c7fbd82701 (patch) | |
| tree | fb7a94cbd0180f62bd099720f6ea061bf285e582 | |
| parent | 3bec884d01428a84e619d408becbd8639e4daa9b (diff) | |
| parent | 603db09fe93651972fe09d670d1a8e14f9806235 (diff) | |
| download | perlweeklychallenge-club-7525469d9065c775bd3ef46218e5b7c7fbd82701.tar.gz perlweeklychallenge-club-7525469d9065c775bd3ef46218e5b7c7fbd82701.tar.bz2 perlweeklychallenge-club-7525469d9065c775bd3ef46218e5b7c7fbd82701.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
63 files changed, 5890 insertions, 4978 deletions
diff --git a/challenge-001/arne-sommer/README b/challenge-001/arne-sommer/README new file mode 100644 index 0000000000..b5d88e4df4 --- /dev/null +++ b/challenge-001/arne-sommer/README @@ -0,0 +1 @@ +Solution by Arne Sommer. diff --git a/challenge-001/arne-sommer/blog.txt b/challenge-001/arne-sommer/blog.txt new file mode 100644 index 0000000000..3984d2819e --- /dev/null +++ b/challenge-001/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://perl6.eu/fizz-e-buzz.html diff --git a/challenge-001/arne-sommer/perl5/ch-1.pl b/challenge-001/arne-sommer/perl5/ch-1.pl new file mode 100644 index 0000000000..42b85ec205 --- /dev/null +++ b/challenge-001/arne-sommer/perl5/ch-1.pl @@ -0,0 +1,9 @@ +#! /usr/bin/env perl + +use feature say; + +my $string = $ARGV[0] || 'Perl Weekly Challenge'; # [1] + +my $count = $string =~ tr/e/E/; # [2] + +say "$string (with $count replacements)."; # [3] diff --git a/challenge-001/arne-sommer/perl5/ch-2.sh b/challenge-001/arne-sommer/perl5/ch-2.sh new file mode 100644 index 0000000000..1a99fb2ca3 --- /dev/null +++ b/challenge-001/arne-sommer/perl5/ch-2.sh @@ -0,0 +1 @@ +perl -E 'say $_ % 5 == 0 ? ( $_ % 3 == 0 ? "fizzbuzz" : "buzz" ) : ($_ % 3 == 0? "fizz" : $_ ) for (1..20)' diff --git a/challenge-001/arne-sommer/perl6/ch-1.p6 b/challenge-001/arne-sommer/perl6/ch-1.p6 new file mode 100644 index 0000000000..8f415fb33c --- /dev/null +++ b/challenge-001/arne-sommer/perl6/ch-1.p6 @@ -0,0 +1,8 @@ +sub MAIN (Str $string is copy = 'Perl Weekly Challenge'); # [1] +{ + my $count = $string ~~ tr/e/E/; + + say "$string (with $count replacements)."; +} + + diff --git a/challenge-001/arne-sommer/perl6/ch-2.p6 b/challenge-001/arne-sommer/perl6/ch-2.p6 new file mode 100644 index 0000000000..37c3c96df0 --- /dev/null +++ b/challenge-001/arne-sommer/perl6/ch-2.p6 @@ -0,0 +1,15 @@ +for 1 .. 20 -> $curr # [1] +{ + if $curr %% 5 # [2] + { + $curr %% 3 ?? say 'fizzbuzz' !! say 'buzz'; # [3] + } + elsif $curr %% 3 # [4] + { + say 'fizz'; # [4] + } + else + { + say $curr; # [5] + } +} diff --git a/challenge-023/andrezgz/perl5/ch-3.pl b/challenge-023/andrezgz/perl5/ch-3.pl new file mode 100644 index 0000000000..4950dc47ee --- /dev/null +++ b/challenge-023/andrezgz/perl5/ch-3.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-023/ +# Task #3 +# Write a script to use Random Poems API. This is the easiset API, I have come across so far. +# You don't need API key for this. They have only route to work with (GET). +# https://www.poemist.com/api/v1/randompoems +# The API task is optional but we would love to see your solution. + +use strict; +use warnings; + +use LWP::Simple qw (get); +use JSON qw( decode_json ); +binmode STDOUT, ':encoding(UTF-8)'; + + +my $json = get('https://www.poemist.com/api/v1/randompoems'); +die 'No poem for you right now :(' unless defined $json; +my $poems = decode_json( $json ); + +my $rnd_poem = $poems->[int rand scalar @$poems]; +my $heading = $rnd_poem->{title}.' by '.$rnd_poem->{poet}->{name}; +my $line = q{=} x length $heading; + +print sprintf "%s\n%s\n\n%s\n", $heading, $line, $rnd_poem->{content}; diff --git a/challenge-023/arne-sommer/perl5/ch-3.pl b/challenge-023/arne-sommer/perl5/ch-3.pl index c6656f574c..6b00bb150b 100755 --- a/challenge-023/arne-sommer/perl5/ch-3.pl +++ b/challenge-023/arne-sommer/perl5/ch-3.pl @@ -15,7 +15,7 @@ my $json = get('https://www.poemist.com/api/v1/randompoems'); my $data = from_json($json); -for ($count) +for (0 .. $count -1) { last unless $data->[$_]; diff --git a/challenge-023/e-choroba/perl5/ch-1.pl b/challenge-023/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..6eb1ba0f44 --- /dev/null +++ b/challenge-023/e-choroba/perl5/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +use List::Util qw{ sum }; + +# Recursive implementation. + +sub fds { + my ($seq, $order) = @_; + my @r = map $seq->[$_] - $seq->[$_ - 1], 1 .. $#$seq; + return @r if $order == 1; + + return fds(\@r, $order - 1) +} + +# Faster, uses memoizing and formulas for the given order. + +{ my %cache; + sub binomial { + my ($n, $k) = @_; + return $cache{$n}{$k} if $cache{$n}{$k}; + my $r = 1; + $r *= $_ for 2 .. $n; + $r /= $_ for 2 .. $k, 2 .. ($n - $k); + return $cache{$n}{$k} = $r + } +} + +sub fds2 { + my ($seq, $order) = @_; + my @coefficients = map binomial($order, $_), 0 .. $order; + $coefficients[2 * $_] *= -1 for -($order + 1) / 2 .. -1; + return map { + my $i = $_; + sum(map $seq->[$i + $_] * $coefficients[$_], 0 .. $order) + } 0 .. $#$seq - $order +} + +my $order = pop; +say join ' ', fds2([@ARGV], $order); diff --git a/challenge-023/e-choroba/perl5/ch-2.pl b/challenge-023/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..c95b5157eb --- /dev/null +++ b/challenge-023/e-choroba/perl5/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; +use List::Util qw{ any }; + +sub prime_decomposition { + my ($n) = @_; + my %primes; + for my $i (2 .. $n) { + next if any { 0 == $i % $_ } keys %primes; + + $primes{$i} = 0; + until ($n % $i) { + ++$primes{$i}; + $n /= $i; + } + last if 1 == $n; + } + return map +($_) x $primes{$_}, sort { $a <=> $b } keys %primes +} + +say join ' ', prime_decomposition(shift); diff --git a/challenge-023/kevin-colyer/perl6/ch-1.p6 b/challenge-023/kevin-colyer/perl6/ch-1.p6 new file mode 100644 index 0000000000..95ec64b448 --- /dev/null +++ b/challenge-023/kevin-colyer/perl6/ch-1.p6 @@ -0,0 +1,36 @@ +#!/usr/bin/perl6 +use v6; + +use Test; + +# 23.1 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. Let me show you with an example. + +# Suppose we have list (X) of numbers: 5, 9, 2, 8, 1, 6 and we would like to create 1st order forward difference series (Y). So using the formula Y(i) = X(i+1) - X(i), we get the following numbers: (9-5), (2-9), (8-2), (1-8), (6-1). In short, the final series would be: 4, -7, 6, -7, 5. If you noticed, it has one less number than the original series. Similary you can carry on 2nd order forward difference series like: (-7-4), (6+7), (-7-6), (5+7) => -11, 13, -13, 12.\ + +sub NthOrderForwardDifference(@X is copy,$n) { + my @Y; + return @Y if $n >= @X.elems; + for ^$n ->$j { + # say "{$j}th loop - X=[{@X}]".indent($j); + @Y = gather for ^(@X.elems-1) -> $i { + take @X[$i+1]-@X[$i]; + } + @X=@Y; + } + return @Y; +} +#| Produce Nth Order Forward Difference sequence from list of integers +multi MAIN(Int $order, *@list where {$_.all ~~ Int} ) { + say NthOrderForwardDifference(@list,$order); +} + +multi MAIN("test") { + my @X= 5, 9, 2, 8, 1, 6; + is NthOrderForwardDifference(@X,0),[],"0th order = fail"; + is NthOrderForwardDifference(@X,6),[],"5th order on series of 6 = fail"; + is NthOrderForwardDifference(@X,1),[4, -7, 6, -7, 5],"first order"; + is NthOrderForwardDifference(@X,2),[-11, 13, -13, 12],"second order"; + is NthOrderForwardDifference(@X,3),[24, -26, 25],"third order"; + done-testing; +} + diff --git a/challenge-023/kevin-colyer/perl6/ch-2.p6 b/challenge-023/kevin-colyer/perl6/ch-2.p6 new file mode 100644 index 0000000000..e7abc03eeb --- /dev/null +++ b/challenge-023/kevin-colyer/perl6/ch-2.p6 @@ -0,0 +1,46 @@ +#!/usr/bin/perl6 +use v6; + +use Test; + +# 23.2 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. + +my @primes = (2,3,*+2 ... ∞).grep: *.is-prime; + +#| Prime Decomosition of given integer N where N>1 +multi MAIN(Int $n where $n > 1 ) { + say primeDecompose($n, True); +} + +multi MAIN("test") { + is primeDecompose(228),[2,2,3,19],"decomposes 228"; + done-testing; +} + +sub primeDecompose( $n is copy, Bool $progress=False) { + my @answer; + my Int $i=0; + print "Searching: ." if $progress; + while $n>1 { + # if $n is a prime divisor, add to result, but stay with this prime to check again... + if $n %% @primes[$i] { + $n /= @primes[$i]; + @answer.push( @primes[$i] ); + print "." if $progress; + next; + } + + # Primality check gives a significant speed up with larger ints when $n decomposes with a large prime... + # however large numbers are very hard to factorise!!! + if $n.is-prime { + @answer.push( $n ); + last; + } + + # move on to next prime... + $i++; # test next prime + } + print "\n" if $progress; + return @answer; +} + diff --git a/challenge-023/mark-anderson/perl5/ch-1.pl b/challenge-023/mark-anderson/perl5/ch-1.pl new file mode 100644 index 0000000000..c225e84204 --- /dev/null +++ b/challenge-023/mark-anderson/perl5/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use Modern::Perl '2018'; +use Getopt::Long; + +#usage: perl ch-1.pl -n=2 -- -11 13 -13 12; + +my $n = 1; +GetOptions('n=i' => \$n) ; + +my @X = (5, 9, 2, 8, 1, 6); +@X = @ARGV if @ARGV; + +while($n and @X > 1) { + my @Y; + foreach my $i (0 .. ($#X - 1)) { + push @Y, $X[$i+1] - $X[$i]; + } + $n--; + @X = @Y; +} + +$" = ", "; +say "@X"; diff --git a/challenge-023/mark-anderson/perl5/ch-2.pl b/challenge-023/mark-anderson/perl5/ch-2.pl new file mode 100644 index 0000000000..050d75104a --- /dev/null +++ b/challenge-023/mark-anderson/perl5/ch-2.pl @@ -0,0 +1,7 @@ +#!/usr/bin/env perl + +use Modern::Perl '2018'; +use Math::Prime::Util 'factor'; + +my $num = shift || 228; +say join ", ", factor($num); diff --git a/challenge-023/mark-anderson/perl5/ch-3.pl b/challenge-023/mark-anderson/perl5/ch-3.pl new file mode 100644 index 0000000000..53603f7ab7 --- /dev/null +++ b/challenge-023/mark-anderson/perl5/ch-3.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl + +use Modern::Perl '2018'; +use Mojo::UserAgent; + +my $ua = Mojo::UserAgent->new; + +my $array = $ua->get("https://www.poemist.com/api/v1/randompoems") + ->result->json; + +foreach my $i (keys $array->@*) { + printf "%-7s%s\n", "URL:", $array->[$i]->{url}; + printf "%-7s%s\n", "Poet:", $array->[$i]->{poet}->{name}; + printf "%-7s%s\n\n", "Title:", $array->[$i]->{title}; + say $array->[$i]->{content}, "\n"; +} 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; + } |
