From be9df65194dde74bb7f0d9736d0336b47430bcbd Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 26 Aug 2024 10:13:29 +0100 Subject: Whitespace --- challenge-248/paulo-custodio/perl/ch-1.pl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/challenge-248/paulo-custodio/perl/ch-1.pl b/challenge-248/paulo-custodio/perl/ch-1.pl index 503126e926..b99dce5818 100644 --- a/challenge-248/paulo-custodio/perl/ch-1.pl +++ b/challenge-248/paulo-custodio/perl/ch-1.pl @@ -7,7 +7,8 @@ # # You are given a string and a character in the given string. # -# Write a script to return an array of integers of size same as length of the given string such that: +# Write a script to return an array of integers of size same as length of +# the given string such that: # # distance[i] is the distance from index i to the closest occurence of # the given character in the given string. -- cgit From 7e7689b2bc6ea77f92d427c91e194caebe1d10a7 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 26 Aug 2024 10:51:57 +0100 Subject: Add Perl solution to challenge 284 --- challenge-248/paulo-custodio/perl/ch-1.pl | 2 +- challenge-284/paulo-custodio/Makefile | 2 ++ challenge-284/paulo-custodio/README | 1 + challenge-284/paulo-custodio/perl/ch-1.pl | 39 +++++++++++++++++++++++++ challenge-284/paulo-custodio/perl/ch-2.pl | 46 ++++++++++++++++++++++++++++++ challenge-284/paulo-custodio/t/test-1.yaml | 15 ++++++++++ challenge-284/paulo-custodio/t/test-2.yaml | 15 ++++++++++ 7 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 challenge-284/paulo-custodio/Makefile create mode 100644 challenge-284/paulo-custodio/README create mode 100644 challenge-284/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-284/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-284/paulo-custodio/t/test-1.yaml create mode 100644 challenge-284/paulo-custodio/t/test-2.yaml diff --git a/challenge-248/paulo-custodio/perl/ch-1.pl b/challenge-248/paulo-custodio/perl/ch-1.pl index b99dce5818..021e65777a 100644 --- a/challenge-248/paulo-custodio/perl/ch-1.pl +++ b/challenge-248/paulo-custodio/perl/ch-1.pl @@ -7,7 +7,7 @@ # # You are given a string and a character in the given string. # -# Write a script to return an array of integers of size same as length of +# Write a script to return an array of integers of size same as length of # the given string such that: # # distance[i] is the distance from index i to the closest occurence of diff --git a/challenge-284/paulo-custodio/Makefile b/challenge-284/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-284/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-284/paulo-custodio/README b/challenge-284/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-284/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-284/paulo-custodio/perl/ch-1.pl b/challenge-284/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..09f0564364 --- /dev/null +++ b/challenge-284/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +# Challenge 284 +# +# Task 1: Lucky Integer +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# +# Write a script to find the lucky integer if found otherwise return -1. If +# there are more than one then return the largest. +# +# A lucky integer is an integer that has a frequency in the array equal to +# its value. +# +# Example 1 +# +# Input: @ints = (2, 2, 3, 4) +# Output: 2 +# +# Example 2 +# +# Input: @ints = (1, 2, 2, 3, 3, 3) +# Output: 3 +# +# Example 3 +# +# Input: @ints = (1, 1, 1, 3) +# Output: -1 + +use Modern::Perl; + +my @ints = @ARGV; +my %count; +$count{$_}++ for @ints; +my @lucky = + sort {$b <=> $a} + grep {$_ == $count{$_}} @ints; +say !@lucky ? -1 : $lucky[0]; diff --git a/challenge-284/paulo-custodio/perl/ch-2.pl b/challenge-284/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..6bb9662a88 --- /dev/null +++ b/challenge-284/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +# Challenge 284 +# +# Task 2: Relative Sort +# Submitted by: Mohammad Sajid Anwar +# +# You are given two list of integers, @list1 and @list2. The elements in the +# @list2 are distinct and also in the @list1. +# +# Write a script to sort the elements in the @list1 such that the relative +# order of items in @list1 is same as in the @list2. Elements that is missing +# in @list2 should be placed at the end of @list1 in ascending order. +# Example 1 +# +# Input: @list1 = (2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5) +# @list2 = (2, 1, 4, 3, 5, 6) +# Ouput: (2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9) +# +# Example 2 +# +# Input: @list1 = (3, 3, 4, 6, 2, 4, 2, 1, 3) +# @list2 = (1, 3, 2) +# Ouput: (1, 3, 3, 3, 2, 2, 4, 4, 6) +# +# Example 3 +# +# Input: @list1 = (3, 0, 5, 0, 2, 1, 4, 1, 1) +# @list2 = (1, 0, 3, 2) +# Ouput: (1, 1, 1, 0, 0, 3, 2, 4, 5) + +use Modern::Perl; +my $input = "@ARGV"; +my($list1, $list2) = split /,/, $input; +my @list1 = split ' ', $list1; +my @list2 = split ' ', $list2; + +my @output; +while (@list2) { + my $n = shift @list2; + push @output, grep {$_ == $n} @list1; + @list1 = grep {$_ != $n} @list1; +} +push @output, sort {$a <=> $b} @list1; + +say "@output"; diff --git a/challenge-284/paulo-custodio/t/test-1.yaml b/challenge-284/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..5feba9c238 --- /dev/null +++ b/challenge-284/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 2 2 3 4 + input: + output: 2 +- setup: + cleanup: + args: 1 2 2 3 3 3 + input: + output: 3 +- setup: + cleanup: + args: 1 1 1 3 + input: + output: -1 diff --git a/challenge-284/paulo-custodio/t/test-2.yaml b/challenge-284/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..f4d2db7f63 --- /dev/null +++ b/challenge-284/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 2 3 9 3 1 4 6 7 2 8 5 , 2 1 4 3 5 6 + input: + output: 2 2 1 4 3 3 5 6 7 8 9 +- setup: + cleanup: + args: 3 3 4 6 2 4 2 1 3 , 1 3 2 + input: + output: 1 3 3 3 2 2 4 4 6 +- setup: + cleanup: + args: 3 0 5 0 2 1 4 1 1 , 1 0 3 2 + input: + output: 1 1 1 0 0 3 2 4 5 -- cgit From 707b0c338d30768c28e61631c27e2e29ccc75953 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 26 Aug 2024 13:58:08 +0100 Subject: Add Perl solution to challenge 259 --- challenge-259/paulo-custodio/Makefile | 2 + challenge-259/paulo-custodio/perl/ch-1.pl | 62 ++++++++++++++++ challenge-259/paulo-custodio/perl/ch-2.pl | 115 +++++++++++++++++++++++++++++ challenge-259/paulo-custodio/t/test-1.yaml | 10 +++ challenge-259/paulo-custodio/t/test-2.yaml | 23 ++++++ 5 files changed, 212 insertions(+) create mode 100644 challenge-259/paulo-custodio/Makefile create mode 100644 challenge-259/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-259/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-259/paulo-custodio/t/test-1.yaml create mode 100644 challenge-259/paulo-custodio/t/test-2.yaml diff --git a/challenge-259/paulo-custodio/Makefile b/challenge-259/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-259/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-259/paulo-custodio/perl/ch-1.pl b/challenge-259/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..241df88e66 --- /dev/null +++ b/challenge-259/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl + +# Challenge 259 +# +# Task 1: Banking Day Offset +# Submitted by: Lee Johnson +# +# You are given a start date and offset counter. Optionally you also get bank +# holiday date list. +# +# Given a number (of days) and a start date, return the number (of days) +# adjusted to take into account non-banking days. In other words: convert a +# banking day offset to a calendar day offset. +# +# Non-banking days are: +# +# a) Weekends +# b) Bank holidays +# +# Example 1 +# +# Input: $start_date = '2018-06-28', $offset = 3, $bank_holidays = ['2018-07-03'] +# Output: '2018-07-04' +# +# Thursday bumped to Wednesday (3 day offset, with Monday a bank holiday) +# +# Example 2 +# +# Input: $start_date = '2018-06-28', $offset = 3 +# Output: '2018-07-03' + +use Modern::Perl; +use DateTime; + +my $start_date = parse_date(shift @ARGV); +my $offset = shift @ARGV; +my @holidays; +push @holidays, parse_date($_) for @ARGV; + +my $end_date = compute_offset($start_date, $offset, @holidays); +say $end_date->ymd; + +sub compute_offset { + my($start_date, $offset, @holidays) = @_; + my %holidays; $holidays{$_}=1 for @holidays; + + my $date = $start_date; + for (1 .. $offset) { + my $dow; + do { + $date->add(DateTime::Duration->new(days=>1)); + $dow = $date->day_of_week; + } while ($dow==6 || $dow==7 || exists $holidays{$date}); + } + return $date; +} + +sub parse_date { + my($text) = @_; + my($year, $month, $day) = split /-/, $text; + return DateTime->new(year=>$year, month=>$month, day=>$day); +} diff --git a/challenge-259/paulo-custodio/perl/ch-2.pl b/challenge-259/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..46eafd3455 --- /dev/null +++ b/challenge-259/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,115 @@ +#!/usr/bin/env perl + +# Challenge 259 +# +# Task 2: Line Parser +# Submitted by: Gabor Szabo +# +# You are given a line like below: +# +# {% id field1="value1" field2="value2" field3=42 %} +# +# +# Where +# +# a) "id" can be \w+. +# b) There can be 0 or more field-value pairs. +# c) The name of the fields are \w+. +# b) The values are either number in which case we don't need double quotes or +# string in which case we need double quotes around them. +# +# +# The line parser should return structure like below: +# +# { +# name => id, +# fields => { +# field1 => value1, +# field2 => value2, +# field3 => value3, +# } +# } +# +# +# It should be able to parse the following edge cases too: +# +# {% youtube title="Title \"quoted\" done" %} +# +# +# and +# +# {% youtube title="Title with escaped backslash \\" %} +# +# +# BONUS: Extend it to be able to handle multiline tags: +# +# {% id filed1="value1" ... %} +# LINES +# {% endid %} +# +# +# You should expect the following structure from your line parser: +# +# { +# name => id, +# fields => { +# field1 => value1, +# field2 => value2, +# field3 => value3, +# } +# text => LINES +# } + +use Modern::Perl; +use Parse::FSM::Lexer; +use Data::Dump 'dump'; + +my $text = "@ARGV"; +my $data = parse($text); +say dump($data); + +sub parse { + my(@text) = @_; + my $data = {}; + + my $lex = Parse::FSM::Lexer->new; + $lex->from_list($text); + + # start marker + (my $token = $lex->get_token()) or die "start marker missing"; + $token->[0] eq "{" or die "start marker missing, got ", $token->[0]; + ($token = $lex->get_token()) or die "start marker missing"; + $token->[0] eq "%" or die "start marker missing, got ", $token->[0]; + + # name + ($token = $lex->get_token()) or die "name missing"; + $token->[0] eq 'NAME' or die "name expected"; + $data->{name} = $token->[1]; + + # fields + for (;;) { + # field name + ($token = $lex->get_token()) or die "field or end marker missing"; + last if $token->[0] eq '%'; + $token->[0] eq 'NAME' or die "field name expected, got ", $token->[0]; + my $field_name = $token->[1]; + + # = + ($token = $lex->get_token()) or die "'=' expected"; + $token->[0] eq '=' or die "'=' expected, got ", $token->[0]; + + # value + ($token = $lex->get_token()) or die "field value expected"; + ($token->[0] eq 'NUM' || $token->[0] eq 'STR') or die "field value expected, got ", $token->[0]; + my $field_value = $token->[1]; + + $data->{fields}{$field_name} = $field_value; + } + + ($token = $lex->get_token()) or die "end marker missing"; + $token->[0] eq '}' or die "end marker missing, got ", $token->[0]; + + defined($token = $lex->get_token()) and die "extra input, got ", $token->[0]; + + return $data; +} diff --git a/challenge-259/paulo-custodio/t/test-1.yaml b/challenge-259/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..e159d3fdc1 --- /dev/null +++ b/challenge-259/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 2018-06-28 3 2018-07-03 + input: + output: 2018-07-04 +- setup: + cleanup: + args: 2018-06-28 3 + input: + output: 2018-07-03 diff --git a/challenge-259/paulo-custodio/t/test-2.yaml b/challenge-259/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..ac68f47087 --- /dev/null +++ b/challenge-259/paulo-custodio/t/test-2.yaml @@ -0,0 +1,23 @@ +- setup: + cleanup: + args: '{% id field1=\"value1\" field2=\"value2\" field3=42 %}' + input: + output: | + |{ + | fields => { field1 => "value1", field2 => "value2", field3 => 42 }, + | name => "id", + |} +- setup: + cleanup: + args: '{% youtube title=\"Title \\\"quoted\\\" done\" %}' + input: + output: { fields => { title => "Title \"quoted\" done" }, name => "youtube" } +- setup: + cleanup: + args: '{% youtube title=\"Title with escaped backslash \\\\\" %}' + input: + output: | + |{ + | fields => { title => "Title with escaped backslash \\" }, + | name => "youtube", + |} -- cgit From a781d76bd0b7823a127506b12ed75a372284fb78 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 26 Aug 2024 15:26:59 +0100 Subject: Add Perl solution to challenge 260 --- challenge-260/paulo-custodio/Makefile | 2 ++ challenge-260/paulo-custodio/perl/ch-1.pl | 47 ++++++++++++++++++++++++++++ challenge-260/paulo-custodio/perl/ch-2.pl | 50 ++++++++++++++++++++++++++++++ challenge-260/paulo-custodio/t/test-1.yaml | 15 +++++++++ challenge-260/paulo-custodio/t/test-2.yaml | 15 +++++++++ 5 files changed, 129 insertions(+) create mode 100644 challenge-260/paulo-custodio/Makefile create mode 100644 challenge-260/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-260/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-260/paulo-custodio/t/test-1.yaml create mode 100644 challenge-260/paulo-custodio/t/test-2.yaml diff --git a/challenge-260/paulo-custodio/Makefile b/challenge-260/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-260/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-260/paulo-custodio/perl/ch-1.pl b/challenge-260/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..9486b72fc7 --- /dev/null +++ b/challenge-260/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +# Challenge 260 +# +# Task 1: Unique Occurrences +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# +# Write a script to return 1 if the number of occurrences of each value in the +# given array is unique or 0 otherwise. +# Example 1 +# +# Input: @ints = (1,2,2,1,1,3) +# Output: 1 +# +# The number 1 occurred 3 times. +# The number 2 occurred 2 times. +# The number 3 occurred 1 time. +# +# All occurrences are unique, therefore the output is 1. +# +# Example 2 +# +# Input: @ints = (1,2,3) +# Output: 0 +# +# Example 3 +# +# Input: @ints = (-2,0,1,-2,1,1,0,1,-2,9) +# Output: 1 + +use Modern::Perl; + +my @ints = @ARGV; +say uniq_occurences(@ints); + +sub uniq_occurences { + my(@ints) = @_; + my %count; + $count{$_}++ for @ints; + my %uniq; + for (values %count) { + return 0 if $uniq{$_}++; + } + return 1; +} diff --git a/challenge-260/paulo-custodio/perl/ch-2.pl b/challenge-260/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b256c5e878 --- /dev/null +++ b/challenge-260/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +# Challenge 260 +# +# Task 2: Dictionary Rank +# Submitted by: Mark Anderson +# +# You are given a word, $word. +# +# Write a script to compute the dictionary rank of the given word. +# Example 1 +# +# Input: $word = 'CAT' +# Output: 3 +# +# All possible combinations of the letters: +# CAT, CTA, ATC, TCA, ACT, TAC +# +# Arrange them in alphabetical order: +# ACT, ATC, CAT, CTA, TAC, TCA +# +# CAT is the 3rd in the list. +# Therefore the dictionary rank of CAT is 3. +# +# Example 2 +# +# Input: $word = 'GOOGLE' +# Output: 88 +# +# Example 3 +# +# Input: $word = 'SECRET' +# Output: 255 + +use Modern::Perl; +use Algorithm::Combinatorics qw(permutations); +use List::Util 'uniq'; +use List::MoreUtils 'onlyidx'; + +say dictionary_rank(shift // ""); + +sub dictionary_rank { + my($word) = @_; + return + 1+(onlyidx {$_ eq $word} + sort {$a cmp $b} + uniq + map {join '', @$_} + permutations([split //, $word]))[0]; +} diff --git a/challenge-260/paulo-custodio/t/test-1.yaml b/challenge-260/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..afed95bb2a --- /dev/null +++ b/challenge-260/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 2 1 1 3 + input: + output: 1 +- setup: + cleanup: + args: 1 2 3 + input: + output: 0 +- setup: + cleanup: + args: -2 0 1 -2 1 1 0 1 -2 9 + input: + output: 1 diff --git a/challenge-260/paulo-custodio/t/test-2.yaml b/challenge-260/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..035ac3f62a --- /dev/null +++ b/challenge-260/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: CAT + input: + output: 3 +- setup: + cleanup: + args: GOOGLE + input: + output: 88 +- setup: + cleanup: + args: SECRET + input: + output: 255 -- cgit From 67022eab2418c935aa5b899d0756ea08cfdb0f63 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 26 Aug 2024 15:56:20 +0100 Subject: Add Perl solution to challenge 261 --- challenge-261/paulo-custodio/Makefile | 2 ++ challenge-261/paulo-custodio/perl/ch-1.pl | 49 ++++++++++++++++++++++++++ challenge-261/paulo-custodio/perl/ch-2.pl | 56 ++++++++++++++++++++++++++++++ challenge-261/paulo-custodio/t/test-1.yaml | 20 +++++++++++ challenge-261/paulo-custodio/t/test-2.yaml | 15 ++++++++ 5 files changed, 142 insertions(+) create mode 100644 challenge-261/paulo-custodio/Makefile create mode 100644 challenge-261/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-261/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-261/paulo-custodio/t/test-1.yaml create mode 100644 challenge-261/paulo-custodio/t/test-2.yaml diff --git a/challenge-261/paulo-custodio/Makefile b/challenge-261/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-261/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-261/paulo-custodio/perl/ch-1.pl b/challenge-261/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..aaec18f3ed --- /dev/null +++ b/challenge-261/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +# Challenge 261 +# +# Task 1: Element Digit Sum +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# +# Write a script to evaluate the absolute difference between element and digit +# sum of the given array. +# Example 1 +# +# Input: @ints = (1,2,3,45) +# Output: 36 +# +# Element Sum: 1 + 2 + 3 + 45 = 51 +# Digit Sum: 1 + 2 + 3 + 4 + 5 = 15 +# Absolute Difference: | 51 - 15 | = 36 +# +# Example 2 +# +# Input: @ints = (1,12,3) +# Output: 9 +# +# Element Sum: 1 + 12 + 3 = 16 +# Digit Sum: 1 + 1 + 2 + 3 = 7 +# Absolute Difference: | 16 - 7 | = 9 +# +# Example 3 +# +# Input: @ints = (1,2,3,4) +# Output: 0 +# +# Element Sum: 1 + 2 + 3 + 4 = 10 +# Digit Sum: 1 + 2 + 3 + 4 = 10 +# Absolute Difference: | 10 - 10 | = 0 +# +# Example 4 +# +# Input: @ints = (236, 416, 336, 350) +# Output: 1296 + +use Modern::Perl; +use List::Util 'sum'; + +my @ints = @ARGV; +my @digits = split //, join '', @ints; +say abs(sum(@ints) - sum(@digits)); diff --git a/challenge-261/paulo-custodio/perl/ch-2.pl b/challenge-261/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..289fc37213 --- /dev/null +++ b/challenge-261/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl + +# Challenge 261 +# +# Task 2: Multiply by Two +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints and an integer $start.. +# +# Write a script to do the followings: +# +# a) Look for $start in the array @ints, if found multiply the number by 2 +# b) If not found stop the process otherwise repeat +# +# In the end return the final value. +# Example 1 +# +# Input: @ints = (5,3,6,1,12) and $start = 3 +# Output: 24 +# +# Step 1: 3 is in the array so 3 x 2 = 6 +# Step 2: 6 is in the array so 6 x 2 = 12 +# Step 3: 12 is in the array so 12 x 2 = 24 +# +# 24 is not found in the array so return 24. +# +# Example 2 +# +# Input: @ints = (1,2,4,3) and $start = 1 +# Output: 8 +# +# Step 1: 1 is in the array so 1 x 2 = 2 +# Step 2: 2 is in the array so 2 x 2 = 4 +# Step 3: 4 is in the array so 4 x 2 = 8 +# +# 8 is not found in the array so return 8. +# +# Example 3 +# +# Input: @ints = (5,6,7) and $start = 2 +# Output: 2 +# +# 2 is not found in the array so return 2. + +use Modern::Perl; + +my($start, @ints) = @ARGV; +say mult_two($start, @ints); + +sub mult_two { + my($n, @ints) = @_; + while (grep {$_ == $n} @ints) { + $n *= 2; + } + return $n; +} diff --git a/challenge-261/paulo-custodio/t/test-1.yaml b/challenge-261/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..26fe36e663 --- /dev/null +++ b/challenge-261/paulo-custodio/t/test-1.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 1 2 3 45 + input: + output: 36 +- setup: + cleanup: + args: 1 12 3 + input: + output: 9 +- setup: + cleanup: + args: 1 2 3 4 + input: + output: 0 +- setup: + cleanup: + args: 236 416 336 350 + input: + output: 1296 diff --git a/challenge-261/paulo-custodio/t/test-2.yaml b/challenge-261/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..b1d5ba5d6c --- /dev/null +++ b/challenge-261/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 3 5 3 6 1 12 + input: + output: 24 +- setup: + cleanup: + args: 1 1 2 4 3 + input: + output: 8 +- setup: + cleanup: + args: 2 5 6 7 + input: + output: 2 -- cgit From cc74618d0dbe8a9e1ec1819aeff9a702cb7536e2 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 26 Aug 2024 16:18:27 +0100 Subject: Add Perl solution to challenge 262 --- challenge-262/paulo-custodio/Makefile | 2 ++ challenge-262/paulo-custodio/perl/ch-1.pl | 43 ++++++++++++++++++++++++++++ challenge-262/paulo-custodio/perl/ch-2.pl | 45 ++++++++++++++++++++++++++++++ challenge-262/paulo-custodio/t/test-1.yaml | 15 ++++++++++ challenge-262/paulo-custodio/t/test-2.yaml | 10 +++++++ 5 files changed, 115 insertions(+) create mode 100644 challenge-262/paulo-custodio/Makefile create mode 100644 challenge-262/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-262/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-262/paulo-custodio/t/test-1.yaml create mode 100644 challenge-262/paulo-custodio/t/test-2.yaml diff --git a/challenge-262/paulo-custodio/Makefile b/challenge-262/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-262/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-262/paulo-custodio/perl/ch-1.pl b/challenge-262/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..de3a9361db --- /dev/null +++ b/challenge-262/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Challenge 262 +# +# Task 1: Max Positive Negative +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# +# Write a script to return the maximum number of either positive or negative +# integers in the given array. +# Example 1 +# +# Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +# Output: 4 +# +# Count of positive integers: 4 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 4 +# +# Example 2 +# +# Input: @ints = (-1, -2, -3, 1) +# Output: 3 +# +# Count of positive integers: 1 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 3 +# +# Example 3 +# +# Input: @ints = (1,2) +# Output: 2 +# +# Count of positive integers: 2 +# Count of negative integers: 0 +# Maximum of count of positive and negative integers: 2 + +use Modern::Perl; +use List::Util 'max'; + +my @ints = @ARGV; +say max(scalar(grep {$_>0} @ints), scalar(grep {$_<0} @ints)); diff --git a/challenge-262/paulo-custodio/perl/ch-2.pl b/challenge-262/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..4977ea41d6 --- /dev/null +++ b/challenge-262/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +# Challenge 262 +# +# Task 2: Count Equal Divisible +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints and an integer $k. +# +# Write a script to return the number of pairs (i, j) where +# +# a) 0 <= i < j < size of @ints +# b) ints[i] == ints[j] +# c) i x j is divisible by k +# +# Example 1 +# +# Input: @ints = (3,1,2,2,2,1,3) and $k = 2 +# Output: 4 +# +# (0, 6) => ints[0] == ints[6] and 0 x 6 is divisible by 2 +# (2, 3) => ints[2] == ints[3] and 2 x 3 is divisible by 2 +# (2, 4) => ints[2] == ints[4] and 2 x 4 is divisible by 2 +# (3, 4) => ints[3] == ints[4] and 3 x 4 is divisible by 2 +# +# Example 2 +# +# Input: @ints = (1,2,3) and $k = 1 +# Output: 0 + +use Modern::Perl; + +my($k, @ints) = @ARGV; +say count_pairs($k, @ints); + +sub count_pairs { + my($k, @ints) = @_; + my $count = 0; + for my $i (0 .. $#ints-1) { + for my $j ($i+1 .. $#ints) { + $count++ if $ints[$i] == $ints[$j] && ($i*$j) % $k == 0; + } + } + return $count; +} diff --git a/challenge-262/paulo-custodio/t/test-1.yaml b/challenge-262/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..0c642bb9bd --- /dev/null +++ b/challenge-262/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: -3 1 2 -1 3 -2 4 + input: + output: 4 +- setup: + cleanup: + args: -1 -2 -3 1 + input: + output: 3 +- setup: + cleanup: + args: 1 2 + input: + output: 2 diff --git a/challenge-262/paulo-custodio/t/test-2.yaml b/challenge-262/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..ce1c8bff67 --- /dev/null +++ b/challenge-262/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 2 3 1 2 2 2 1 3 + input: + output: 4 +- setup: + cleanup: + args: 1 1 2 3 + input: + output: 0 -- cgit From d8ff7a20c8b9a2d659e323c0da9c52e75cb85b08 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 26 Aug 2024 16:54:51 +0100 Subject: Add Perl solution to challenge 263 --- challenge-263/paulo-custodio/Makefile | 2 ++ challenge-263/paulo-custodio/perl/ch-1.pl | 43 +++++++++++++++++++++++++ challenge-263/paulo-custodio/perl/ch-2.pl | 51 ++++++++++++++++++++++++++++++ challenge-263/paulo-custodio/t/test-1.yaml | 15 +++++++++ challenge-263/paulo-custodio/t/test-2.yaml | 15 +++++++++ 5 files changed, 126 insertions(+) create mode 100644 challenge-263/paulo-custodio/Makefile create mode 100644 challenge-263/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-263/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-263/paulo-custodio/t/test-1.yaml create mode 100644 challenge-263/paulo-custodio/t/test-2.yaml diff --git a/challenge-263/paulo-custodio/Makefile b/challenge-263/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-263/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-263/paulo-custodio/perl/ch-1.pl b/challenge-263/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..228096e9f8 --- /dev/null +++ b/challenge-263/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Challenge 263 +# +# Task 1: Target Index +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints and a target element $k. +# +# Write a script to return the list of indices in the sorted array where the +# element is same as the given target element. +# Example 1 +# +# Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2 +# Output: (1, 2) +# +# Sorted array: (1, 2, 2, 3, 4, 5) +# Target indices: (1, 2) as $ints[1] = 2 and $ints[2] = 2 +# +# Example 2 +# +# Input: @ints = (1, 2, 4, 3, 5), $k = 6 +# Output: () +# +# No element in the given array matching the given target. +# +# Example 3 +# +# Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4 +# Output: (4) +# +# Sorted array: (1, 2, 2, 3, 4, 5) +# Target index: (4) as $ints[4] = 4 + +use Modern::Perl; + +my($k, @ints) = @ARGV; +@ints = sort {$a <=> $b} @ints; +my @idx = + map {$_->[0]} + grep {$_->[1] == $k} + map {[$_, $ints[$_]]} 0 .. $#ints; +say @idx ? "@idx" : "()"; diff --git a/challenge-263/paulo-custodio/perl/ch-2.pl b/challenge-263/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..83d7e446ce --- /dev/null +++ b/challenge-263/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +# Challenge 263 +# +# Task 2: Merge Items +# Submitted by: Mohammad Sajid Anwar +# +# You are given two 2-D array of positive integers, $items1 and $items2 where +# element is pair of (item_id, item_quantity). +# +# Write a script to return the merged items. +# Example 1 +# +# Input: $items1 = [ [1,1], [2,1], [3,2] ] +# $items2 = [ [2,2], [1,3] ] +# Output: [ [1,4], [2,3], [3,2] ] +# +# Item id (1) appears 2 times: [1,1] and [1,3]. Merged item now (1,4) +# Item id (2) appears 2 times: [2,1] and [2,2]. Merged item now (2,3) +# Item id (3) appears 1 time: [3,2] +# +# Example 2 +# +# Input: $items1 = [ [1,2], [2,3], [1,3], [3,2] ] +# $items2 = [ [3,1], [1,3] ] +# Output: [ [1,8], [2,3], [3,3] ] +# +# Example 3 +# +# Input: $items1 = [ [1,1], [2,2], [3,3] ] +# $items2 = [ [2,3], [2,4] ] +# Output: [ [1,1], [2,9], [3,3] ] + +use Modern::Perl; +use List::Util qw( uniq pairs ); + +my($items1, $items2) = split /,/, "@ARGV"; +my @items1 = (split ' ', $items1); +my @items2 = (split ' ', $items2); + +my %items; +for (pairs(@items1, @items2)) { + my($k, $v) = @$_; + $items{$k} += $v; +} + +my @result; +for my $k (sort {$a <=> $b} keys %items) { + push @result, $k." ".$items{$k}; +} +say join " ", @result; diff --git a/challenge-263/paulo-custodio/t/test-1.yaml b/challenge-263/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..6640790b5a --- /dev/null +++ b/challenge-263/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 2 1 5 3 2 4 2 + input: + output: 1 2 +- setup: + cleanup: + args: 6 1 2 4 3 5 + input: + output: () +- setup: + cleanup: + args: 4 5 3 2 4 2 1 + input: + output: 4 diff --git a/challenge-263/paulo-custodio/t/test-2.yaml b/challenge-263/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..b54303afda --- /dev/null +++ b/challenge-263/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 1 2 1 3 2 , 2 2 1 3 + input: + output: 1 4 2 3 3 2 +- setup: + cleanup: + args: 1 2 2 3 1 3 3 2 , 3 1 1 3 + input: + output: 1 8 2 3 3 3 +- setup: + cleanup: + args: 1 1 2 2 3 3 , 2 3 2 4 + input: + output: 1 1 2 9 3 3 -- cgit