diff options
71 files changed, 3803 insertions, 2443 deletions
diff --git a/challenge-108/paulo-custodio/perl/ch-1.pl b/challenge-108/paulo-custodio/perl/ch-1.pl index 333532278a..87e7548d1b 100644..100755 --- a/challenge-108/paulo-custodio/perl/ch-1.pl +++ b/challenge-108/paulo-custodio/perl/ch-1.pl @@ -1,11 +1,11 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 108 # -# TASK #1 › Locate Memory +# TASK #1 - Locate Memory # Submitted by: Mohammad S Anwar # -# Write a script to declare a variable or constant and print it’s location in +# Write a script to declare a variable or constant and print it's location in # the memory. use Modern::Perl; diff --git a/challenge-108/paulo-custodio/perl/ch-2.pl b/challenge-108/paulo-custodio/perl/ch-2.pl index d76209a4e8..c709a3010b 100644..100755 --- a/challenge-108/paulo-custodio/perl/ch-2.pl +++ b/challenge-108/paulo-custodio/perl/ch-2.pl @@ -1,8 +1,8 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 108 # -# TASK #2 › Bell Numbers +# TASK #2 - Bell Numbers # Submitted by: Mohammad S Anwar # # Write a script to display top 10 Bell Numbers. Please refer to wikipedia page diff --git a/challenge-108/paulo-custodio/test.pl b/challenge-108/paulo-custodio/test.pl index 01ed2b83cd..cf1ced98e0 100644..100755 --- a/challenge-108/paulo-custodio/test.pl +++ b/challenge-108/paulo-custodio/test.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl use strict; use warnings; diff --git a/challenge-114/paulo-custodio/perl/ch-1.pl b/challenge-114/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..c543df4a7e --- /dev/null +++ b/challenge-114/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +# Challenge 114 +# +# TASK #1 - Next Palindrome Number +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# +# Write a script to find out the next Palindrome Number higher than the given +# integer $N. +# +# Example +# Input: $N = 1234 +# Output: 1331 +# +# Input: $N = 999 +# Output: 1001 + +use Modern::Perl; + +my $N = shift || 0; +say next_palindrome($N); + +sub is_palindrome { + my($n) = @_; + return reverse($n)==$n; +} + +sub next_palindrome { + my($n) = @_; + while (1) { + $n++; + return $n if is_palindrome($n); + } +} diff --git a/challenge-114/paulo-custodio/perl/ch-2.pl b/challenge-114/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..c76fcaf286 --- /dev/null +++ b/challenge-114/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +# Challenge 114 +# +# TASK #2 - Higher Integer Set Bits +# Submitted by: Mohammad S Anwar +# You are given a positive integer $N. +# +# Write a script to find the next higher integer having the same number of +# 1 bits in binary representation as $N. +# +# Example +# Input: $N = 3 +# Output: 5 +# +# Binary representation of $N is 011. There are two 1 bits. So the next higher +# integer is 5 having the same the number of 1 bits i.e. 101. +# +# Input: $N = 12 +# Output: 17 +# +# Binary representation of $N is 1100. There are two 1 bits. So the next higher +# integer is 17 having the same number of 1 bits i.e. 10001. + +use Modern::Perl; + +my $N = shift || 0; +say next_same_1bits($N); + +sub num_1bits { + my($n) = @_; + my $sum = 0; + map {$sum += $_} split //, sprintf("%b", $n); + return $sum; +} + +sub next_same_1bits { + my($n) = @_; + my $num1s = num_1bits($n); + while (1) { + $n++; + return $n if num_1bits($n) == $num1s; + } +} diff --git a/challenge-114/paulo-custodio/t/test-1.yaml b/challenge-114/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..8a671155df --- /dev/null +++ b/challenge-114/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 1234 + input: + output: 1331 +- setup: + cleanup: + args: 999 + input: + output: 1001 diff --git a/challenge-114/paulo-custodio/t/test-2.yaml b/challenge-114/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..5f1d95cf31 --- /dev/null +++ b/challenge-114/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 3 + input: + output: 5 +- setup: + cleanup: + args: 12 + input: + output: 17 diff --git a/challenge-114/paulo-custodio/test.pl b/challenge-114/paulo-custodio/test.pl new file mode 100755 index 0000000000..cf1ced98e0 --- /dev/null +++ b/challenge-114/paulo-custodio/test.pl @@ -0,0 +1,7 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use 5.030; + +require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-115/aaronreidsmith/blog.txt b/challenge-115/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..c6fd3c9318 --- /dev/null +++ b/challenge-115/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-115/ diff --git a/challenge-115/aaronreidsmith/raku/ch-1.raku b/challenge-115/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..b9a1d0813f --- /dev/null +++ b/challenge-115/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env raku + +sub challenge(@S) returns Int { + my @solutions = @S.race.permutations.grep: -> @permutation { + my $valid = True; + for @permutation Z (|@permutation[1..*], @permutation.head) -> ($a, $b) { + if $a.comb.tail ne $b.comb.head { + $valid = False; + last; + } + } + $valid; + } + (@solutions.elems > 0).Int; +} + +multi sub MAIN(*@S where all(@S) ~~ Str) { + say challenge(@S); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + (('abc', 'dea', 'cd'), 1), + (('ade', 'cbd', 'fgh'), 0) + ); + + for @tests -> (@S, $expected) { + is(challenge(@S), $expected); + } +} diff --git a/challenge-115/aaronreidsmith/raku/ch-2.raku b/challenge-115/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..2f3dd8f59a --- /dev/null +++ b/challenge-115/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +sub challenge(@N) returns Int { + @N.race.permutations.map(*.join.Int).grep(* %% 2).max; +} + +multi sub MAIN(*@N where all(@N) ~~ /^<digit>$/) { + say challenge(@N); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + ((1, 0, 2, 6), 6210), + ((1, 4, 2, 8), 8412), + ((4, 1, 7, 6), 7614) + ); + + for @tests -> (@N, $expected) { + is(challenge(@N), $expected); + } + + done-testing; +} diff --git a/challenge-115/paulo-custodio/perl/ch-1.pl b/challenge-115/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..6d28fbd2d6 --- /dev/null +++ b/challenge-115/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +# Challenge 115 +# +# TASK #1 - String Chain +# Submitted by: Mohammad S Anwar +# You are given an array of strings. +# +# Write a script to find out if the given strings can be chained to form a +# circle. Print 1 if found otherwise 0. +# +# A string $S can be put before another string $T in circle if the last +# character of $S is same as first character of $T. +# +# Examples: +# Input: @S = ("abc", "dea", "cd") +# Output: 1 as we can form circle e.g. "abc", "cd", "dea". +# +# Input: @S = ("ade", "cbd", "fgh") +# Output: 0 as we can't form circle. + +use Modern::Perl; + +my @words = @ARGV; +@words or die "Usage: ch-1.pl words...\n"; + +say is_circle(\@words) ? 1 : 0; + +sub is_circle { + my($pending, @words) = @_; + my $found_solution; + + if (@$pending == 0) { + $found_solution ||= substr($words[-1],-1,1) eq substr($words[0],0,1); + } + else { + for my $word (@$pending) { + if (@words == 0 || + substr($words[-1],-1,1) eq substr($word,0,1)) { + my @new_pending = grep {$_ ne $word} @$pending; + $found_solution ||= is_circle(\@new_pending, @words, $word); + } + } + } + return $found_solution; +} diff --git a/challenge-115/paulo-custodio/perl/ch-2.pl b/challenge-115/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..afb6efc0ab --- /dev/null +++ b/challenge-115/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Challenge 115 +# +# TASK #2 - Largest Multiple +# Submitted by: Mohammad S Anwar +# You are given a list of positive integers (0-9), single digit. +# +# Write a script to find the largest multiple of 2 that can be formed from the +# list. +# +# Examples +# Input: @N = (1, 0, 2, 6) +# Output: 6210 +# +# Input: @N = (1, 4, 2, 8) +# Output: 8412 +# +# Input: @N = (4, 1, 7, 6) +# Output: 7614 + +use Modern::Perl; + +my @nums = @ARGV; +@nums or die "Usage: ch-1.pl words...\n"; +say largest_mult2(@nums); + +sub largest_mult2 { + my(@nums) = @_; + + # select smallest even number for last element + my @even = sort {$a->[1] <=> $b->[1]} + grep {$_->[1] % 2 == 0} + map {[$_, $nums[$_]]} 0..$#nums; + return 0 if !@even; # no even numbers + my($index, $last) = @{$even[0]}; + splice(@nums, $index, 1); # remove it from @nums + + # sort the other elements in descending order + @nums = sort {$b <=> $a} @nums; + + return 0+join('',@nums,$last); +} diff --git a/challenge-115/paulo-custodio/t/test-1.yaml b/challenge-115/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..017eb0af04 --- /dev/null +++ b/challenge-115/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: abc dea cd + input: + output: 1 +- setup: + cleanup: + args: ade cbd fgh + input: + output: 0 diff --git a/challenge-115/paulo-custodio/t/test-2.yaml b/challenge-115/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..6acb5ad25d --- /dev/null +++ b/challenge-115/paulo-custodio/t/test-2.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 1 0 2 6 + input: + output: 6210 +- setup: + cleanup: + args: 1 4 2 8 + input: + output: 8412 +- setup: + cleanup: + args: 4 1 7 6 + input: + output: 7614 +- setup: + cleanup: + args: 1 3 5 7 + input: + output: 0 diff --git a/challenge-115/paulo-custodio/test.pl b/challenge-115/paulo-custodio/test.pl new file mode 100755 index 0000000000..cf1ced98e0 --- /dev/null +++ b/challenge-115/paulo-custodio/test.pl @@ -0,0 +1,7 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use 5.030; + +require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-116/aaronreidsmith/blog.txt b/challenge-116/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..b790b1478f --- /dev/null +++ b/challenge-116/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-116/ diff --git a/challenge-116/aaronreidsmith/raku/ch-1.raku b/challenge-116/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..3812c95eb2 --- /dev/null +++ b/challenge-116/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,55 @@ +#!/usr/bin/env raku + +# Raku adaptation of Python's more-itertools.partitions: https://git.io/JZL8Q +sub partitions(Str $S) { + my @sequence = $S.comb; + my $n = @sequence.elems; + my @partitions = gather for (1..^$n).combinations -> @combination { + my @partition = gather for (0, |@combination) Z (|@combination, $n) -> ($i, $j) { + take [@sequence[$i..^$j]]; + } + take @partition; + } + gather for @partitions -> @partition { + # Filter out elements with leading zeros + my @invalid = @partition.grep(*.head eq '0'); + if @invalid.elems == 0 { + take @partition.map(*.join.Int); + } + } +} + +sub challenge(Int $N where $N >= 10) returns Str { + my $S = $N.Str; + my $solution = partitions($S).first: -> @partition { + my @zipped = @partition[0..*-1] Z @partition[1..*]; + my @filtered = @zipped.grep(-> ($a, $b) { $b - $a == 1 }); + @zipped.elems > 0 && @zipped.elems == @filtered.elems; + } + + with $solution { + $solution.join(','); + } else { + $S + } +} + +multi sub MAIN(Int $N) { + say challenge($N); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + (1234, '1,2,3,4'), + (91011, '9,10,11'), + (10203, '10203') + ); + + for @tests -> ($N, $expected) { + is(c |
