diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-28 18:38:41 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-28 18:39:15 +0100 |
| commit | 1bbce619a1d4a5e54c9c6737e69e733db48da22b (patch) | |
| tree | 41a776d065499099aff6793391aac947950cdbaa | |
| parent | 1478301bf14e05ce900cb87cbefb8405a123e0ab (diff) | |
| download | perlweeklychallenge-club-1bbce619a1d4a5e54c9c6737e69e733db48da22b.tar.gz perlweeklychallenge-club-1bbce619a1d4a5e54c9c6737e69e733db48da22b.tar.bz2 perlweeklychallenge-club-1bbce619a1d4a5e54c9c6737e69e733db48da22b.zip | |
Add Perl solution to challenge 269
| -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 |
5 files changed, 189 insertions, 0 deletions
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 |
