diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-25 19:13:45 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-25 19:13:45 +0100 |
| commit | 009c16c2546a3c4ac0746a2b8dc524283046ea40 (patch) | |
| tree | 7c0b74725ee6d12cb8a60b58eaff50727f8e7e79 /challenge-258 | |
| parent | 093d0b21158a2f785f068e530bf3475fa906c30e (diff) | |
| download | perlweeklychallenge-club-009c16c2546a3c4ac0746a2b8dc524283046ea40.tar.gz perlweeklychallenge-club-009c16c2546a3c4ac0746a2b8dc524283046ea40.tar.bz2 perlweeklychallenge-club-009c16c2546a3c4ac0746a2b8dc524283046ea40.zip | |
Add Perl solution to challenge 258
Diffstat (limited to 'challenge-258')
| -rw-r--r-- | challenge-258/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-258/paulo-custodio/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-258/paulo-custodio/perl/ch-2.pl | 44 | ||||
| -rw-r--r-- | challenge-258/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-258/paulo-custodio/t/test-2.yaml | 15 |
5 files changed, 102 insertions, 0 deletions
diff --git a/challenge-258/paulo-custodio/Makefile b/challenge-258/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-258/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-258/paulo-custodio/perl/ch-1.pl b/challenge-258/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..2cf654752e --- /dev/null +++ b/challenge-258/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +# Challenge 258 +# +# Task 1: Count Even Digits Number +# Submitted by: Mohammad Sajid Anwar +# You are given a array of positive integers, @ints. +# +# Write a script to find out how many integers have even number of digits. +# +# Example 1 +# Input: @ints = (10, 1, 111, 24, 1000) +# Output: 3 +# +# There are 3 integers having even digits i.e. 10, 24 and 1000. +# Example 2 +# Input: @ints = (111, 1, 11111) +# Output: 0 +# Example 3 +# Input: @ints = (2, 8, 1024, 256) +# Output: 1 + +use Modern::Perl; + +my @ints = @ARGV; +say scalar grep {length($_) % 2 == 0} @ints; diff --git a/challenge-258/paulo-custodio/perl/ch-2.pl b/challenge-258/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b7770dee60 --- /dev/null +++ b/challenge-258/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +# Challenge 258 +# +# Task 2: Sum of Values +# Submitted by: Mohammad Sajid Anwar +# You are given an array of integers, @int and an integer $k. +# +# Write a script to find the sum of values whose index binary representation has exactly $k number of 1-bit set. +# +# Example 1 +# Input: @ints = (2, 5, 9, 11, 3), $k = 1 +# Output: 17 +# +# Binary representation of index 0 = 0 +# Binary representation of index 1 = 1 +# Binary representation of index 2 = 10 +# Binary representation of index 3 = 11 +# Binary representation of index 4 = 100 +# +# So the indices 1, 2 and 4 have total one 1-bit sets. +# Therefore the sum, $ints[1] + $ints[2] + $ints[4] = 17 +# Example 2 +# Input: @ints = (2, 5, 9, 11, 3), $k = 2 +# Output: 11 +# Example 3 +# Input: @ints = (2, 5, 9, 11, 3), $k = 0 +# Output: 2 + +use Modern::Perl; +use List::Util 'sum'; + +my($k, @ints) = @ARGV; +say sum + map {$_->[1]} + grep {num_ones($_->[0]) == $k} + map {[$_, $ints[$_]]} 0 .. $#ints; + +sub num_ones { + my($n) = @_; + my $bin = sprintf("%b", $n); + my $ones = $bin =~ tr/1/1/; + return $ones; +} diff --git a/challenge-258/paulo-custodio/t/test-1.yaml b/challenge-258/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..5138f53225 --- /dev/null +++ b/challenge-258/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 10 1 111 24 1000 + input: + output: 3 +- setup: + cleanup: + args: 111 1 11111 + input: + output: 0 +- setup: + cleanup: + args: 2 8 1024 256 + input: + output: 1 diff --git a/challenge-258/paulo-custodio/t/test-2.yaml b/challenge-258/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..1f8234084e --- /dev/null +++ b/challenge-258/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 5 9 11 3 + input: + output: 17 +- setup: + cleanup: + args: 2 2 5 9 11 3 + input: + output: 11 +- setup: + cleanup: + args: 0 2 5 9 11 3 + input: + output: 2 |
