From 3a1a900813d5fd4c21ffd5d78d23abb151bedfce Mon Sep 17 00:00:00 2001 From: KUEPPO Date: Fri, 11 Nov 2022 21:42:37 +0100 Subject: pwc-190 ch-2.pl --- challenge-190/kueppo-wesley/Perl/ch-2.pl | 74 ++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 challenge-190/kueppo-wesley/Perl/ch-2.pl diff --git a/challenge-190/kueppo-wesley/Perl/ch-2.pl b/challenge-190/kueppo-wesley/Perl/ch-2.pl new file mode 100644 index 0000000000..065874316c --- /dev/null +++ b/challenge-190/kueppo-wesley/Perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw(state current_sub); + +use Test::More; +use Test::Deep; + +=pod + +=head1 PROBLEM + +Given C and C, words from different encodings and C, a function +which transliterates the alphabets between these encodings. The +primary issue is that composed alphabets are also alphabets. + + +=head1 SOLUTION + +A possible solution to this problem can be to parse the encoded +string to build a tree on which a prefix search generates all the +possible Cs. + +=cut + +sub decode_string { + my ( $encoded, $decoded ) = @_; + + state @stack; + state %maps = map { $_ => ( undef, 'A' .. 'Z' )[ $_ ] } 1 .. 26; + + # Mandatory + $decoded = [] unless defined $decoded; + + foreach my $len ( 1 .. length $encoded ) { + my ( $alpha, $rest ) = ( substr( $encoded, 0, $len ), substr( $encoded, $len ) ); + + # Check if substr of length `$len' is an alphabet in the formal system + # of the encoded string. + if ( grep { $_ eq $alpha } keys %maps ) { + + next if length $rest == 1 and not exists $maps{$rest}; + if ( length $rest > 0 ) { + + # Instead of building a tree to later on perform a prefix search + # on it, stack the values and print the final results when + # length $rest == 0 + push @stack, $maps{$alpha}; + __SUB__->( $rest, $decoded ); + } + else { + push @$decoded, join '', @stack, $maps{$alpha}; + } + } + } + pop @stack; + + return $decoded; +} + +my $expected = [ + [ qw( AAAE AAO AKE KAE KO ) ], + [ qw( AA K ) ], + [ qw( ABG LG ) ], +]; + +cmp_deeply( + [ map { decode_string( $_ ) } qw(1115 11 127) ], + $expected, + "Did you decode them correctly?" +); + +done_testing( 1 ); -- cgit From e99dd2b37f5df5097e5deb7389163e334ac368af Mon Sep 17 00:00:00 2001 From: KUEPPO Date: Fri, 11 Nov 2022 21:59:28 +0100 Subject: Update doc --- challenge-190/kueppo-wesley/ch-2.pl | 75 +++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 challenge-190/kueppo-wesley/ch-2.pl diff --git a/challenge-190/kueppo-wesley/ch-2.pl b/challenge-190/kueppo-wesley/ch-2.pl new file mode 100644 index 0000000000..2546e0dbdb --- /dev/null +++ b/challenge-190/kueppo-wesley/ch-2.pl @@ -0,0 +1,75 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw(state current_sub); + +use Test::More; +use Test::Deep; + +=pod + +=head1 PROBLEM + +Given C and C, words from different encodings and C, a function +which transliterates the alphabets between these encodings. +The primary issue is that the results of composed alphabets are also alphabets. + +So, get all possible Cs from C. + +=head1 SOLUTION + +A possible solution to this problem can be to parse the encoded +string to build a tree on which a prefix search generates all the +possible Cs. + +=cut + +sub decode_string { + my ( $encoded, $decoded ) = @_; + + state @stack; + state %maps = map { $_ => ( undef, 'A' .. 'Z' )[ $_ ] } 1 .. 26; + + # Mandatory + $decoded = [] unless defined $decoded; + + foreach my $len ( 1 .. length $encoded ) { + my ( $alpha, $rest ) = ( substr( $encoded, 0, $len ), substr( $encoded, $len ) ); + + # Check if substr of length `$len' is an alphabet in the formal system + # of the encoded string. + if ( grep { $_ eq $alpha } keys %maps ) { + + next if length $rest == 1 and not exists $maps{$rest}; + if ( length $rest > 0 ) { + + # Instead of building a tree to later on perform a prefix search + # on it, stack the values and print the final results when + # length $rest == 0 + push @stack, $maps{$alpha}; + __SUB__->( $rest, $decoded ); + } + else { + push @$decoded, join '', @stack, $maps{$alpha}; + } + } + } + pop @stack; + + return $decoded; +} + +my $expected = [ + [ qw( AAAE AAO AKE KAE KO ) ], + [ qw( AA K ) ], + [ qw( ABG LG ) ], +]; + +cmp_deeply( + [ map { decode_string( $_ ) } qw(1115 11 127) ], + $expected, + "Did you decode them correctly?" +); + +done_testing( 1 ); -- cgit From 68630ce60585dd02c630c7f1eb8e5927ed94dfd3 Mon Sep 17 00:00:00 2001 From: KUEPPO Date: Fri, 11 Nov 2022 22:02:26 +0100 Subject: Delete ch-2.pl --- challenge-190/kueppo-wesley/ch-2.pl | 75 ------------------------------------- 1 file changed, 75 deletions(-) delete mode 100644 challenge-190/kueppo-wesley/ch-2.pl diff --git a/challenge-190/kueppo-wesley/ch-2.pl b/challenge-190/kueppo-wesley/ch-2.pl deleted file mode 100644 index 2546e0dbdb..0000000000 --- a/challenge-190/kueppo-wesley/ch-2.pl +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env perl - -use strict; -use warnings; -use feature qw(state current_sub); - -use Test::More; -use Test::Deep; - -=pod - -=head1 PROBLEM - -Given C and C, words from different encodings and C, a function -which transliterates the alphabets between these encodings. -The primary issue is that the results of composed alphabets are also alphabets. - -So, get all possible Cs from C. - -=head1 SOLUTION - -A possible solution to this problem can be to parse the encoded -string to build a tree on which a prefix search generates all the -possible Cs. - -=cut - -sub decode_string { - my ( $encoded, $decoded ) = @_; - - state @stack; - state %maps = map { $_ => ( undef, 'A' .. 'Z' )[ $_ ] } 1 .. 26; - - # Mandatory - $decoded = [] unless defined $decoded; - - foreach my $len ( 1 .. length $encoded ) { - my ( $alpha, $rest ) = ( substr( $encoded, 0, $len ), substr( $encoded, $len ) ); - - # Check if substr of length `$len' is an alphabet in the formal system - # of the encoded string. - if ( grep { $_ eq $alpha } keys %maps ) { - - next if length $rest == 1 and not exists $maps{$rest}; - if ( length $rest > 0 ) { - - # Instead of building a tree to later on perform a prefix search - # on it, stack the values and print the final results when - # length $rest == 0 - push @stack, $maps{$alpha}; - __SUB__->( $rest, $decoded ); - } - else { - push @$decoded, join '', @stack, $maps{$alpha}; - } - } - } - pop @stack; - - return $decoded; -} - -my $expected = [ - [ qw( AAAE AAO AKE KAE KO ) ], - [ qw( AA K ) ], - [ qw( ABG LG ) ], -]; - -cmp_deeply( - [ map { decode_string( $_ ) } qw(1115 11 127) ], - $expected, - "Did you decode them correctly?" -); - -done_testing( 1 ); -- cgit From e48a2bdff10a4b57981672058aa2ff036e89103e Mon Sep 17 00:00:00 2001 From: KUEPPO Date: Fri, 11 Nov 2022 22:03:10 +0100 Subject: Update ch-2.pl --- challenge-190/kueppo-wesley/Perl/ch-2.pl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/challenge-190/kueppo-wesley/Perl/ch-2.pl b/challenge-190/kueppo-wesley/Perl/ch-2.pl index 065874316c..2546e0dbdb 100644 --- a/challenge-190/kueppo-wesley/Perl/ch-2.pl +++ b/challenge-190/kueppo-wesley/Perl/ch-2.pl @@ -12,9 +12,10 @@ use Test::Deep; =head1 PROBLEM Given C and C, words from different encodings and C, a function -which transliterates the alphabets between these encodings. The -primary issue is that composed alphabets are also alphabets. +which transliterates the alphabets between these encodings. +The primary issue is that the results of composed alphabets are also alphabets. +So, get all possible Cs from C. =head1 SOLUTION -- cgit