From cb9f5d2247cec6070eea3595ab2dc1e785fb03d3 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 21 Oct 2021 15:55:51 +0100 Subject: Add Perl and Python solutions to challenge 131 --- challenge-131/paulo-custodio/perl/ch-1.pl | 43 ++++++++++++++++++++++++ challenge-131/paulo-custodio/perl/ch-2.pl | 51 +++++++++++++++++++++++++++++ challenge-131/paulo-custodio/python/ch-1.py | 37 +++++++++++++++++++++ challenge-131/paulo-custodio/python/ch-2.py | 43 ++++++++++++++++++++++++ challenge-131/paulo-custodio/t/test-1.yaml | 20 +++++++++++ challenge-131/paulo-custodio/t/test-2.yaml | 18 ++++++++++ challenge-131/paulo-custodio/test.pl | 4 +++ 7 files changed, 216 insertions(+) create mode 100644 challenge-131/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-131/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-131/paulo-custodio/python/ch-1.py create mode 100644 challenge-131/paulo-custodio/python/ch-2.py create mode 100644 challenge-131/paulo-custodio/t/test-1.yaml create mode 100644 challenge-131/paulo-custodio/t/test-2.yaml create mode 100644 challenge-131/paulo-custodio/test.pl diff --git a/challenge-131/paulo-custodio/perl/ch-1.pl b/challenge-131/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..544735f33e --- /dev/null +++ b/challenge-131/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# TASK #1 > Consecutive Arrays +# Submitted by: Mark Anderson +# You are given a sorted list of unique positive integers. +# +# Write a script to return list of arrays where the arrays are consecutive +# integers. +# +# Example 1: +# Input: (1, 2, 3, 6, 7, 8, 9) +# Output: ([1, 2, 3], [6, 7, 8, 9]) +# Example 2: +# Input: (11, 12, 14, 17, 18, 19) +# Output: ([11, 12], [14], [17, 18, 19]) +# Example 3: +# Input: (2, 4, 6, 8) +# Output: ([2], [4], [6], [8]) +# Example 4: +# Input: (1, 2, 3, 4, 5) +# Output: ([1, 2, 3, 4, 5]) + +use Modern::Perl; +use Data::Dump 'dump'; + +my @input = @ARGV; +my @output = cons_arrays(@input); +say "[".join(", ", map {"[".join(", ", @$_)."]"} @output)."]"; + +sub cons_arrays { + my(@input) = @_; + my @output = [shift @input]; + while (@input) { + my $n = shift @input; + if ($n == $output[-1][-1] + 1) { + push @{$output[-1]}, $n; + } + else { + push @output, [$n]; + } + } + return @output; +} diff --git a/challenge-131/paulo-custodio/perl/ch-2.pl b/challenge-131/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..d37470a793 --- /dev/null +++ b/challenge-131/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +# TASK #2 > Find Pairs +# Submitted by: Yary +# You are given a string of delimiter pairs and a string to search. +# +# Write a script to return two strings, the first with any characters +# matching the "opening character" set, the second with any matching +# the "closing character" set. +# +# Example 1: +# Input: +# Delimiter pairs: ""[]() +# Search String: "I like (parens) and the Apple ][+" they said. +# +# Output: +# "([" +# ")]" +# Example 2: +# Input: +# Delimiter pairs: **//<> +# Search String: /* This is a comment (in some languages) */ +# +# Output: +# /**/< +# /**/> + +use Modern::Perl; + +my $delims = <>; +my $string = <>; + +my $open_delims = "["; +my $close_delims = "["; +while (length($delims) >= 2) { + $open_delims .= "\\".substr($delims,0,1); + $close_delims .= "\\".substr($delims,1,1); + $delims = substr($delims,2); +} +$open_delims .= "]"; +$close_delims .= "]"; + +my $open_string; +my $close_string; +for my $c (split //, $string) { + $open_string .= $c if $c =~ /$open_delims/; + $close_string .= $c if $c =~ /$close_delims/; +} + +say $open_string; +say $close_string; diff --git a/challenge-131/paulo-custodio/python/ch-1.py b/challenge-131/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..3f047245c9 --- /dev/null +++ b/challenge-131/paulo-custodio/python/ch-1.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 + +# TASK #1 > Consecutive Arrays +# Submitted by: Mark Anderson +# You are given a sorted list of unique positive integers. +# +# Write a script to return list of arrays where the arrays are consecutive +# integers. +# +# Example 1: +# Input: (1, 2, 3, 6, 7, 8, 9) +# Output: ([1, 2, 3], [6, 7, 8, 9]) +# Example 2: +# Input: (11, 12, 14, 17, 18, 19) +# Output: ([11, 12], [14], [17, 18, 19]) +# Example 3: +# Input: (2, 4, 6, 8) +# Output: ([2], [4], [6], [8]) +# Example 4: +# Input: (1, 2, 3, 4, 5) +# Output: ([1, 2, 3, 4, 5]) + +import sys + +def cons_arrays(input): + output = [[input.pop(0)]] + while len(input) > 0: + n = input.pop(0) + if n == output[-1][-1] + 1: + output[-1].append(n) + else: + output.append([n]) + return output + +input = [int(x) for x in sys.argv[1:]] +output = cons_arrays(input) +print(output) \ No newline at end of file diff --git a/challenge-131/paulo-custodio/python/ch-2.py b/challenge-131/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..52fe765741 --- /dev/null +++ b/challenge-131/paulo-custodio/python/ch-2.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +# TASK #2 > Find Pairs +# Submitted by: Yary +# You are given a string of delimiter pairs and a string to search. +# +# Write a script to return two strings, the first with any characters +# matching the "opening character" set, the second with any matching +# the "closing character" set. +# +# Example 1: +# Input: +# Delimiter pairs: ""[]() +# Search String: "I like (parens) and the Apple ][+" they said. +# +# Output: +# "([" +# ")]" +# Example 2: +# Input: +# Delimiter pairs: **//<> +# Search String: /* This is a comment (in some languages) */ +# +# Output: +# /**/< +# /**/> + +delims = input() +string = input() + +open_delims = delims[0::2] +close_delims = delims[1::2] + +open_string = "" +close_string = "" +for c in string: + if c in open_delims: + open_string += c + if c in close_delims: + close_string += c + +print(open_string) +print(close_string) diff --git a/challenge-131/paulo-custodio/t/test-1.yaml b/challenge-131/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..dff99ecb49 --- /dev/null +++ b/challenge-131/paulo-custodio/t/test-1.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 1 2 3 6 7 8 9 + input: + output: [[1, 2, 3], [6, 7, 8, 9]] +- setup: + cleanup: + args: 11 12 14 17 18 19 + input: + output: [[11, 12], [14], [17, 18, 19]] +- setup: + cleanup: + args: 2 4 6 8 + input: + output: [[2], [4], [6], [8]] +- setup: + cleanup: + args: 1 2 3 4 5 + input: + output: [[1, 2, 3, 4, 5]] diff --git a/challenge-131/paulo-custodio/t/test-2.yaml b/challenge-131/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..9fc70d6e9a --- /dev/null +++ b/challenge-131/paulo-custodio/t/test-2.yaml @@ -0,0 +1,18 @@ +- setup: + cleanup: + args: + input: | + ""[]() + "I like (parens) and the Apple ][+" they said. + output: | + "([" + ")]" +- setup: + cleanup: + args: + input: | + **//<> + /* This is a comment (in some languages) */ + output: | + /**/< + /**/> diff --git a/challenge-131/paulo-custodio/test.pl b/challenge-131/paulo-custodio/test.pl new file mode 100644 index 0000000000..ba6c37260b --- /dev/null +++ b/challenge-131/paulo-custodio/test.pl @@ -0,0 +1,4 @@ +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; +require '../../challenge-001/paulo-custodio/test.pl'; -- cgit