diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-04-07 14:59:00 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-07 14:59:00 +0100 |
| commit | 9dd0fb882db93d7b5e8ad4b42a3a1b2ada449061 (patch) | |
| tree | 7d7b1719a66ba3fe3aa08b3d94da9dd817cf7c54 | |
| parent | 1f9ecf096869e3fdd17a5e9cbdcd80fba5c03bf0 (diff) | |
| parent | 20ffdf0f472b18b14672347152a7611942687f76 (diff) | |
| download | perlweeklychallenge-club-9dd0fb882db93d7b5e8ad4b42a3a1b2ada449061.tar.gz perlweeklychallenge-club-9dd0fb882db93d7b5e8ad4b42a3a1b2ada449061.tar.bz2 perlweeklychallenge-club-9dd0fb882db93d7b5e8ad4b42a3a1b2ada449061.zip | |
Merge pull request #5897 from pauloscustodio/master
Add Perl solutions
29 files changed, 411 insertions, 0 deletions
diff --git a/challenge-044/paulo-custodio/Makefile b/challenge-044/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-044/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-044/paulo-custodio/README b/challenge-044/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-044/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-044/paulo-custodio/perl/ch-1.pl b/challenge-044/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..610d74808b --- /dev/null +++ b/challenge-044/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +# Challenge 044 +# +# TASK #1 +# Only 100, please. +# You are given a string “123456789”. Write a script that would insert +# “+” or "-" in between digits so that when you evaluate, the result should +# be 100. + +use Modern::Perl; + +for my $a ('', '-', '+') { + for my $b ('', '-', '+') { + for my $c ('', '-', '+') { + for my $d ('', '-', '+') { + for my $e ('', '-', '+') { + for my $f ('', '-', '+') { + for my $g ('', '-', '+') { + for my $h ('', '-', '+') { + my $test = "1${a}2${b}3${c}4${d}5${e}6${f}7${g}8${h}9"; + my $res = eval $test; + if ($res==100) { + say $test; + } + } + } + } + } + } + } + } +} diff --git a/challenge-044/paulo-custodio/perl/ch-2.pl b/challenge-044/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b6afba3e3d --- /dev/null +++ b/challenge-044/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +# Challenge 044 +# +# TASK #2 +# Make it $200 +# You have only $1 left at the start of the week. You have been given an +# opportunity to make it $200. The rule is simple with every move you can either +# double what you have or add another $1. Write a script to help you get $200 +# with the smallest number of moves. + +use Modern::Perl; +no warnings 'recursion'; + +my $min = join("+", (1)x200); +find_min("1", 1); +say $min; + +sub find_min { + my($str, $num) = @_; + if ($num > 200) { + } + elsif ($num == 200) { + if (length($str) < length($min)) { + $min = $str; + } + } + else { + find_min("$str*2", $num*2); + find_min("$str+1", $num+1); + } +} diff --git a/challenge-044/paulo-custodio/t/test-1.yaml b/challenge-044/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..aa228c82a0 --- /dev/null +++ b/challenge-044/paulo-custodio/t/test-1.yaml @@ -0,0 +1,16 @@ +- setup: + cleanup: + args: + input: + output: | + |123-45-67+89 + |123-4-5-6-7+8-9 + |123+45-67+8-9 + |123+4-5+67-89 + |12-3-4+5-6+7+89 + |12+3-4+5+67+8+9 + |12+3+4+5-6-7+89 + |1+23-4+56+7+8+9 + |1+23-4+5+6+78-9 + |1+2+34-5+67-8+9 + |1+2+3-4+5+6+78+9 diff --git a/challenge-044/paulo-custodio/t/test-2.yaml b/challenge-044/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..2fd9e639c9 --- /dev/null +++ b/challenge-044/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 1*2+1*2*2*2+1*2*2*2 diff --git a/challenge-045/paulo-custodio/Makefile b/challenge-045/paulo-custodio/Makefile new file mode 100644 index 0000000000..6a9bfeacd6 --- /dev/null +++ b/challenge-045/paulo-custodio/Makefile @@ -0,0 +1,3 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl + perl ./perl/ch-2.pl | diff - ./perl/ch-2.pl
\ No newline at end of file diff --git a/challenge-045/paulo-custodio/README b/challenge-045/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-045/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-045/paulo-custodio/perl/ch-1.pl b/challenge-045/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..a7ae828b0e --- /dev/null +++ b/challenge-045/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl + +# Challenge 045 +# +# TASK #1 +# Square Secret Code +# The square secret code mechanism first removes any space from the original +# message. Then it lays down the message in a row of 8 columns. The coded message +# is then obtained by reading down the columns going left to right. +# +# For example, the message is “The quick brown fox jumps over the lazy dog”. +# +# Then the message would be laid out as below: +# +# thequick +# brownfox +# jumpsove +# rthelazy +# dog +# The code message would be as below: +# +# tbjrd hruto eomhg qwpe unsl ifoa covz kxey +# Write a script that accepts a message from command line and prints the +# equivalent coded message. + +use Modern::Perl; + +say encode("@ARGV"); + +sub encode { + my($mess) = @_; + $mess = lc($mess); + $mess =~ s/\W//g; + + my @box; + while ($mess ne '') { + push @box, substr($mess,0,8); + $mess = length($mess)>=8 ? substr($mess, 8) : ''; + } + + my $encoded = ''; + for my $col (0..7) { + $encoded .= ' ' if $encoded ne ''; + for my $row (0 .. $#box) { + if (length($box[$row]) >= $col) { + $encoded .= substr($box[$row], $col, 1); + } + } + } + + return $encoded; +} diff --git a/challenge-045/paulo-custodio/perl/ch-2.pl b/challenge-045/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..f92859885f --- /dev/null +++ b/challenge-045/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl + +# Challenge 045 +# +# TASK #2 +# Source Dumper +# Write a script that dumps its own source code. For example, say, the script +# name is ch-2.pl then the following command should returns nothing. +# +# $ perl ch-2.pl | diff - ch-2.pl + +use Modern::Perl; + +@ARGV = ($0); +print <>; diff --git a/challenge-045/paulo-custodio/t/test-1.yaml b/challenge-045/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..993708c0b3 --- /dev/null +++ b/challenge-045/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: the quick brown fox jumps over the lazy dog + input: + output: tbjrd hruto eomhg qwpe unsl ifoa covz kxey diff --git a/challenge-046/paulo-custodio/Makefile b/challenge-046/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-046/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-046/paulo-custodio/README b/challenge-046/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-046/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-046/paulo-custodio/perl/ch-1.pl b/challenge-046/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..e5461d09b6 --- /dev/null +++ b/challenge-046/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl + +# Challenge 046 +# +# TASK #1 +# Cryptic Message +# The communication system of an office is broken and message received are not +# completely reliable. To send message Hello, it ended up sending these following: +# +# H x l 4 ! +# c e - l o +# z e 6 l g +# H W l v R +# q 9 m # o +# Similary another day we received a message repeatedly like below: +# +# P + 2 l ! a t o +# 1 e 8 0 R $ 4 u +# 5 - r ] + a > / +# P x w l b 3 k \ +# 2 e 3 5 R 8 y u +# < ! r ^ ( ) k 0 +# Write a script to decrypt the above repeated message (one message repeated 6 +# times). +# +# HINT: Look for characters repeated in a particular position in all six messages +# received. + +use Modern::Perl; + +my @input = + ("P+2l!ato", + "1e80R\$4u", + "5-r]+a>/", + "Pxwlb3k\\", + "2e35R8yu", + "<!r^()k0"); + +say decode(@input); + +sub decode { + my(@input) = @_; + my $decoded = ""; + for my $col (0..length($input[0])-1) { + my %hist; + my $max_letter = ''; my $max_count = 0; + for my $row (@input) { + my $letter = substr($row, $col, 1); + $hist{$letter}++; + if ($hist{$letter} > $max_count) { + ($max_letter, $max_count) = ($letter, $hist{$letter}); + } + } + $decoded .= $max_letter; + } + return $decoded; +} diff --git a/challenge-046/paulo-custodio/perl/ch-2.pl b/challenge-046/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..33cd97e265 --- /dev/null +++ b/challenge-046/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +# Challenge 046 +# +# TASK #2 +# Is the room open? +# There are 500 rooms in a hotel with 500 employees having keys to all the rooms. +# The first employee opened main entrance door of all the rooms. The second +# employee then closed the doors of room numbers 2,4,6,8,10 and so on to 500. The +# third employee then closed the door if it was opened or opened the door if it +# was closed of rooms 3,6,9,12,15 and so on to 500. Similarly the fourth employee +# did the same as the third but only room numbers 4,8,12,16 and so on to 500. +# This goes on until all employees has had a turn. +# +# Write a script to find out all the rooms still open at the end. + +use Modern::Perl; + +my @rooms = (0, (0)x500); +for my $emp (1..500) { + for (my $door = $emp; $door < @rooms; $door += $emp) { + $rooms[$door] = 1-$rooms[$door]; + } +} +my @doors = map {$_->[0]} grep {$_->[1]} map {[$_ => $rooms[$_]]} (1..500); +say join(", ", @doors); diff --git a/challenge-046/paulo-custodio/t/test-1.yaml b/challenge-046/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..d4da5ac307 --- /dev/null +++ b/challenge-046/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: PerlRaku diff --git a/challenge-046/paulo-custodio/t/test-2.yaml b/challenge-046/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..bfc39eaaed --- /dev/null +++ b/challenge-046/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484 diff --git a/challenge-047/paulo-custodio/Makefile b/challenge-047/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-047/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-047/paulo-custodio/README b/challenge-047/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-047/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-047/paulo-custodio/perl/ch-1.pl b/challenge-047/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..7888c446e3 --- /dev/null +++ b/challenge-047/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +# Challenge 047 +# +# TASK #1 +# Roman Calculator +# Write a script that accepts two roman numbers and operation. It should then +# perform the operation on the give roman numbers and print the result. +# +# For example, +# +# perl ch-1.pl V + VI +# It should print +# +# XI + +use Modern::Perl; +use Math::Roman qw(roman); + +@ARGV==3 or die "Usage: ch-1.pl xxx +- xxx\n"; + +my $a = roman($ARGV[0]); +my $op = $ARGV[1]; +my $b = roman($ARGV[2]); +if ($op eq '+') { + say $a+$b; +} +elsif ($op eq '-') { + say $a-$b; +} +else { + die "invalid operation\n"; +} diff --git a/challenge-047/paulo-custodio/perl/ch-2.pl b/challenge-047/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..d9dd8b3cc0 --- /dev/null +++ b/challenge-047/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +# Challenge 047 +# +# TASK #2 +# Gapful Number +# Write a script to print first 20 Gapful Numbers greater than or equal to 100. +# Please check out the page for more information about Gapful Numbers. + +use Modern::Perl; + +my @out; +my $n = 100; +while (@out < 20) { + push @out, $n if is_gapfull($n); + $n++; +} +say join(", ", @out); + +sub is_gapfull { + my($n) = @_; + my $div = substr($n,0,1).substr($n,-1,1); + return ($n % $div) == 0; +} diff --git a/challenge-047/paulo-custodio/t/test-1.yaml b/challenge-047/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..16bb61a604 --- /dev/null +++ b/challenge-047/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: V + VI + input: + output: XI +- setup: + cleanup: + args: V - II + input: + output: III diff --git a/challenge-047/paulo-custodio/t/test-2.yaml b/challenge-047/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..aa0e359aea --- /dev/null +++ b/challenge-047/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 100, 105, 108, 110, 120, 121, 130, 132, 135, 140, 143, 150, 154, 160, 165, 170, 176, 180, 187, 190 diff --git a/challenge-048/paulo-custodio/Makefile b/challenge-048/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-048/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-048/paulo-custodio/README b/challenge-048/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-048/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-048/paulo-custodio/perl/ch-1.pl b/challenge-048/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..68892ad5bc --- /dev/null +++ b/challenge-048/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +# Challenge 048 +# +# TASK #1 +# Survivor +# There are 50 people standing in a circle in position 1 to 50. The person +# standing at position 1 has a sword. He kills the next person i.e. standing at +# position 2 and pass on the sword to the immediate next i.e. person standing at +# position 3. Now the person at position 3 does the same and it goes on until +# only one survives. +# +# Write a script to find out the survivor. + +use Modern::Perl; + +my @surv = (1..50); +my $p = 0; +while (@surv > 1) { + # kill next + if ($p >= $#surv) { + @surv = @surv[1..$#surv]; + } + else { + splice(@surv, $p+1, 1); + } + # advance sword + $p++; + $p = 0 if $p >= @surv; +} +say @surv; diff --git a/challenge-048/paulo-custodio/perl/ch-2.pl b/challenge-048/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..27e9fe5bc8 --- /dev/null +++ b/challenge-048/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +# Challenge 048 +# +# TASK #2 +# Palindrome Dates +# Write a script to print all Palindrome Dates between 2000 and 2999. The format +# of date is mmddyyyy. For example, the first one was on October 2, 2001 as it is +# represented as 10022001. + +use Modern::Perl; +use DateTime; + +my $dt = DateTime->new(year=>2000, month=>1, day=>1); +while ($dt->year < 2100) { + my $txt = $dt->strftime("%m%d%Y"); + say $txt if reverse($txt) eq $txt; # is palindrome + $dt->add(days=>1); +} diff --git a/challenge-048/paulo-custodio/t/test-1.yaml b/challenge-048/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..94725595bb --- /dev/null +++ b/challenge-048/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 37 diff --git a/challenge-048/paulo-custodio/t/test-2.yaml b/challenge-048/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..fdb31f1c58 --- /dev/null +++ b/challenge-048/paulo-custodio/t/test-2.yaml @@ -0,0 +1,17 @@ +- setup: + cleanup: + args: + input: + output: | + |10022001 + |01022010 + |11022011 + |02022020 + |12022021 + |03022030 + |04022040 + |05022050 + |06022060 + |07022070 + |08022080 + |09022090 |
