diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-04-07 04:57:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-07 04:57:30 +0100 |
| commit | 6fd99c7e57ba3d1689c58bfd90c85ce8a5d0e8b9 (patch) | |
| tree | cd737d95ad497ee1fb3de3fba1e165de1e97fd57 | |
| parent | ec7dcbf9d0bcce338475367779bd91081374d589 (diff) | |
| parent | 84acbc5f5c5b466d5d2218a648a4283e5a8da2db (diff) | |
| download | perlweeklychallenge-club-6fd99c7e57ba3d1689c58bfd90c85ce8a5d0e8b9.tar.gz perlweeklychallenge-club-6fd99c7e57ba3d1689c58bfd90c85ce8a5d0e8b9.tar.bz2 perlweeklychallenge-club-6fd99c7e57ba3d1689c58bfd90c85ce8a5d0e8b9.zip | |
Merge pull request #5895 from pauloscustodio/master
Add Perl solution to challenge 040
24 files changed, 430 insertions, 0 deletions
diff --git a/challenge-040/paulo-custodio/Makefile b/challenge-040/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-040/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-040/paulo-custodio/README b/challenge-040/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-040/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-040/paulo-custodio/perl/ch-1.pl b/challenge-040/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..897def825f --- /dev/null +++ b/challenge-040/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +# Challenge 040 +# +# TASK #1 +# Show multiple arrays content +# You are given two or more arrays. Write a script to display values of each +# list at a given index. +# +# For example: +# +# Array 1: [ I L O V E Y O U ] +# Array 2: [ 2 4 0 3 2 0 1 9 ] +# Array 3: [ ! ? @ $ % ^ & * ] +# We expect the following output: +# +# I 2 ! +# L 4 ? +# O 0 @ +# V 3 $ +# E 2 % +# Y 0 ^ +# O 1 & +# U 9 * + +use Modern::Perl; + +show_multiple([qw( I L O V E Y O U )], + [qw( 2 4 0 3 2 0 1 9 )], + [qw( ! ? @ $ % ^ & * )]); + +sub show_multiple { + my(@data) = @_; + for my $i (0 .. $#{$data[0]}) { + for (@data) { + print $_->[$i]," "; + } + print "\n"; + } +} diff --git a/challenge-040/paulo-custodio/perl/ch-2.pl b/challenge-040/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..eff9d7cd74 --- /dev/null +++ b/challenge-040/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +# Challenge 040 +# +# TASK #2 +# Sort SubList +# You are given a list of numbers and set of indices belong to the list. +# Write a script to sort the values belongs to the indices. +# +# For example, +# +# List: [ 10, 4, 1, 8, 12, 3 ] +# Indices: 0,2,5 +# We would sort the values at indices 0, 2 and 5 i.e. 10, 1 and 3. +# +# Final List would look like below: +# +# List: [ 1, 4, 3, 8, 12, 10 ] + +use Modern::Perl; + +my @list = (10, 4, 1, 8, 12, 3); +my @indices = (0,2,5); + +@list = sort_sublist(\@list, \@indices); +say "[", join(", ", @list), "]"; + +sub sort_sublist { + my($list, $indices) = @_; + my @list = @$list; + my @indices = @$indices; + my @values = sort {$a<=>$b} @list[@indices]; + @list[@indices] = @values; + return @list; +} diff --git a/challenge-040/paulo-custodio/t/test-1.yaml b/challenge-040/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..f64e7676f4 --- /dev/null +++ b/challenge-040/paulo-custodio/t/test-1.yaml @@ -0,0 +1,13 @@ +- setup: + cleanup: + args: + input: + output: | + |I 2 ! + |L 4 ? + |O 0 @ + |V 3 $ + |E 2 % + |Y 0 ^ + |O 1 & + |U 9 * diff --git a/challenge-040/paulo-custodio/t/test-2.yaml b/challenge-040/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..78fcc55415 --- /dev/null +++ b/challenge-040/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: [1, 4, 3, 8, 12, 10] diff --git a/challenge-041/paulo-custodio/Makefile b/challenge-041/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-041/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-041/paulo-custodio/README b/challenge-041/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-041/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-041/paulo-custodio/perl/ch-1.pl b/challenge-041/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..0c8f0c5420 --- /dev/null +++ b/challenge-041/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl + +# Challenge 041 +# +# TASK #1 +# Write a script to display attractive number between 1 and 50. +# A number is an attractive number if the number of its prime factors is also +# prime number. +# +# The number 20 is an attractive number, whose prime factors are 2, 2 and 5. +# The total prime factors is 3 which is also a prime number. + +use Modern::Perl; +use ntheory qw( is_prime factor ); + +my @attractive = grep {is_attractive($_)} 1..50; +say join(", ", @attractive); + +sub is_attractive { + my($n) = @_; + my @factors = factor($n); + return is_prime(scalar(@factors)); +} diff --git a/challenge-041/paulo-custodio/perl/ch-2.pl b/challenge-041/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..3d4e3e33fe --- /dev/null +++ b/challenge-041/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +# Challenge 041 +# +# TASK #2 +# Write a script to display first 20 Leonardo Numbers. Please checkout wiki +# page for more information. +# For example: +# +# L(0) = 1 +# L(1) = 1 +# L(2) = L(0) + L(1) + 1 = 3 +# L(3) = L(1) + L(2) + 1 = 5 +# and so on. + +use Modern::Perl; + +my @out; +for (0..19) { + push @out, leonardo($_); +} +say join(", ", @out); + + +sub leonardo { + my($n) = @_; + if ($n < 2) { + return 1; + } + else { + return leonardo($n-1)+leonardo($n-2)+1; + } +} diff --git a/challenge-041/paulo-custodio/t/test-1.yaml b/challenge-041/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..4d78f36c48 --- /dev/null +++ b/challenge-041/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 4, 6, 8, 9, 10, 12, 14, 15, 18, 20, 21, 22, 25, 26, 27, 28, 30, 32, 33, 34, 35, 38, 39, 42, 44, 45, 46, 48, 49, 50 diff --git a/challenge-041/paulo-custodio/t/test-2.yaml b/challenge-041/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..a7af00752b --- /dev/null +++ b/challenge-041/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529 diff --git a/challenge-042/paulo-custodio/Makefile b/challenge-042/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-042/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-042/paulo-custodio/README b/challenge-042/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-042/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-042/paulo-custodio/perl/ch-1.pl b/challenge-042/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..ca1246718e --- /dev/null +++ b/challenge-042/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +# Challenge 042 +# +# TASK #1 +# Octal Number System +# Write a script to print decimal number 0 to 50 in Octal Number System. +# +# For example: +# +# Decimal 0 = Octal 0 +# Decimal 1 = Octal 1 +# Decimal 2 = Octal 2 +# Decimal 3 = Octal 3 +# Decimal 4 = Octal 4 +# Decimal 5 = Octal 5 +# Decimal 6 = Octal 6 +# Decimal 7 = Octal 7 +# Decimal 8 = Octal 10 +# and so on. + +use Modern::Perl; +use Math::BaseCnv; + +for (0..50) { + say "Decimal $_ = Octal ",cnv($_,10,8); +} diff --git a/challenge-042/paulo-custodio/perl/ch-2.pl b/challenge-042/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..f5a3afe5ed --- /dev/null +++ b/challenge-042/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +# Challenge 042 +# +# TASK #2 +# Balanced Brackets +# Write a script to generate a string with random number of ( and ) brackets. +# Then make the script validate the string if it has balanced brackets. +# +# For example: +# +# () - OK +# (()) - OK +# )( - NOT OK +# ())() - NOT OK + +use Modern::Perl; + +while (<>) { + chomp; + print "$_ - "; + 1 while s/\(\)//; + say $_ eq '' ? "OK" : "NOT OK"; +} diff --git a/challenge-042/paulo-custodio/t/test-1.yaml b/challenge-042/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..34693b8c6c --- /dev/null +++ b/challenge-042/paulo-custodio/t/test-1.yaml @@ -0,0 +1,56 @@ +- setup: + cleanup: + args: + input: + output: | + |Decimal 0 = Octal 0 + |Decimal 1 = Octal 1 + |Decimal 2 = Octal 2 + |Decimal 3 = Octal 3 + |Decimal 4 = Octal 4 + |Decimal 5 = Octal 5 + |Decimal 6 = Octal 6 + |Decimal 7 = Octal 7 + |Decimal 8 = Octal 10 + |Decimal 9 = Octal 11 + |Decimal 10 = Octal 12 + |Decimal 11 = Octal 13 + |Decimal 12 = Octal 14 + |Decimal 13 = Octal 15 + |Decimal 14 = Octal 16 + |Decimal 15 = Octal 17 + |Decimal 16 = Octal 20 + |Decimal 17 = Octal 21 + |Decimal 18 = Octal 22 + |Decimal 19 = Octal 23 + |Decimal 20 = Octal 24 + |Decimal 21 = Octal 25 + |Decimal 22 = Octal 26 + |Decimal 23 = Octal 27 + |Decimal 24 = Octal 30 + |Decimal 25 = Octal 31 + |Decimal 26 = Octal 32 + |Decimal 27 = Octal 33 + |Decimal 28 = Octal 34 + |Decimal 29 = Octal 35 + |Decimal 30 = Octal 36 + |Decimal 31 = Octal 37 + |Decimal 32 = Octal 40 + |Decimal 33 = Octal 41 + |Decimal 34 = Octal 42 + |Decimal 35 = Octal 43 + |Decimal 36 = Octal 44 + |Decimal 37 = Octal 45 + |Decimal 38 = Octal 46 + |Decimal 39 = Octal 47 + |Decimal 40 = Octal 50 + |Decimal 41 = Octal 51 + |Decimal 42 = Octal 52 + |Decimal 43 = Octal 53 + |Decimal 44 = Octal 54 + |Decimal 45 = Octal 55 + |Decimal 46 = Octal 56 + |Decimal 47 = Octal 57 + |Decimal 48 = Octal 60 + |Decimal 49 = Octal 61 + |Decimal 50 = Octal 62 diff --git a/challenge-042/paulo-custodio/t/test-2.yaml b/challenge-042/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..6a6b817d08 --- /dev/null +++ b/challenge-042/paulo-custodio/t/test-2.yaml @@ -0,0 +1,13 @@ +- setup: + cleanup: + args: + input: | + |() + |(()) + |)( + |())() + output: | + |() - OK + |(()) - OK + |)( - NOT OK + |())() - NOT OK diff --git a/challenge-043/paulo-custodio/Makefile b/challenge-043/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-043/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-043/paulo-custodio/README b/challenge-043/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-043/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-043/paulo-custodio/perl/ch-1.pl b/challenge-043/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..787812b2ef --- /dev/null +++ b/challenge-043/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +# Challenge 043 +# +# TASK #1 +# Olympic Rings +# There are 5 rings in the Olympic Logo as shown below. They are color coded as +# in Blue, Black, Red, Yellow and Green. +# +# Olympic Rings +# +# We have allocated some numbers to these rings as below: +# +# Blue: 8 +# Yellow: 7 +# Green: 5 +# Red: 9 +# The Black ring is empty currently. You are given the numbers 1, 2, 3, 4 and 6. +# Write a script to place these numbers in the rings so that the sum of numbers +# in each ring is exactly 11. + +use Modern::Perl; + +my $total = 11; + +my $red = 9; +my $red_green = $total-$red; + +my $green = 5; +my $green_black = $total-$green-$red_green; + +my $blue = 8; +my $blue_yellow = $total-$blue; + +my $yellow = 7; +my $yellow_black = $total-$yellow-$blue_yellow; + +my $black = $total-$green_black-$yellow_black; + +assert($red+$red_green==$total); +assert($red_green+$green+$green_black==$total); +assert($green_black+$black+$yellow_black==$total); +assert($yellow_black+$yellow+$blue_yellow==$total); +assert($blue_yellow+$blue==$total); + +say "$red $red_green $green $green_black $black $yellow_black $yellow $blue_yellow $blue"; + +sub assert { + my($f) = @_; + $f or die "assertion failed"; +} diff --git a/challenge-043/paulo-custodio/perl/ch-2.pl b/challenge-043/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..4295a76046 --- /dev/null +++ b/challenge-043/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl + +# Challenge 043 +# +# TASK #2 +# Self-descriptive Numbers +# Contributed by Laurent Rosenfeld +# Write a script to generate Self-descriptive Numbers in a given base. +# +# In mathematics, a self-descriptive number is an integer m that in a given base +# b is b digits long in which each digit d at position n (the most significant +# digit being at position 0 and the least significant at position b - 1) counts +# how many instances of digit n are in m. +# +# For example, if the given base is 10, then script should print 6210001000. For +# more information, please checkout wiki page. + +use Modern::Perl; + +my $base = shift||4; +print_self_descriptive($base); + +sub print_self_descriptive { + my($base) = @_; + my @n = (1, (0) x ($base-1)); + while (@n == $base) { + if (is_self_descriptive(@n)) { + say join '', @n; + return; + } + increment(\@n, $base); + } +} + +sub increment { + my($n, $base) = @_; + my $i = $#$n; + while ($i >= 0) { + $n->[$i]++; + if ($n->[$i] < $base) { + return; + } + else { + $n->[$i] = 0; + $i--; + } + } + unshift @$n, 1; +} + +sub is_self_descriptive { + my(@n) = @_; + for my $i (0..$#n) { + my $count = scalar grep {$_==$i} @n; + return if $n[$i] != $count; + } + return 1; +} diff --git a/challenge-043/paulo-custodio/t/test-1.yaml b/challenge-043/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..a7f06c1429 --- /dev/null +++ b/challenge-043/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 9 2 5 4 6 1 7 3 8 diff --git a/challenge-043/paulo-custodio/t/test-2.yaml b/challenge-043/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..812c38975b --- /dev/null +++ b/challenge-043/paulo-custodio/t/test-2.yaml @@ -0,0 +1,25 @@ +- setup: + cleanup: + args: 4 + input: + output: 1210 +- setup: + cleanup: + args: 5 + input: + output: 21200 +- setup: + cleanup: + args: 7 + input: + output: 3211000 +- setup: + cleanup: + args: 8 + input: + output: 42101000 +- setup: + cleanup: + args: 9 + input: + output: 521001000 |
