diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-11-29 10:10:41 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-29 10:10:41 +0000 |
| commit | d402485c52ecc37251f09ed16bd362922bb1141c (patch) | |
| tree | e6f0a7c8590100f277e8c24abb14bca4a17324bc | |
| parent | a9df9b0b61d40ea6ad71c153c17bd7c0447f9784 (diff) | |
| parent | 4952a46d21fde44a24d855c18fec8c183592d949 (diff) | |
| download | perlweeklychallenge-club-d402485c52ecc37251f09ed16bd362922bb1141c.tar.gz perlweeklychallenge-club-d402485c52ecc37251f09ed16bd362922bb1141c.tar.bz2 perlweeklychallenge-club-d402485c52ecc37251f09ed16bd362922bb1141c.zip | |
Merge pull request #7180 from choroba/ech193
Add solutions to 193: Binary String & Odd String by E. Choroba
| -rwxr-xr-x | challenge-193/e-choroba/perl/ch-1.pl | 27 | ||||
| -rwxr-xr-x | challenge-193/e-choroba/perl/ch-2.pl | 46 |
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-193/e-choroba/perl/ch-1.pl b/challenge-193/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..4eaa4f8865 --- /dev/null +++ b/challenge-193/e-choroba/perl/ch-1.pl @@ -0,0 +1,27 @@ +#! /usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +sub binary_string($n) { + return [] if 0 == $n; + + my $i = 0; + my @strings; + push @strings, sprintf "%0${n}b", $i++ + while $i < 2 ** $n; + return \@strings +} + +use Test2::V0; +plan 4; + +is binary_string(2), + bag { item $_ for qw[ 00 11 01 10 ]; end }, 'Example 1'; +is binary_string(3), + bag { item $_ for qw[ 000 001 010 100 111 110 101 011 ]; end }, 'Example 2'; + +is binary_string(1), [0, 1], 'Size 1'; + +my $large = binary_string(20); +is scalar @$large, 1 << 20, 'Large'; diff --git a/challenge-193/e-choroba/perl/ch-2.pl b/challenge-193/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..b9ab66902f --- /dev/null +++ b/challenge-193/e-choroba/perl/ch-2.pl @@ -0,0 +1,46 @@ +#! /usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +sub odd_string (@s) { + my %difference_array; + for my $string (@s) { + my @da; + for my $pos (1 .. length($string) - 1) { + push @da, ord(substr $string, $pos, 1) + - ord(substr $string, $pos - 1, 1); + } + push @{ $difference_array{"@da"} }, $string; + die 'Too many different strings' if 2 < keys %difference_array; + } + die 'No odd string' if 1 == keys %difference_array; + + my @ones = grep 1 == @{ $difference_array{$_} }, keys %difference_array; + die 'Too many candidates' if 2 == @ones; + die 'Both groups too large' if 1 != @ones; + + return $difference_array{ $ones[0] }[0] +} + +use Test2::V0; +plan 6; + +is odd_string('adc', 'wzy', 'abc'), 'abc', 'Example 1'; +is odd_string('aaa', 'bob', 'ccc', 'ddd'), 'bob', 'Example 2'; + +like dies { odd_string('aaa', 'abc', 'ace') }, + qr/Too many different strings/, + 'Too many'; + +like dies { odd_string('aaa', 'bbb', 'ccc') }, + qr/No odd string/, + 'All same'; + +like dies { odd_string('aaa', 'bbb', 'abc', 'xyz') }, + qr/Both groups too large/, + 'Too large'; + +like dies { odd_string('aaa', 'abc') }, + qr/Too many candidates/, + 'Several ones'; |
