From c876443748a48ca1098dd90fec6d617803959a5d Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 28 Nov 2022 09:35:12 +0000 Subject: w193 - Task 1 & 2 --- challenge-193/perlboy1967/perl/ch-1.pl | 30 +++++++++++++++++++ challenge-193/perlboy1967/perl/ch-2.pl | 54 ++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100755 challenge-193/perlboy1967/perl/ch-1.pl create mode 100755 challenge-193/perlboy1967/perl/ch-2.pl diff --git a/challenge-193/perlboy1967/perl/ch-1.pl b/challenge-193/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..f80197db50 --- /dev/null +++ b/challenge-193/perlboy1967/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 193 + - https://theweeklychallenge.org/blog/perl-weekly-challenge-193/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +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. + +=cut + +use v5.16; +use warnings; + + +sub binaryString ($) { + map { sprintf("%0$_[0]b", $_) } (0 .. (1<<$_[0])-1); +} + + +for (1 .. 5) { + say join(",", binaryString($_)); +} diff --git a/challenge-193/perlboy1967/perl/ch-2.pl b/challenge-193/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..990ecc08ec --- /dev/null +++ b/challenge-193/perlboy1967/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 193 + - https://theweeklychallenge.org/blog/perl-weekly-challenge-193/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +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. + +=cut + +use v5.16; +use warnings; + +use List::MoreUtils qw(slide frequency); +use Test::More; + + +sub oddString { + state $c = {map { ($_, ord($_) - ord('a')) } 'a' .. 'z'}; + + my (%h,%freq,@k); + + for (@_) { + no warnings 'once'; + my $k = join('|',slide { $c->{$b} - $c->{$a}} split //); + use warnings; + + $h{$k} = $_; + $freq{$k}++; + } + + @k = grep { $freq{$_} == 1 } keys %h; + + return $h{$k[0]} if (scalar(@k) == 1); +} + + +is(oddString("adc", "wzy", "abc"),'abc', 'abc'); +is(oddString("aaa", "bob", "ccc", "ddd"), 'bob', 'bob'); +is(oddString("abcd", "bcde", "cdef", "defh"), 'defh', 'defh'); + +done_testing; -- cgit