From a55eab1183897ff96f579151f73435083de8b3f5 Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Sun, 26 Sep 2021 02:39:40 +0800 Subject: Week 131 --- challenge-131/cheok-yin-fung/julia/ch-1.jl | 34 +++++ challenge-131/cheok-yin-fung/perl/ch-1.pl | 65 ++++++++++ challenge-131/cheok-yin-fung/perl/ch-2.pl | 196 +++++++++++++++++++++++++++++ challenge-131/julia/ch-1.jl | 34 ----- challenge-131/perl/ch-1.pl | 65 ---------- 5 files changed, 295 insertions(+), 99 deletions(-) create mode 100644 challenge-131/cheok-yin-fung/julia/ch-1.jl create mode 100644 challenge-131/cheok-yin-fung/perl/ch-1.pl create mode 100644 challenge-131/cheok-yin-fung/perl/ch-2.pl delete mode 100644 challenge-131/julia/ch-1.jl delete mode 100644 challenge-131/perl/ch-1.pl diff --git a/challenge-131/cheok-yin-fung/julia/ch-1.jl b/challenge-131/cheok-yin-fung/julia/ch-1.jl new file mode 100644 index 0000000000..cd8259e8a7 --- /dev/null +++ b/challenge-131/cheok-yin-fung/julia/ch-1.jl @@ -0,0 +1,34 @@ +tests = [ + [1, 2, 3, 6, 7, 8, 9], + [11, 12, 14, 17, 18, 19], + [2, 4, 6, 8], + [1, 2, 3, 4, 5], +] + + + +function consecutive(a) + list = [ [ a[1] ], ] + for i = 2:length(a) + if a[i] == a[i-1]+1 + push!(list[end], a[i]) + else + push!(list, [ a[i] ]) + end + end + return list +end + + + +for a in tests + println(consecutive(a)) +end + + + +# julia> include("ch-1.jl"); +# [[1, 2, 3], [6, 7, 8, 9]] +# [[11, 12], [14], [17, 18, 19]] +# [[2], [4], [6], [8]] +# [[1, 2, 3, 4, 5]] diff --git a/challenge-131/cheok-yin-fung/perl/ch-1.pl b/challenge-131/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..dc172cbfa7 --- /dev/null +++ b/challenge-131/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl +# The Weekly Challenge 131 +# Task 1 Consecutive Arrays +# Usage: ch-1.pl @array +use warnings; +use v5.24.0; +use Data::Dumper; +use Test::More tests => 4; +use Test::Deep; + +my @arr = (1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 15); +@arr = @ARGV if defined($ARGV[0]); + +# $Data::Dumper::Indent = 0; +# $Data::Dumper::Terse = 1; +# say Dumper @{consec(@arr)}; +# DEFAULT OUTPUT: [1,2,3][5,6,7][9,10,11][13][15] + +my @ans_str; +for (@{consec(@arr)}) { + push @ans_str, "[". (join ", ", @{$_}) . "]"; +} +say join ", ", @ans_str; + + + +sub consec { + my @a = @_; + my @list = ([ $a[0] ],); + for my $i (1..$#a) { + if ($a[$i] == $a[$i-1] + 1) { + push $list[-1]->@*, $a[$i]; + } + else { + push @list, [$a[$i]]; + } + } + return \@list; +} + + + +cmp_deeply( + consec(1, 2, 3, 6, 7, 8, 9), + [[1, 2, 3], [6, 7, 8, 9]], + "Example 1" +); + +cmp_deeply( + consec(11, 12, 14, 17, 18, 19), + [[11, 12], [14], [17, 18, 19]], + "Example 2" +); + +cmp_deeply( + consec(2, 4, 6, 8), + [( [2],[4],[6],[8] )], + "Example 3" +); + +cmp_deeply( + consec(1, 2, 3, 4, 5), + [[1, 2, 3, 4, 5]], + "Example 4" +); diff --git a/challenge-131/cheok-yin-fung/perl/ch-2.pl b/challenge-131/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..e504deaa70 --- /dev/null +++ b/challenge-131/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,196 @@ +#!/usr/bin/perl +# The Weekly Challenge 131 +# TasK 2 Find Pairs +# Usage: $ ch-2.pl delimiters_string search_string +# Example: $ ch-2.pl '()**{}[]' '({Hello *World*})' +no warnings; +use v5.24.0; +use List::Util qw/any pairmap/; +use Data::Dumper; + +my @open_uni = qw/ ( [ { < /; +my @close_uni = qw/ ) ] } > /; +my %partner = ( ')'=>'(', ']'=>'[', '}'=>'{', '>' => '<' ); +my @neutral_uni = qw{ , . ? ! / \ }; +push @neutral_uni, qw{ @ # $ & + - ^ * % " ' }; # dangerous zone + +my $d = '()**'; +my $str = '(*Hello*)(*World*)'; + +if (defined($ARGV[1])) { + $d = $ARGV[0]; + $str = $ARGV[1]; +} + +find_pair( [pairmap {$a.$b} (split //, $d)] , $str); + +say "========================================="; + +say "Example 1"; +find_pair( + [qw' "" [] () '], + '"I like (parens) and the Apple ][+" they said.' +); + +say "Example 2"; +find_pair( + [qw' ** // <> '], + '/* This is a comment (in some languages) */ ' +); + + +say "Test Case 1a"; +find_pair( + [qw' () {} '], + 'for ($a..$b) {do_sth;}' +); + +say "Test Case 1b"; +find_pair( + [qw' () {} '], + 'for ($a..$b} (cannot_do_sth;}' +); + +say "Test Case 2a"; +find_pair( + [qw' () {} ** '], + '( ilovePerl()*) {bad;}' +); + +say "Test Case 2b"; +find_pair( + [qw' () {} ** '], + '(*ilovePerl()*) {good;}' +); + +say "Test Case 3"; +find_pair( + ["<>"], + 'HELLO' +); + + +say "Test Case 4a"; +find_pair( + [qw{ ** // \\\\ }], + '*/layer/*' +); + +say "Test Case 4b"; +find_pair( + [qw{ ** // \\\\ }], + '*/wrong layer*/' +); + + + +sub find_pair { + my %open_found; + my %close_found; + my %neutral_found; + my @char = split //, $_[1]; + my @delimiters = split //, (join "", $_[0]->@*); + + for my $pair (@{$_[0]}) { + my $s_head = substr($pair, 0, 1); + my $s_tail = substr($pair, 1, 1); + $open_found{$s_head} = [] + if any { $_ eq $s_head } (map {substr($_, 0, 1)} @open_uni); + $close_found{$s_tail} = [] + if any { $_ eq $s_tail } (map {substr($_, -1, 1)} @close_uni); + $neutral_found{$s_head} = [] + if any { $_ eq $s_head } (map {substr($_, 0, 1)} @neutral_uni); + } + + for my $i (0..$#char) { + my $c = $char[$i]; + push $open_found{$c}->@*, $i if defined($open_found{$c}); + push $close_found{$c}->@*, $i if defined($close_found{$c}); + push $neutral_found{$c}->@*, $i if defined($neutral_found{$c}); + } + + my @open_positions; + my @close_positions; + my @all_positions; + + for (values %open_found, values %neutral_found) { + push @open_positions, $_->@*; + } + for (values %close_found, values %neutral_found) { + push @close_positions, $_->@*; + } + for (values %open_found, values %close_found, values %neutral_found) { + push @all_positions, $_->@*; + } + + say "Delimiters : ", @delimiters; + say "Search String: ", $_[1]; + @open_positions = sort {$a<=>$b} @open_positions; + @close_positions = sort {$a<=>$b} @close_positions; + say " ", join "", map {$char[$_]} @open_positions; + say " ", join "", map {$char[$_]} @close_positions; + + + @all_positions = sort {$a<=>$b} @all_positions; + my @stack; + my @waiting_to_be_closed; # variable for warning message + my $early_warn; # variable for warning message + for my $p (@all_positions) { + my $c = $char[$p]; + if (defined($open_found{$c})) { + push @stack, $c; + push @waiting_to_be_closed, $p; + } + if (defined($close_found{$c})) { + if (scalar @stack > 0 && $stack[-1] eq $partner{$c}) { + pop @stack; + pop @waiting_to_be_closed; + } + elsif (!defined($neutral_found{$c})) { + say("Warning: $stack[-1] at position ", $waiting_to_be_closed[-1], + " does not close appropriately."); + say("Warning: $c at position ", $p, + " may not be correspond to an opening delimiter."); + $early_warn = 1; + last; + } + else { + say("Warning: $stack[-1] at position ", $waiting_to_be_closed[-1], + " does not open or close appropriately." ); + $early_warn = 1; + last; + } + } + if (defined($neutral_found{$c})) { + if (scalar @stack == 0 || $stack[-1] ne $c) { + push @stack, $c; + push @waiting_to_be_closed, $p; + } + elsif (scalar @stack != 0 && $stack[-1] eq $c) { + pop @stack; + pop @waiting_to_be_closed; + } + else { + say("Warning: $stack[-1] at position ", $p, + " does not open or close appropriately." ); + $early_warn = 1; + last; + } + } + # say "Pos: ", $p; + # say "STACK: ", @stack; + # say "WAITING TO BE CLOSED: ", "@waiting_to_be_closed"; + } + if (!$early_warn && scalar @stack != 0 && !defined($close_found{$stack[-1]}) ) { + say "Warning: delimiter(s) do not open or close appropriately:"; + say "Delimiters: @stack"; + say "Positions: ", "@waiting_to_be_closed"; + } + if ($early_warn) { + say "Feedback: It is recommended that you check other delimiters as well."; + } + + + say ""; + return; +} diff --git a/challenge-131/julia/ch-1.jl b/challenge-131/julia/ch-1.jl deleted file mode 100644 index cd8259e8a7..0000000000 --- a/challenge-131/julia/ch-1.jl +++ /dev/null @@ -1,34 +0,0 @@ -tests = [ - [1, 2, 3, 6, 7, 8, 9], - [11, 12, 14, 17, 18, 19], - [2, 4, 6, 8], - [1, 2, 3, 4, 5], -] - - - -function consecutive(a) - list = [ [ a[1] ], ] - for i = 2:length(a) - if a[i] == a[i-1]+1 - push!(list[end], a[i]) - else - push!(list, [ a[i] ]) - end - end - return list -end - - - -for a in tests - println(consecutive(a)) -end - - - -# julia> include("ch-1.jl"); -# [[1, 2, 3], [6, 7, 8, 9]] -# [[11, 12], [14], [17, 18, 19]] -# [[2], [4], [6], [8]] -# [[1, 2, 3, 4, 5]] diff --git a/challenge-131/perl/ch-1.pl b/challenge-131/perl/ch-1.pl deleted file mode 100644 index dc172cbfa7..0000000000 --- a/challenge-131/perl/ch-1.pl +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/perl -# The Weekly Challenge 131 -# Task 1 Consecutive Arrays -# Usage: ch-1.pl @array -use warnings; -use v5.24.0; -use Data::Dumper; -use Test::More tests => 4; -use Test::Deep; - -my @arr = (1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 15); -@arr = @ARGV if defined($ARGV[0]); - -# $Data::Dumper::Indent = 0; -# $Data::Dumper::Terse = 1; -# say Dumper @{consec(@arr)}; -# DEFAULT OUTPUT: [1,2,3][5,6,7][9,10,11][13][15] - -my @ans_str; -for (@{consec(@arr)}) { - push @ans_str, "[". (join ", ", @{$_}) . "]"; -} -say join ", ", @ans_str; - - - -sub consec { - my @a = @_; - my @list = ([ $a[0] ],); - for my $i (1..$#a) { - if ($a[$i] == $a[$i-1] + 1) { - push $list[-1]->@*, $a[$i]; - } - else { - push @list, [$a[$i]]; - } - } - return \@list; -} - - - -cmp_deeply( - consec(1, 2, 3, 6, 7, 8, 9), - [[1, 2, 3], [6, 7, 8, 9]], - "Example 1" -); - -cmp_deeply( - consec(11, 12, 14, 17, 18, 19), - [[11, 12], [14], [17, 18, 19]], - "Example 2" -); - -cmp_deeply( - consec(2, 4, 6, 8), - [( [2],[4],[6],[8] )], - "Example 3" -); - -cmp_deeply( - consec(1, 2, 3, 4, 5), - [[1, 2, 3, 4, 5]], - "Example 4" -); -- cgit