diff options
| -rw-r--r-- | challenge-219/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-219/paulo-custodio/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-219/paulo-custodio/perl/ch-2.pl | 81 | ||||
| -rw-r--r-- | challenge-219/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-219/paulo-custodio/t/test-2.yaml | 10 | ||||
| -rw-r--r-- | challenge-220/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-220/paulo-custodio/perl/ch-1.pl | 44 | ||||
| -rw-r--r-- | challenge-220/paulo-custodio/perl/ch-2.pl | 54 | ||||
| -rw-r--r-- | challenge-220/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-220/paulo-custodio/t/test-2.yaml | 10 |
10 files changed, 247 insertions, 0 deletions
diff --git a/challenge-219/paulo-custodio/Makefile b/challenge-219/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-219/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-219/paulo-custodio/perl/ch-1.pl b/challenge-219/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..209cfced68 --- /dev/null +++ b/challenge-219/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +# Challenge 219 +# +# Task 1: Sorted Squares +# Submitted by: Mohammad S Anwar +# +# You are given a list of numbers. +# +# Write a script to square each number in the list and return the sorted list, +# increasing order. +# Example 1 +# +# Input: @list = (-2, -1, 0, 3, 4) +# Output: (0, 1, 4, 9, 16) +# +# Example 2 +# +# Input: @list = (5, -4, -1, 3, 6) +# Output: (1, 9, 16, 25, 36) + +use Modern::Perl; + +say join " ", sort {$a<=>$b} map {$_*$_} @ARGV; diff --git a/challenge-219/paulo-custodio/perl/ch-2.pl b/challenge-219/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..3b43939b30 --- /dev/null +++ b/challenge-219/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,81 @@ +#!/usr/bin/env perl + +# Challenge 219 +# +# Task 2: Travel Expenditure +# Submitted by: Mohammad S Anwar +# +# You are given two list, @costs and @days. +# +# The list @costs contains the cost of three different types of travel cards +# you can buy. +# +# For example @costs = (5, 30, 90) +# +# Index 0 element represent the cost of 1 day travel card. +# Index 1 element represent the cost of 7 days travel card. +# Index 2 element represent the cost of 30 days travel card. +# +# The list @days contains the day number you want to travel in the year. +# +# For example: @days = (1, 3, 4, 5, 6) +# +# The above example means you want to travel on day 1, day 3, day 4, day 5 and +# day 6 of the year. +# +# Write a script to find the minimum travel cost. +# Example 1: +# +# Input: @costs = (2, 7, 25) +# @days = (1, 5, 6, 7, 9, 15) +# Output: 11 +# +# On day 1, we buy a one day pass for 2 which would cover the day 1. +# On day 5, we buy seven days pass for 7 which would cover days 5 - 9. +# On day 15, we buy a one day pass for 2 which would cover the day 15. +# +# So the total cost is 2 + 7 + 2 => 11. +# +# Example 2: +# +# Input: @costs = (2, 7, 25) +# @days = (1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31) +# Output: 20 +# +# On day 1, we buy a seven days pass for 7 which would cover days 1 - 7. +# On day 10, we buy a seven days pass for 7 which would cover days 10 - 14. +# On day 20, we buy a one day pass for 2 which would cover day 20. +# On day 30, we buy a one day pass for 2 which would cover day 30. +# On day 31, we buy a one day pass for 2 which would cover day 31. +# +# So the total cost is 7 + 7 + 2 + 2 + 2 => 20. + +use Modern::Perl; +use List::Util 'min'; + +@ARGV>=4 or die "Usage:$0 cost1 cost7 cost30 days...\n"; +my($cost1, $cost7, $cost30, @days) = @ARGV; +my $min_cost = calc_min_cost(0, $cost1, $cost7, $cost30, @days); +say $min_cost; + +sub calc_min_cost { + my($total, $cost1, $cost7, $cost30, @days) = @_; + + return $total if @days==0; + + my $total1 = calc_min_cost($total + $cost1, $cost1, $cost7, $cost30, trim_days(1, @days)); + my $total7 = calc_min_cost($total + $cost7, $cost1, $cost7, $cost30, trim_days(7, @days)); + my $total30 = calc_min_cost($total + $cost30, $cost1, $cost7, $cost30, trim_days(30, @days)); + + return min($total1, $total7, $total30); +} + +sub trim_days { + my($num_days, @days) = @_; + die unless @days; + my $day1 = $days[0]; + while (@days && $days[0] < $day1+$num_days) { + shift @days; + } + return @days; +} diff --git a/challenge-219/paulo-custodio/t/test-1.yaml b/challenge-219/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..04ef737aa7 --- /dev/null +++ b/challenge-219/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: -2 -1 0 3 4 + input: + output: 0 1 4 9 16 +- setup: + cleanup: + args: 5 -4 -1 3 6 + input: + output: 1 9 16 25 36 diff --git a/challenge-219/paulo-custodio/t/test-2.yaml b/challenge-219/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..672ad744e0 --- /dev/null +++ b/challenge-219/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 2 7 25 1 5 6 7 9 15 + input: + output: 11 +- setup: + cleanup: + args: 2 7 25 1 2 3 5 7 10 11 12 14 20 30 31 + input: + output: 20 diff --git a/challenge-220/paulo-custodio/Makefile b/challenge-220/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-220/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-220/paulo-custodio/perl/ch-1.pl b/challenge-220/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..1b997ef403 --- /dev/null +++ b/challenge-220/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +# Challenge 220 +# +# Task 1: Common Characters +# Submitted by: Mohammad S Anwar +# +# You are given a list of words. +# +# Write a script to return the list of common characters (sorted alphabeticall) +# found in every word of the given list. +# Example 1 +# +# Input: @words = ("Perl", "Rust", "Raku") +# Output: ("r") +# +# Example 2 +# +# Input: @words = ("love", "live", "leave") +# Output: ("e", "l", "v") + +use Modern::Perl; + +my @words = @ARGV; +my @letters; +my %chars; +for (0..$#words) { + for my $ch (split //, lc($words[$_])) { + $letters[$_]{$ch} = 1; + $chars{$ch} = 1; + } +} + +my @result; + +for my $ch (sort keys %chars) { + my $count = 0; + for (0..$#words) { + $count++ if $letters[$_]{$ch}; + } + push @result, $ch if $count == @words; +} + +say "@result"; diff --git a/challenge-220/paulo-custodio/perl/ch-2.pl b/challenge-220/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..c08a0993b7 --- /dev/null +++ b/challenge-220/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +# Challenge 220 +# +# Task 2: Squareful +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers, @ints. +# +# An array is squareful if the sum of every pair of adjacent elements is +# a perfect square. +# +# Write a script to find all the permutations of the given array that are squareful. +# Example 1: +# +# Input: @ints = (1, 17, 8) +# Output: (1, 8, 17), (17, 8, 1) +# +# (1, 8, 17) since 1 + 8 => 9, a perfect square and also 8 + 17 => 25 is perfect square too. +# (17, 8, 1) since 17 + 8 => 25, a perfect square and also 8 + 1 => 9 is perfect square too. +# +# Example 2: +# +# Input: @ints = (2, 2, 2) +# Output: (2, 2, 2) +# +# There is only one permutation possible. + +use Modern::Perl; +use Math::Combinatorics; + +my @ints = @ARGV; + +my $combinat = Math::Combinatorics->new(count => scalar(@ints), data => [@ints]); +my @result; +my %found; + +while(my @permu = $combinat->next_permutation) { + next if $found{"@permu"}++; + next unless is_squareful(@permu); + push @result, \@permu; +} + +say join(", ", sort map {"(" . join(", ", @$_) . ")"} @result); + +sub is_squareful { + my(@ints) = @_; + for (0..$#ints-1) { + my $sum = $ints[$_]+$ints[$_+1]; + my $sq = sqrt($sum); + return 0 unless int($sq) == $sq; + } + return 1; +} diff --git a/challenge-220/paulo-custodio/t/test-1.yaml b/challenge-220/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..ab4e64127d --- /dev/null +++ b/challenge-220/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: Perl Rust Raku + input: + output: r +- setup: + cleanup: + args: love live leave + input: + output: e l v diff --git a/challenge-220/paulo-custodio/t/test-2.yaml b/challenge-220/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..0a368b5fe3 --- /dev/null +++ b/challenge-220/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 1 17 8 + input: + output: (1, 8, 17), (17, 8, 1) +- setup: + cleanup: + args: 2 2 2 + input: + output: (2, 2, 2) |
