diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-28 20:28:12 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-28 20:28:12 +0100 |
| commit | 553bcbfae25eaa5166a19597c16b8dfa1ca0a996 (patch) | |
| tree | c2b95a855aed8973efbb63f80fb18722279a284e | |
| parent | 6669d291d290c26627713509c1de4fe72ffe43bc (diff) | |
| parent | 1bbce619a1d4a5e54c9c6737e69e733db48da22b (diff) | |
| download | perlweeklychallenge-club-553bcbfae25eaa5166a19597c16b8dfa1ca0a996.tar.gz perlweeklychallenge-club-553bcbfae25eaa5166a19597c16b8dfa1ca0a996.tar.bz2 perlweeklychallenge-club-553bcbfae25eaa5166a19597c16b8dfa1ca0a996.zip | |
Merge pull request #10727 from pauloscustodio/master
Add Perl solutions
| -rw-r--r-- | challenge-268/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-268/paulo-custodio/perl/ch-1.pl | 52 | ||||
| -rw-r--r-- | challenge-268/paulo-custodio/perl/ch-2.pl | 44 | ||||
| -rw-r--r-- | challenge-268/paulo-custodio/t/test-1.yaml | 25 | ||||
| -rw-r--r-- | challenge-268/paulo-custodio/t/test-2.yaml | 15 | ||||
| -rw-r--r-- | challenge-269/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-269/paulo-custodio/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-269/paulo-custodio/perl/ch-2.pl | 115 | ||||
| -rw-r--r-- | challenge-269/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-269/paulo-custodio/t/test-2.yaml | 15 |
10 files changed, 327 insertions, 0 deletions
diff --git a/challenge-268/paulo-custodio/Makefile b/challenge-268/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-268/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-268/paulo-custodio/perl/ch-1.pl b/challenge-268/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..71802c3800 --- /dev/null +++ b/challenge-268/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl + +# Challenge 268 +# +# Input: @x = (3, 7, 5) +# @y = (9, 5, 7) +# Output: 2 +# +# The magic number is 2. +# @x = (3, 7, 5) +# + 2 2 2 +# @y = (5, 9, 7) +# +# Example 2 +# +# Input: @x = (1, 2, 1) +# @y = (5, 4, 4) +# Output: 3 +# +# The magic number is 3. +# @x = (1, 2, 1) +# + 3 3 3 +# @y = (5, 4, 4) +# +# Example 3 +# +# Input: @x = (2) +# @y = (5) +# Output: 3 + +use Modern::Perl; + +my($x, $y) = split /,/, "@ARGV"; +my @x = split ' ', $x; +my @y = split ' ', $y; + +say magic_number(\@x, \@y); + +sub magic_number { + my($x, $y) = @_; + + my @x = sort {$a <=> $b} @$x; + my @y = sort {$a <=> $b} @$y; + + return -1 if scalar(@x) != scalar(@y); + + my $delta = $y[0]-$x[0]; + for (0 .. $#x) { + return -1 if $y[$_]-$x[$_] != $delta; + } + return $delta; +} diff --git a/challenge-268/paulo-custodio/perl/ch-2.pl b/challenge-268/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..5072831943 --- /dev/null +++ b/challenge-268/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +# Challenge 268 +# +# Task 2: Number Game +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints, with even number of elements. +# +# Write a script to create a new array made up of elements of the given array. +# Pick the two smallest integers and add it to new array in decreasing order +# i.e. high to low. Keep doing until the given array is empty. +# Example 1 +# +# Input: @ints = (2, 5, 3, 4) +# Output: (3, 2, 5, 4) +# +# Round 1: we picked (2, 3) and push it to the new array (3, 2) +# Round 2: we picked the remaining (4, 5) and push it to the new array (5, 4) +# +# Example 2 +# +# Input: @ints = (9, 4, 1, 3, 6, 4, 6, 1) +# Output: (1, 1, 4, 3, 6, 4, 9, 6) +# +# Example 3 +# +# Input: @ints = (1, 2, 2, 3) +# Output: (2, 1, 3, 2) + +use Modern::Perl; + +say join " ", number_game(@ARGV); + +sub number_game { + my(@ints) = @_; + my @out; + while (@ints) { + @ints = sort {$a <=> $b} @ints; + push @out, $ints[1], $ints[0]; + splice @ints, 0, 2; + } + return @out; +} diff --git a/challenge-268/paulo-custodio/t/test-1.yaml b/challenge-268/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..dabdffb7a6 --- /dev/null +++ b/challenge-268/paulo-custodio/t/test-1.yaml @@ -0,0 +1,25 @@ +- setup: + cleanup: + args: 3 7 5 , 9 5 7 + input: + output: 2 +- setup: + cleanup: + args: 1 2 1 , 5 4 4 + input: + output: 3 +- setup: + cleanup: + args: 1 2 1 , 5 4 3 + input: + output: -1 +- setup: + cleanup: + args: 1 2 1 , 5 4 + input: + output: -1 +- setup: + cleanup: + args: 2 , 5 + input: + output: 3 diff --git a/challenge-268/paulo-custodio/t/test-2.yaml b/challenge-268/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..9cc8213279 --- /dev/null +++ b/challenge-268/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 2 5 3 4 + input: + output: 3 2 5 4 +- setup: + cleanup: + args: 9 4 1 3 6 4 6 1 + input: + output: 1 1 4 3 6 4 9 6 +- setup: + cleanup: + args: 1 2 2 3 + input: + output: 2 1 3 2 diff --git a/challenge-269/paulo-custodio/Makefile b/challenge-269/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-269/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-269/paulo-custodio/perl/ch-1.pl b/challenge-269/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..ad34b0c94c --- /dev/null +++ b/challenge-269/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +# Challenge 269 +# +# Task 1: Bitwise OR +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of positive integers, @ints. +# +# Write a script to find out if it is possible to select two or more elements +# of the given array such that the bitwise OR of the selected elements has at +# lest one trailing zero in its binary representation. +# Example 1 +# +# Input: @ints = (1, 2, 3, 4, 5) +# Output: true +# +# Say, we pick 2 and 4, thier bitwise OR is 6. The binary representation of 6 is 110. +# Return true since we have one trailing zero. +# +# Example 2 +# +# Input: @ints = (2, 3, 8, 16) +# Output: true +# +# Say, we pick 2 and 8, thier bitwise OR is 10. The binary representation of 10 is 1010. +# Return true since we have one trailing zero. +# +# Example 3 +# +# Input: @ints = (1, 2, 5, 7, 9) +# Output: false + +use Modern::Perl; + +say trailing_zero(@ARGV) ? 'true' : 'false'; + +sub trailing_zero { + my(@ints) = @_; + my $count_trailing_zero = grep {($_ & 1) == 0} @ints; + return $count_trailing_zero >= 2; +} diff --git a/challenge-269/paulo-custodio/perl/ch-2.pl b/challenge-269/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..1dc3721b10 --- /dev/null +++ b/challenge-269/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,115 @@ +#!/usr/bin/env perl + +# Challenge 269 +# +# Task 2: Distribute Elements +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of distinct integers, @ints. +# +# Write a script to distribute the elements as described below: +# +# 1) Put the 1st element of the given array to a new array @arr1. +# 2) Put the 2nd element of the given array to a new array @arr2. +# +# Once you have one element in each arrays, @arr1 and @arr2, then follow the rule below: +# +# If the last element of the array @arr1 is greater than the last +# element of the array @arr2 then add the first element of the +# given array to @arr1 otherwise to the array @arr2. +# +# When done distribution, return the concatenated arrays. @arr1 and @arr2. +# Example 1 +# +# Input: @ints = (2, 1, 3, 4, 5) +# Output: (2, 3, 4, 5, 1) +# +# 1st operation: +# Add 1 to @arr1 = (2) +# +# 2nd operation: +# Add 2 to @arr2 = (1) +# +# 3rd operation: +# Now the last element of @arr1 is greater than the last element +# of @arr2, add 3 to @arr1 = (2, 3). +# +# 4th operation: +# Again the last element of @arr1 is greate than the last element +# of @arr2, add 4 to @arr1 = (2, 3, 4) +# +# 5th operation: +# Finally, the last element of @arr1 is again greater than the last +# element of @arr2, add 5 to @arr1 = (2, 3, 4, 5) +# +# Mow we have two arrays: +# @arr1 = (2, 3, 4, 5) +# @arr2 = (1) +# +# Concatenate the two arrays and return the final array: (2, 3, 4, 5, 1). +# +# Example 2 +# +# Input: @ints = (3, 2, 4) +# Output: (3, 4, 2) +# +# 1st operation: +# Add 1 to @arr1 = (3) +# +# 2nd operation: +# Add 2 to @arr2 = (2) +# +# 3rd operation: +# Now the last element of @arr1 is greater than the last element +# of @arr2, add 4 to @arr1 = (3, 4). +# +# Mow we have two arrays: +# @arr1 = (3, 4) +# @arr2 = (2) +# +# Concatenate the two arrays and return the final array: (3, 4, 2). +# +# Example 3 +# +# Input: @ints = (5, 4, 3 ,8) +# Output: (5, 3, 4, 8) +# +# 1st operation: +# Add 1 to @arr1 = (5) +# +# 2nd operation: +# Add 2 to @arr2 = (4) +# +# 3rd operation: +# Now the last element of @arr1 is greater than the last element +# of @arr2, add 3 to @arr1 = (5, 3). +# +# 4th operation: +# Again the last element of @arr2 is greate than the last element +# of @arr1, add 8 to @arr2 = (4, 8) +# +# Mow we have two arrays: +# @arr1 = (5, 3) +# @arr2 = (4, 8) +# +# Concatenate the two arrays and return the final array: (5, 3, 4, 8). + +use Modern::Perl; + +say join " ", distribute(@ARGV); + +sub distribute { + my(@ints) = @_; + return @ints if @ints < 2; + my @a1 = (shift @ints); + my @a2 = (shift @ints); + while (@ints) { + if ($a1[-1] > $a2[-1]) { + push @a1, shift @ints; + } + else { + push @a2, shift @ints; + } + } + return (@a1, @a2); +} diff --git a/challenge-269/paulo-custodio/t/test-1.yaml b/challenge-269/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..36f459ed26 --- /dev/null +++ b/challenge-269/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 3 4 5 + input: + output: true +- setup: + cleanup: + args: 2 3 8 16 + input: + output: true +- setup: + cleanup: + args: 1 2 5 7 9 + input: + output: false diff --git a/challenge-269/paulo-custodio/t/test-2.yaml b/challenge-269/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..e3ccfd73ba --- /dev/null +++ b/challenge-269/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 2 1 3 4 5 + input: + output: 2 3 4 5 1 +- setup: + cleanup: + args: 3 2 4 + input: + output: 3 4 2 +- setup: + cleanup: + args: 5 4 3 8 + input: + output: 5 3 4 8 |
