diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-30 20:31:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-30 20:31:45 +0100 |
| commit | e2ffc94d311bbf053dbbfc5ce984170152b66589 (patch) | |
| tree | e7d01daae042d70f25ad42409799e54825f29480 | |
| parent | 9d8b35adb9460155304b6fbaf2b2480c6c7fe723 (diff) | |
| parent | e5b5d21abbae5d4dd147ec2bf9d9cec96457c744 (diff) | |
| download | perlweeklychallenge-club-e2ffc94d311bbf053dbbfc5ce984170152b66589.tar.gz perlweeklychallenge-club-e2ffc94d311bbf053dbbfc5ce984170152b66589.tar.bz2 perlweeklychallenge-club-e2ffc94d311bbf053dbbfc5ce984170152b66589.zip | |
Merge pull request #10728 from pauloscustodio/master
Add Perl solutions
30 files changed, 814 insertions, 0 deletions
diff --git a/challenge-270/paulo-custodio/Makefile b/challenge-270/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-270/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-270/paulo-custodio/perl/ch-1.pl b/challenge-270/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..a143b72356 --- /dev/null +++ b/challenge-270/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl + +# Challenge 270 +# +# Task 1: Special Positions +# Submitted by: Mohammad Sajid Anwar +# You are given a m x n binary matrix. +# +# Write a script to return the number of special positions in the given binary matrix. +# +# A position (i, j) is called special if $matrix[i][j] == 1 and all other +# elements in the row i and column j are 0. +# +# Example 1 +# Input: $matrix = [ [1, 0, 0], +# [0, 0, 1], +# [1, 0, 0], +# ] +# Output: 1 +# +# There is only one special position (1, 2) as $matrix[1][2] == 1 +# and all other elements in row 1 and column 2 are 0. +# Example 2 +# Input: $matrix = [ [1, 0, 0], +# [0, 1, 0], +# [0, 0, 1], +# ] +# Output: 3 +# +# Special positions are (0,0), (1, 1) and (2,2). + +use Modern::Perl; + +my @matrix = split /,/, "@ARGV"; +@matrix = map {[split ' ', $_]} @matrix; + +say count_special_pos(@matrix); + +sub count_special_pos { + my(@matrix) = @_; + my $special = 0; + for my $i (0 .. $#matrix) { + for my $j (0 .. $#{$matrix[0]}) { + $special++ if is_special_pos($i, $j, @matrix); + } + } + return $special; +} + +sub is_special_pos { + my($i, $j, @matrix) = @_; + return 0 if $matrix[$i][$j] != 1; + for my $ii (0 .. $#matrix) { + if ($ii != $i) { + return 0 if $matrix[$ii][$j] != 0; + } + } + for my $jj (0 .. $#{$matrix[0]}) { + if ($jj != $j) { + return 0 if $matrix[$i][$jj] != 0; + } + } + return 1; +} diff --git a/challenge-270/paulo-custodio/perl/ch-2.pl b/challenge-270/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..4e31dda4c6 --- /dev/null +++ b/challenge-270/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,90 @@ +#!/usr/bin/env perl + +# Challenge 270 +# +# Task 2: Equalize Array +# Submitted by: Mohammad Sajid Anwar +# You are give an array of integers, @ints and two integers, $x and $y. +# +# Write a script to execute one of the two options: +# +# Level 1: +# Pick an index i of the given array and do $ints[i] += 1 +# +# Level 2: +# Pick two different indices i,j and do $ints[i] +=1 and $ints[j] += 1. +# You are allowed to perform as many levels as you want to make every elements +# in the given array equal. There is cost attach for each level, for Level 1, +# the cost is $x and $y for Level 2. +# +# In the end return the minimum cost to get the work done. +# +# Example 1 +# Input: @ints = (4, 1), $x = 3 and $y = 2 +# Output: 9 +# +# Level 1: i=1, so $ints[1] += 1. +# @ints = (4, 2) +# +# Level 1: i=1, so $ints[1] += 1. +# @ints = (4, 3) +# +# Level 1: i=1, so $ints[1] += 1. +# @ints = (4, 4) +# +# We perforned operation Level 1, 3 times. +# So the total cost would be 3 x $x => 3 x 3 => 9 +# Example 2 +# Input: @ints = (2, 3, 3, 3, 5), $x = 2 and $y = 1 +# Output: 6 +# +# Level 2: i=0, j=1, so $ints[0] += 1 and $ints[1] += 1 +# @ints = (3, 4, 3, 3, 5) +# +# Level 2: i=0, j=2, so $ints[0] += 1 and $ints[2] += 1 +# @ints = (4, 4, 4, 3, 5) +# +# Level 2: i=0, j=3, so $ints[0] += 1 and $ints[3] += 1 +# @ints = (5, 4, 4, 4, 5) +# +# Level 2: i=1, j=2, so $ints[1] += 1 and $ints[2] += 1 +# @ints = (5, 5, 5, 4, 5) +# +# Level 1: i=3, so $ints[3] += 1 +# @ints = (5, 5, 5, 5, 5) +# +# We perforned operation Level 1, 1 time and Level 2, 4 times. +# So the total cost would be (1 x $x) + (4 x $y) => (1 x 2) + (4 x 1) => 6 + +use Modern::Perl; + +my($x, $y, @ints) = @ARGV; + +say equalize($x, $y, @ints); + +sub equalize { + my($x, $y, @ints) = @_; + + my $cost = 0; + @ints = sort {$a <=> $b} @ints; + return $cost if !@ints; + my $max = $ints[-1]; + + while (@ints) { + @ints = sort {$a <=> $b} @ints; + @ints = grep {$_ != $max} @ints; + last if !@ints; + + if (@ints == 1) { + $ints[0]++; + $cost += $x; + } + else { + $ints[0]++; + $ints[1]++; + $cost += $y; + } + } + + return $cost; +} diff --git a/challenge-270/paulo-custodio/t/test-1.yaml b/challenge-270/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..f7b1a39e9a --- /dev/null +++ b/challenge-270/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 1 0 0 , 0 0 1 , 1 0 0 + input: + output: 1 +- setup: + cleanup: + args: 1 0 0 , 0 1 0 , 0 0 1 + input: + output: 3 diff --git a/challenge-270/paulo-custodio/t/test-2.yaml b/challenge-270/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..ba06b2f9ec --- /dev/null +++ b/challenge-270/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 3 2 4 1 + input: + output: 9 +- setup: + cleanup: + args: 2 1 2 3 3 3 5 + input: + output: 6 diff --git a/challenge-271/paulo-custodio/Makefile b/challenge-271/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-271/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-271/paulo-custodio/perl/ch-1.pl b/challenge-271/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..a724f3a898 --- /dev/null +++ b/challenge-271/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +# Challenge 271 +# +# Task 1: Maximum Ones +# Submitted by: Mohammad Sajid Anwar +# You are given a m x n binary matrix. +# +# Write a script to return the row number containing maximum ones, in case of +# more than one rows then return smallest row number. +# +# Example 1 +# Input: $matrix = [ [0, 1], +# [1, 0], +# ] +# Output: 1 +# +# Row 1 and Row 2 have the same number of ones, so return row 1. +# Example 2 +# Input: $matrix = [ [0, 0, 0], +# [1, 0, 1], +# ] +# Output: 2 +# +# Row 2 has the maximum ones, so return row 2. +# Example 3 +# Input: $matrix = [ [0, 0], +# [1, 1], +# [0, 0], +# ] +# Output: 2 +# +# Row 2 have the maximum ones, so return row 2. + +use Modern::Perl; +use List::Util 'max'; + +my @matrix = split /,/, "@ARGV"; +@matrix = map {[split ' ', $_]} @matrix; + +say 1+max_ones(@matrix); + +sub max_ones { + my(@matrix) = @_; + my @ones = map {tr/1/1/} map {join '', @$_} @matrix; + my $max = max(@ones); + for (0 .. $#ones) { + return $_ if $ones[$_] == $max; + } + return 0; +} diff --git a/challenge-271/paulo-custodio/perl/ch-2.pl b/challenge-271/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b442ed791c --- /dev/null +++ b/challenge-271/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +# Challenge 271 +# +# Task 2: Sort by 1 bits +# Submitted by: Mohammad Sajid Anwar +# You are give an array of integers, @ints. +# +# Write a script to sort the integers in ascending order by the number of 1 bits +# in their binary representation. In case more than one integers have the same +# number of 1 bits then sort them in ascending order. +# +# Example 1 +# Input: @ints = (0, 1, 2, 3, 4, 5, 6, 7, 8) +# Output: (0, 1, 2, 4, 8, 3, 5, 6, 7) +# +# 0 = 0 one bits +# 1 = 1 one bits +# 2 = 1 one bits +# 4 = 1 one bits +# 8 = 1 one bits +# 3 = 2 one bits +# 5 = 2 one bits +# 6 = 2 one bits +# 7 = 3 one bits +# Example 2 +# Input: @ints = (1024, 512, 256, 128, 64) +# Output: (64, 128, 256, 512, 1024) +# +# All integers in the given array have one 1-bits, so just sort them in +# ascending order. + +use Modern::Perl; + +my @n = @ARGV; + +say join ' ', + map {$_->[0]} + sort {$a->[1] != $b->[1] ? $a->[1] <=> $b->[1] : $a->[0] <=> $b->[0]} + map {[$n[$_], sprintf("%b", $n[$_]) =~ tr/1/1/]} 0 .. $#n; diff --git a/challenge-271/paulo-custodio/t/test-1.yaml b/challenge-271/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..aa277fd3e8 --- /dev/null +++ b/challenge-271/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 0 1 , 1 0 + input: + output: 1 +- setup: + cleanup: + args: 0 0 0 , 1 0 1 + input: + output: 2 +- setup: + cleanup: + args: 0 0 , 1 1 , 0 0 + input: + output: 2 diff --git a/challenge-271/paulo-custodio/t/test-2.yaml b/challenge-271/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..248d4ffc6b --- /dev/null +++ b/challenge-271/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 0 1 2 3 4 5 6 7 8 + input: + output: 0 1 2 4 8 3 5 6 7 +- setup: + cleanup: + args: 1024 512 256 128 64 + input: + output: 64 128 256 512 1024 diff --git a/challenge-272/paulo-custodio/Makefile b/challenge-272/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-272/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-272/paulo-custodio/perl/ch-1.pl b/challenge-272/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..03ab92a468 --- /dev/null +++ b/challenge-272/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl + +# Challenge 272 +# +# Task 1: Defang IP Address +# Submitted by: Mohammad Sajid Anwar +# You are given a valid IPv4 address. +# +# Write a script to return the defanged version of the given IP address. +# +# A defanged IP address replaces every period “.” with “[.]". +# +# Example 1 +# Input: $ip = "1.1.1.1" +# Output: "1[.]1[.]1[.]1" +# Example 2 +# Input: $ip = "255.101.1.0" +# Output: "255[.]101[.]1[.]0" + +use Modern::Perl; + +$_ = shift // ""; +say s/\./[.]/gr; diff --git a/challenge-272/paulo-custodio/perl/ch-2.pl b/challenge-272/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..8c000da0c5 --- /dev/null +++ b/challenge-272/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl + +# Challenge 272 +# +# Task 2: String Score +# Submitted by: Mohammad Sajid Anwar +# You are given a string, $str. +# +# Write a script to return the score of the given string. +# +# The score of a string is defined as the sum of the absolute difference between +# the ASCII values of adjacent characters. +# +# Example 1 +# Input: $str = "hello" +# Output: 13 +# +# ASCII values of characters: +# h = 104 +# e = 101 +# l = 108 +# l = 108 +# o = 111 +# +# Score => |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| +# => 3 + 7 + 0 + 3 +# => 13 +# Example 2 +# Input: "perl" +# Output: 30 +# +# ASCII values of characters: +# p = 112 +# e = 101 +# r = 114 +# l = 108 +# +# Score => |112 - 101| + |101 - 114| + |114 - 108| +# => 11 + 13 + 6 +# => 30 +# Example 3 +# Input: "raku" +# Output: 37 +# +# ASCII values of characters: +# r = 114 +# a = 97 +# k = 107 +# u = 117 +# +# Score => |114 - 97| + |97 - 107| + |107 - 117| +# => 17 + 10 + 10 +# => 37 + +use Modern::Perl; + +say score(shift // ""); + +sub score { + my($str) = @_; + my @chars = map {ord} split //, $str; + my $score = 0; + for (0 .. $#chars-1) { + $score += abs($chars[$_+1] - $chars[$_]); + } + return $score; +} diff --git a/challenge-272/paulo-custodio/t/test-1.yaml b/challenge-272/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..b8a8df42e4 --- /dev/null +++ b/challenge-272/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 1.1.1.1 + input: + output: 1[.]1[.]1[.]1 +- setup: + cleanup: + args: 255.101.1.0 + input: + output: 255[.]101[.]1[.]0 diff --git a/challenge-272/paulo-custodio/t/test-2.yaml b/challenge-272/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..a776b62625 --- /dev/null +++ b/challenge-272/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: hello + input: + output: 13 +- setup: + cleanup: + args: perl + input: + output: 30 +- setup: + cleanup: + args: raku + input: + output: 37 diff --git a/challenge-273/paulo-custodio/Makefile b/challenge-273/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-273/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-273/paulo-custodio/perl/ch-1.pl b/challenge-273/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..c46e80c366 --- /dev/null +++ b/challenge-273/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +# Challenge 273 +# +# Task 1: Percentage of Character +# Submitted by: Mohammad Sajid Anwar +# You are given a string, $str and a character $char. +# +# Write a script to return the percentage, nearest whole, of given character in +# the given string. +# +# Example 1 +# Input: $str = "perl", $char = "e" +# Output: 25 +# Example 2 +# Input: $str = "java", $char = "a" +# Output: 50 +# Example 3 +# Input: $str = "python", $char = "m" +# Output: 0 +# Example 4 +# Input: $str = "ada", $char = "a" +# Output: 67 +# Example 5 +# Input: $str = "ballerina", $char = "l" +# Output: 22 +# Example 6 +# Input: $str = "analitik", $char = "k" +# Output: 13 + +use Modern::Perl; + +say percent_char(@ARGV); + +sub percent_char { + my($str, $ch) = @_; + my $percent = 100 * ($str =~ s/$ch/$ch/g) / length($str); + return int($percent+0.5); +} diff --git a/challenge-273/paulo-custodio/perl/ch-2.pl b/challenge-273/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..a21c7db279 --- /dev/null +++ b/challenge-273/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +# Challenge 273 +# +# Task 2: B After A +# Submitted by: Mohammad Sajid Anwar +# You are given a string, $str. +# +# Write a script to return true if there is at least one b, and no a appears +# after the first b. +# +# Example 1 +# Input: $str = "aabb" +# Output: true +# Example 2 +# Input: $str = "abab" +# Output: false +# Example 3 +# Input: $str = "aaa" +# Output: false +# Example 4 +# Input: $str = "bbb" +# Output: true + +use Modern::Perl; + +$_ = shift // ""; +say /b/ && !/^[^b]*b.*?a/ ? 'true' : 'false'; diff --git a/challenge-273/paulo-custodio/t/test-1.yaml b/challenge-273/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..756e9f80b7 --- /dev/null +++ b/challenge-273/paulo-custodio/t/test-1.yaml @@ -0,0 +1,30 @@ +- setup: + cleanup: + args: perl e + input: + output: 25 +- setup: + cleanup: + args: java a + input: + output: 50 +- setup: + cleanup: + args: python m + input: + output: 0 +- setup: + cleanup: + args: ada a + input: + output: 67 +- setup: + cleanup: + args: ballerina l + input: + output: 22 +- setup: + cleanup: + args: analitik k + input: + output: 13 diff --git a/challenge-273/paulo-custodio/t/test-2.yaml b/challenge-273/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..a114fccb37 --- /dev/null +++ b/challenge-273/paulo-custodio/t/test-2.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: aabb + input: + output: true +- setup: + cleanup: + args: abab + input: + output: false +- setup: + cleanup: + args: aaa + input: + output: false +- setup: + cleanup: + args: bbb + input: + output: true diff --git a/challenge-274/paulo-custodio/Makefile b/challenge-274/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-274/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-274/paulo-custodio/perl/ch-1.pl b/challenge-274/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..a3b1d3d83b --- /dev/null +++ b/challenge-274/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +# Challenge 274 +# +# Task 1: Goat Latin +# Submitted by: Mohammad Sajid Anwar +# You are given a sentence, $sentence. +# +# Write a script to convert the given sentence to Goat Latin, a made up language +# similar to Pig Latin. +# +# Rules for Goat Latin: +# +# 1) If a word begins with a vowel ("a", "e", "i", "o", "u"), append +# "ma" to the end of the word. +# 2) If a word begins with consonant i.e. not a vowel, remove first +# letter and append it to the end then add "ma". +# 3) Add letter "a" to the end of first word in the sentence, "aa" to +# the second word, etc etc. +# Example 1 +# Input: $sentence = "I love Perl" +# Output: "Imaa ovelmaaa erlPmaaaa" +# Example 2 +# Input: $sentence = "Perl and Raku are friends" +# Output: "erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa" +# Example 3 +# Input: $sentence = "The Weekly Challenge" +# Output: "heTmaa eeklyWmaaa hallengeCmaaaa" + +use Modern::Perl; + +say join ' ', map{goat_latin($ARGV[$_], $_)} 0 .. $#ARGV; + +sub goat_latin { + my($word, $i) = @_; + if ($word =~ /^[aeiou]/i) { + $word .= "ma"; + } + else { + my $ch = substr($word,0,1); + $word = substr($word,1).$ch."ma"; + } + $word .= "a" x ($i+1); + return $word; +} diff --git a/challenge-274/paulo-custodio/perl/ch-2.pl b/challenge-274/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..ad99e7c4e1 --- /dev/null +++ b/challenge-274/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,83 @@ +#!/usr/bin/env perl + +# Challenge 274 +# +# Task 2: Bus Route +# Submitted by: Peter Campbell Smith +# Several bus routes start from a bus stop near my home, and go to the same stop +# in town. They each run to a set timetable, but they take different times to +# get into town. +# +# Write a script to find the times - if any - I should let one bus leave and +# catch a strictly later one in order to get into town strictly sooner. +# +# An input timetable consists of the service interval, the offset within the +# hour, and the duration of the trip. +# +# Example 1 +# Input: [ [12, 11, 41], [15, 5, 35] ] +# Output: [36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47] +# +# Route 1 leaves every 12 minutes, starting at 11 minutes past the hour +# (so 11, 23, ...) and takes 41 minutes. Route 2 leaves every 15 minutes, +# starting at 5 minutes past (5, 20, ...) and takes 35 minutes. +# +# At 45 minutes past the hour I could take the route 1 bus at 47 past the hour, +# arriving at 28 minutes past the following hour, but if I wait for the +# route 2 bus at 50 past I will get to town sooner, at 25 minutes past the +# next hour. +# Example 2 +# Input: [ [12, 3, 41], [15, 9, 35], [30, 5, 25] ] +# Output: [ 0, 1, 2, 3, 25, 26, 27, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 55, 56, 57, 58, 59 ] + +use Modern::Perl; + +my @routes = map {[split ' ', $_]} split /,/, "@ARGV"; + +say join ' ', skip_minutes(@routes); + +sub skip_minutes { + my(@routes) = @_; + my @buses = calc_buses(@routes); + my @out; + for (0..59) { + push @out, $_ if skip_sooner($_, @buses); + } + return @out; +} + +# build sorted list of [start-time,end-time] for all routes +sub calc_buses { + my(@routes) = @_; + my @buses; + for (@routes) { + my($interval, $start, $duration) = @$_; + for (my $time = $start; $time < 120; $time += $interval) { + push @buses, [$time, $time+$duration]; + } + } + return sort {$a->[0] <=> $b->[0]} @buses; +} + +# skip bus at this minute to arrive sooner +sub skip_sooner { + my($minute, @buses) = @_; + + # get first bus that matches + my($start_time, $end_time); + for (@buses) { + my($bus_start, $bus_end) = @$_; + if ($bus_start >= $minute) { + ($start_time, $end_time) = ($bus_start, $bus_end); + last; + } + } + + # check if a later bus arrives sooner + for (@buses) { + my($bus_start, $bus_end) = @$_; + return 1 if $bus_start > $start_time && $bus_end < $end_time; + } + + return 0; +} diff --git a/challenge-274/paulo-custodio/t/test-1.yaml b/challenge-274/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..0a5b326ddf --- /dev/null +++ b/challenge-274/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: I love Perl + input: + output: Imaa ovelmaaa erlPmaaaa +- setup: + cleanup: + args: Perl and Raku are friends + input: + output: erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa +- setup: + cleanup: + args: The Weekly Challenge + input: + output: heTmaa eeklyWmaaa hallengeCmaaaa diff --git a/challenge-274/paulo-custodio/t/test-2.yaml b/challenge-274/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..af899fe156 --- /dev/null +++ b/challenge-274/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 12 11 41 , 15 5 35 + input: + output: 36 37 38 39 40 41 42 43 44 45 46 47 +- setup: + cleanup: + args: 12 3 41 , 15 9 35 , 30 5 25 + input: + output: 0 1 2 3 25 26 27 40 41 42 43 44 45 46 47 48 49 50 51 55 56 57 58 59 diff --git a/challenge-275/paulo-custodio/Makefile b/challenge-275/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-275/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-275/paulo-custodio/perl/ch-1.pl b/challenge-275/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..b2180af349 --- /dev/null +++ b/challenge-275/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +# Challenge 275 +# +# Task 1: Broken Keys +# Submitted by: Mohammad Sajid Anwar +# You are given a sentence, $sentence and list of broken keys @keys. +# +# Write a script to find out how many words can be typed fully. +# +# Example 1 +# Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a') +# Output: 0 +# Example 2 +# Input: $sentence = "Perl and Raku", @keys = ('a') +# Output: 1 +# +# Only Perl since the other word two words contain 'a' and can't be typed fully. +# Example 3 +# Input: $sentence = "Well done Team PWC", @keys = ('l', 'o') +# Output: 2 +# Example 4 +# Input: $sentence = "The joys of polyglottism", @keys = ('T') +# Output: 2 + +use Modern::Perl; + +my($sentence, $keys) = split /,/, "@ARGV"; +my @words = split ' ', $sentence; +my @keys = split ' ', $keys; + +say scalar grep {can_type($_, @keys)} @words; + +sub can_type { + my($word, @keys) = @_; + for my $ch (@keys) { + return 0 if $word =~ /$ch/i; + } + return 1; +} |
