diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-27 22:51:55 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-27 22:51:55 +0100 |
| commit | 1f0e0a061b0e758d9a10010c573443f1dcf9ae20 (patch) | |
| tree | f1a14445254d09edb0944a43b1cf4eed722fa33a | |
| parent | 9e3e295b48ecd4cd97c2e17f2fa98d5fee18c8da (diff) | |
| download | perlweeklychallenge-club-1f0e0a061b0e758d9a10010c573443f1dcf9ae20.tar.gz perlweeklychallenge-club-1f0e0a061b0e758d9a10010c573443f1dcf9ae20.tar.bz2 perlweeklychallenge-club-1f0e0a061b0e758d9a10010c573443f1dcf9ae20.zip | |
Add Perl solution
| -rw-r--r-- | challenge-193/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-193/paulo-custodio/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-193/paulo-custodio/perl/ch-2.pl | 78 | ||||
| -rw-r--r-- | challenge-193/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-193/paulo-custodio/t/test-2.yaml | 15 |
5 files changed, 128 insertions, 0 deletions
diff --git a/challenge-193/paulo-custodio/Makefile b/challenge-193/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-193/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-193/paulo-custodio/perl/ch-1.pl b/challenge-193/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..4f0c936287 --- /dev/null +++ b/challenge-193/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl + +# Challenge 193 +# +# Task 1: Binary String +# Submitted by: Mohammad S Anwar +# You are given an integer, $n > 0. +# +# Write a script to find all possible binary numbers of size $n. +# +# Example 1 +# Input: $n = 2 +# Output: 00, 11, 01, 10 +# Example 2 +# Input: $n = 3 +# Output: 000, 001, 010, 100, 111, 110, 101, 011 + +use Modern::Perl; + +@ARGV==1 or die "usage: ch-1.pl n\n"; +my $n=shift||1; +say join ", ", map{sprintf("%0${n}b", $_)} (0..2**$n-1); + diff --git a/challenge-193/paulo-custodio/perl/ch-2.pl b/challenge-193/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..8a3d5f6484 --- /dev/null +++ b/challenge-193/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,78 @@ +#!/usr/bin/perl + +# Challenge 193 +# +# Task 2: Odd String +# Submitted by: Mohammad S Anwar +# You are given a list of strings of same length, @s. +# +# Write a script to find the odd string in the given list. Use positional value +# of alphabet starting with 0, i.e. a = 0, b = 1, ... z = 25. +# +# Find the difference array for each string as shown in the example. Then pick +# the odd one out. +# +# Example 1: +# Input: @s = ("adc", "wzy", "abc") +# Output: "abc" +# +# Difference array for "adc" => [ d - a, c - d ] +# => [ 3 - 0, 2 - 3 ] +# => [ 3, -1 ] +# +# Difference array for "wzy" => [ z - w, y - z ] +# => [ 25 - 22, 24 - 25 ] +# => [ 3, -1 ] +# +# Difference array for "abc" => [ b - a, c - b ] +# => [ 1 - 0, 2 - 1 ] +# => [ 1, 1 ] +# +# The difference array for "abc" is the odd one. +# Example 2: +# Input: @s = ("aaa", "bob", "ccc", "ddd") +# Output: "bob" +# +# Difference array for "aaa" => [ a - a, a - a ] +# => [ 0 - 0, 0 - 0 ] +# => [ 0, 0 ] +# +# Difference array for "bob" => [ o - b, b - o ] +# => [ 14 - 1, 1 - 14 ] +# => [ 13, -13 ] +# +# Difference array for "ccc" => [ c - c, c - c ] +# => [ 2 - 2, 2 - 2 ] +# => [ 0, 0 ] +# +# Difference array for "ddd" => [ d - d, d - d ] +# => [ 3 - 3, 3 - 3 ] +# => [ 0, 0 ] +# +# The difference array for "bob" is the odd one. + +use Modern::Perl; + +sub string_diff { + my($string)=@_; + my @values = map {ord} split //, $string; + my @diff = map {$values[$_+1]-$values[$_]} 0..$#values-1; + return @diff; +} + +sub odd_string { + my(@strings)=@_; + my %diffs; + my %strings; + for (@strings) { + my @diffs = string_diff($_); + $diffs{"@diffs"}++; + $strings{"@diffs"}=$_; + } + my @odd = grep {$diffs{$_}==1} keys %diffs; + return @odd==1 ? $strings{$odd[0]} : "."; +} + +@ARGV or die "usage: ch-2.pl string...\n"; +my @strings=@ARGV; +say odd_string(@strings); diff --git a/challenge-193/paulo-custodio/t/test-1.yaml b/challenge-193/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..f23806ddcd --- /dev/null +++ b/challenge-193/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 2 + input: + output: 00, 01, 10, 11 +- setup: + cleanup: + args: 3 + input: + output: 000, 001, 010, 011, 100, 101, 110, 111 diff --git a/challenge-193/paulo-custodio/t/test-2.yaml b/challenge-193/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..1b1854b292 --- /dev/null +++ b/challenge-193/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: adc wzy abc + input: + output: abc +- setup: + cleanup: + args: aaa bob ccc ddd + input: + output: bob +- setup: + cleanup: + args: aaa ccc ddd + input: + output: . |
