diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-13 11:53:24 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-13 11:53:24 +0100 |
| commit | 7bb9ebdb66464c211d36fd2c737ccd611328f154 (patch) | |
| tree | 123abd40787b144d76eaabb87528e77cae3bd969 | |
| parent | fffef0a0521b04060898de864398ed258774a0c0 (diff) | |
| download | perlweeklychallenge-club-7bb9ebdb66464c211d36fd2c737ccd611328f154.tar.gz perlweeklychallenge-club-7bb9ebdb66464c211d36fd2c737ccd611328f154.tar.bz2 perlweeklychallenge-club-7bb9ebdb66464c211d36fd2c737ccd611328f154.zip | |
Add Perl solution to challenge 054
| -rw-r--r-- | challenge-054/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-054/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-054/paulo-custodio/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-054/paulo-custodio/perl/ch-2.pl | 53 | ||||
| -rw-r--r-- | challenge-054/paulo-custodio/t/test-1.yaml | 11 | ||||
| -rw-r--r-- | challenge-054/paulo-custodio/t/test-2.yaml | 25 |
6 files changed, 121 insertions, 0 deletions
diff --git a/challenge-054/paulo-custodio/Makefile b/challenge-054/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-054/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-054/paulo-custodio/README b/challenge-054/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-054/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-054/paulo-custodio/perl/ch-1.pl b/challenge-054/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..4aaa753da6 --- /dev/null +++ b/challenge-054/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +# Challenge 054 +# +# TASK #1 +# kth Permutation Sequence +# Write a script to accept two integers n (>=1) and k (>=1). It should print the +# kth permutation of n integers. For more information, please follow the wiki +# page. +# +# For example, n=3 and k=4, the possible permutation sequences are listed below: +# +# 123 +# 132 +# 213 +# 231 +# 312 +# 321 +# The script should print the 4th permutation sequence 231. + +use Modern::Perl; +use Math::Combinatorics; + +my($n, $k) = @ARGV; +my @data = (1..$n); +my $combinat = Math::Combinatorics->new(count => $k, data => \@data); +while (my @permu = $combinat->next_permutation) { + say @permu; +} diff --git a/challenge-054/paulo-custodio/perl/ch-2.pl b/challenge-054/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..5aa109c437 --- /dev/null +++ b/challenge-054/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl + +# Challenge 054 +# +# TASK #2 +# Collatz Conjecture +# Contributed by Ryan Thompson +# It is thought that the following sequence will always reach 1: +# +# $n = $n / 2 when $n is even +# $n = 3*$n + 1 when $n is odd +# For example, if we start at 23, we get the following sequence: +# +# 23 ? 70 ? 35 ? 106 ? 53 ? 160 ? 80 ? 40 ? 20 ? 10 ? 5 ? 16 ? 8 ? 4 ? 2 ? 1 +# +# Write a function that finds the Collatz sequence for any positive integer. +# Notice how the sequence itself may go far above the original starting number. +# +# Extra Credit +# Have your script calculate the sequence length for all starting numbers up to +# 1000000 (1e6), and output the starting number and sequence length for the +# longest 20 sequences. + +use Modern::Perl; + +my @longest; +for my $n (1..1e6) { + my @seq = collatz($n); + my $len = scalar(@seq); + push @longest, [$n => $len]; + @longest = sort {$b->[1] <=> $a->[1]} @longest; + pop @longest if @longest > 20; +} + +for (@longest) { + my($n, $len) = @$_; + say "$n $len"; +} + +sub collatz { + my($n) = @_; + my @out = ($n); + while ($n != 1) { + if ($n%2==0) { + $n /= 2; + } + else { + $n = 3*$n+1; + } + push @out, $n; + } + return @out; +} diff --git a/challenge-054/paulo-custodio/t/test-1.yaml b/challenge-054/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..4f1abdea10 --- /dev/null +++ b/challenge-054/paulo-custodio/t/test-1.yaml @@ -0,0 +1,11 @@ +- setup: + cleanup: + args: 3 3 + input: + output: | + |123 + |132 + |213 + |231 + |312 + |321 diff --git a/challenge-054/paulo-custodio/t/test-2.yaml b/challenge-054/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..78babb1408 --- /dev/null +++ b/challenge-054/paulo-custodio/t/test-2.yaml @@ -0,0 +1,25 @@ +- setup: + cleanup: + args: 20 + input: + output: | + |837799 525 + |626331 509 + |939497 507 + |704623 504 + |910107 476 + |927003 476 + |511935 470 + |767903 468 + |796095 468 + |970599 458 + |546681 452 + |818943 450 + |820022 450 + |820023 450 + |410011 449 + |615017 447 + |886953 445 + |906175 445 + |922524 445 + |922525 445 |
