diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-10 21:45:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-10 21:45:42 +0000 |
| commit | 4cc2cab9d4786ce0810313fcc611c03a19b7cc76 (patch) | |
| tree | 2f0082749182121d1b2e63a77bc77745f73cfc89 | |
| parent | f243f79ae0fc6e3f6748ced34c42cf51e13f6009 (diff) | |
| parent | e2b3a9b9cbd927d33516c42b88ec16bf0aebcb58 (diff) | |
| download | perlweeklychallenge-club-4cc2cab9d4786ce0810313fcc611c03a19b7cc76.tar.gz perlweeklychallenge-club-4cc2cab9d4786ce0810313fcc611c03a19b7cc76.tar.bz2 perlweeklychallenge-club-4cc2cab9d4786ce0810313fcc611c03a19b7cc76.zip | |
Merge pull request #7700 from pauloscustodio/master
Add Perl, C, C++ and Forth solutions
| -rw-r--r-- | challenge-201/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-201/paulo-custodio/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-201/paulo-custodio/perl/ch-2.pl | 56 | ||||
| -rw-r--r-- | challenge-201/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-201/paulo-custodio/t/test-2.yaml | 5 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/basic/ch-1.bas | 62 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/basic/ch-2.bas | 88 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/c/ch-1.c | 64 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/c/ch-2.c | 86 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/cpp/ch-1.cpp | 61 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/cpp/ch-2.cpp | 85 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/forth/ch-1.fs | 69 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/forth/ch-2.fs | 86 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/perl/ch-2.pl | 59 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/t/test-1.yaml | 40 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/t/test-2.yaml | 30 |
18 files changed, 874 insertions, 0 deletions
diff --git a/challenge-201/paulo-custodio/Makefile b/challenge-201/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-201/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-201/paulo-custodio/perl/ch-1.pl b/challenge-201/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..9e463d4072 --- /dev/null +++ b/challenge-201/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl + +# Challenge 201 +# +# Task 1: Missing Numbers +# Submitted by: Mohammad S Anwar +# +# You are given an array of unique numbers. +# +# Write a script to find out all missing numbers in the range 0..$n where $n +# is the array size. +# +# Example 1 +# +# Input: @array = (0,1,3) +# Output: 2 +# +# The array size i.e. total element count is 3, so the range is 0..3. +# The missing number is 2 in the given array. +# +# Example 2 +# +# Input: @array = (0,1) +# Output: 2 +# +# The array size is 2, therefore the range is 0..2. +# The missing number is 2. + +use Modern::Perl; + +my @in = @ARGV; +my %in; $in{$_}=1 for @in; +say join(" ", grep {!$in{$_}} 0..@in); diff --git a/challenge-201/paulo-custodio/perl/ch-2.pl b/challenge-201/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..e4e9fef4b8 --- /dev/null +++ b/challenge-201/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +# Challenge 201 +# +# Task 2: Penny Piles +# Submitted by: Robbie Hatley +# +# You are given an integer, $n > 0. +# +# Write a script to determine the number of ways of putting $n pennies in a row +# of piles of ascending heights from left to right. +# Example +# +# Input: $n = 5 +# Output: 7 +# +# Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles: +# +# 1 1 1 1 1 +# 1 1 1 2 +# 1 2 2 +# 1 1 3 +# 2 3 +# 1 4 +# 5 + +use Modern::Perl; +no warnings 'recursion'; + +sub make_piles1 { + my($count, $prev, $n) = @_; + my @prev = @$prev; + if ($n < 0) { + } + elsif ($n == 0) { + #say "@prev"; + $$count++; + } + else { + my $max = @prev==0 ? $n : $prev[-1]; + for my $i (1..$max) { + make_piles1($count, [@prev, $i], $n-$i); + } + } +} + +sub make_piles { + my($n) = @_; + my $count = 0; + make_piles1(\$count, [], $n); + return $count; +} + +@ARGV==1 or die "usage: ch-2.pl n\n"; +my($n) = @ARGV; +say make_piles($n); diff --git a/challenge-201/paulo-custodio/t/test-1.yaml b/challenge-201/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..ddb6cf1c40 --- /dev/null +++ b/challenge-201/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 0 1 3 + input: + output: 2 +- setup: + cleanup: + args: 0 1 + input: + output: 2 diff --git a/challenge-201/paulo-custodio/t/test-2.yaml b/challenge-201/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..ed3c8af885 --- /dev/null +++ b/challenge-201/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 5 + input: + output: 7 diff --git a/challenge-202/paulo-custodio/Makefile b/challenge-202/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-202/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-202/paulo-custodio/basic/ch-1.bas b/challenge-202/paulo-custodio/basic/ch-1.bas new file mode 100644 index 0000000000..e3e827da1d --- /dev/null +++ b/challenge-202/paulo-custodio/basic/ch-1.bas @@ -0,0 +1,62 @@ +' Challenge 202 +' +' Task 1: Consecutive Odds +' Submitted by: Mohammad S Anwar +' +' You are given an array of integers. +' +' Write a script to print 1 if there are THREE consecutive odds in the given array +' otherwise print 0. +' +' Example 1 +' +' Input: @array = (1,5,3,6) +' Output: 1 +' +' Example 2 +' +' Input: @array = (2,6,3,5) +' Output: 0 +' +' Example 3 +' +' Input: @array = (1,2,3,4) +' Output: 0 +' +' Example 4 +' +' Input: @array = (2,3,5,7) +' Output: 1 + +sub collect_args(nums() as integer) + dim i as integer + i=0 + do while command(i+1)<>"" + redim preserve nums(i) as integer + nums(i)=val(command(i+1)) + i=i+1 + loop +end sub + +function is_odd(n as integer) as boolean + if n mod 2=0 then + is_odd=false + else + is_odd=true + end if +end function + +function three_consecutive_odds(nums() as integer) as integer + dim i as integer + for i=0 to ubound(nums)-2 + if is_odd(nums(i)) and is_odd(nums(i+1)) and is_odd(nums(i+2)) then + three_consecutive_odds=1 + exit function + end if + next + three_consecutive_odds=0 +end function + +dim nums() as integer +collect_args nums() +print three_consecutive_odds(nums()) diff --git a/challenge-202/paulo-custodio/basic/ch-2.bas b/challenge-202/paulo-custodio/basic/ch-2.bas new file mode 100644 index 0000000000..fe96444873 --- /dev/null +++ b/challenge-202/paulo-custodio/basic/ch-2.bas @@ -0,0 +1,88 @@ +' Challenge 202 +' +' Task 2: Widest Valley +' Submitted by: E. Choroba +' +' Given a profile as a list of altitudes, return the leftmost widest valley. +' A valley is defined as a subarray of the profile consisting of two parts: +' the first part is non-increasing and the second part is non-decreasing. +' Either part can be empty. +' +' Example 1 +' +' Input: 1, 5, 5, 2, 8 +' Output: 5, 5, 2, 8 +' +' Example 2 +' +' Input: 2, 6, 8, 5 +' Output: 2, 6, 8 +' +' Example 3 +' +' Input: 9, 8, 13, 13, 2, 2, 15, 17 +' Output: 13, 13, 2, 2, 15, 17 +' +' Example 4 +' +' Input: 2, 1, 2, 1, 3 +' Output: 2, 1, 2 +' +' Example 5 +' +' Input: 1, 3, 3, 2, 1, 2, 3, 3, 2 +' Output: 3, 3, 2, 1, 2, 3, 3 + +sub collect_args(nums() as integer) + dim i as integer + i=0 + do while command(i+1)<>"" + redim preserve nums(i) as integer + nums(i)=val(command(i+1)) + i=i+1 + loop +end sub + +sub largest_valey(heights() as integer, valey() as integer) + dim c as integer, l as integer, r as integer, i as integer + dim l1 as integer, r1 as integer + + l1=0: r1=-1 ' initial interval + for c=0 to ubound(heights) ' center of valey + + l=c ' left + do while l>0 + if heights(l-1)>=heights(l) then + l=l-1 + else + exit do + end if + loop + + r=c ' right + do while r<ubound(heights) + if heights(r+1)>=heights(r) then + r=r+1 + else + exit do + end if + loop + + if r-l+1>r1-l1+1 then + l1=l: r1=r + end if + next + + redim valey(r1-l1) + for i=0 to r1-l1 + valey(i)=heights(i+l1) + next +end sub + +dim heights() as integer, valey() as integer, i as integer +collect_args heights() +largest_valey heights(), valey() +for i=0 to ubound(valey) + print valey(i); +next +print diff --git a/challenge-202/paulo-custodio/c/ch-1.c b/challenge-202/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..dc8f770f46 --- /dev/null +++ b/challenge-202/paulo-custodio/c/ch-1.c @@ -0,0 +1,64 @@ +/* +Challenge 202 + +Task 1: Consecutive Odds +Submitted by: Mohammad S Anwar + +You are given an array of integers. + +Write a script to print 1 if there are THREE consecutive odds in the given array +otherwise print 0. + +Example 1 + +Input: @array = (1,5,3,6) +Output: 1 + +Example 2 + +Input: @array = (2,6,3,5) +Output: 0 + +Example 3 + +Input: @array = (1,2,3,4) +Output: 0 + +Example 4 + +Input: @array = (2,3,5,7) +Output: 1 +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> + +bool is_odd(int n) { + return n%2==1; +} + +int three_consecutive_odds(int nums[], int nums_size) { + for (int i=0; i < nums_size-2; i++) + if (is_odd(nums[i]) && is_odd(nums[i+1]) && is_odd(nums[i+2])) + return 1; + return 0; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc < 2) { + fputs("Usage: ch-1 nums...\n", stderr); + return EXIT_FAILURE; + } + + int nums_size = argc; + int* nums = malloc(nums_size * sizeof(int)); + for (int i = 0; i < nums_size; i++) + nums[i] = atoi(argv[i]); + + printf("%d\n", three_consecutive_odds(nums, nums_size)); + + free(nums); + return EXIT_SUCCESS; +} diff --git a/challenge-202/paulo-custodio/c/ch-2.c b/challenge-202/paulo-custodio/c/ch-2.c new file mode 100644 index 0000000000..09e61bdefe --- /dev/null +++ b/challenge-202/paulo-custodio/c/ch-2.c @@ -0,0 +1,86 @@ +/* +Challenge 202 + +Task 2: Widest Valley +Submitted by: E. Choroba + +Given a profile as a list of altitudes, return the leftmost widest valley. +A valley is defined as a subarray of the profile consisting of two parts: +the first part is non-increasing and the second part is non-decreasing. +Either part can be empty. + +Example 1 + +Input: 1, 5, 5, 2, 8 +Output: 5, 5, 2, 8 + +Example 2 + +Input: 2, 6, 8, 5 +Output: 2, 6, 8 + +Example 3 + +Input: 9, 8, 13, 13, 2, 2, 15, 17 +Output: 13, 13, 2, 2, 15, 17 + +Example 4 + +Input: 2, 1, 2, 1, 3 +Output: 2, 1, 2 + +Example 5 + +Input: 1, 3, 3, 2, 1, 2, 3, 3, 2 +Output: 3, 3, 2, 1, 2, 3, 3 +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int largest_valey(int heights[], int heights_size) { + int l1 = 0, r1 = -1; // initial interval + + // find largest valey + for (int c = 0; c < heights_size; c++) { // center of valey + + int l = c; // left + while (l-1 >= 0 && heights[l-1] >= heights[l]) + l--; + + int r = c; // right + while (r+1 < heights_size && heights[r+1] >= heights[r]) + r++; + + if (r - l + 1 > r1 - l1 + 1) { + l1 = l; + r1 = r; + } + } + + // return valey in heights array + memmove(heights, heights+l1, (r1-l1+1)*sizeof(int)); + return r1-l1+1; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc < 2) { + fputs("Usage: ch-2 height...\n", stderr); + return EXIT_FAILURE; + } + + int heights_size = argc; + int* heights = malloc(heights_size * sizeof(int)); + for (int i = 0; i < heights_size; i++) + heights[i] = atoi(argv[i]); + + int width = largest_valey(heights, heights_size); + for (int i = 0; i < width; i++) + printf("%d ", heights[i]); + printf("\n"); + + free(heights); + return EXIT_SUCCESS; +} diff --git a/challenge-202/paulo-custodio/cpp/ch-1.cpp b/challenge-202/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..cc4f1a7661 --- /dev/null +++ b/challenge-202/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,61 @@ +/* +Challenge 202 + +Task 1: Consecutive Odds +Submitted by: Mohammad S Anwar + +You are given an array of integers. + +Write a script to print 1 if there are THREE consecutive odds in the given array +otherwise print 0. + +Example 1 + +Input: @array = (1,5,3,6) +Output: 1 + +Example 2 + +Input: @array = (2,6,3,5) +Output: 0 + +Example 3 + +Input: @array = (1,2,3,4) +Output: 0 + +Example 4 + +Input: @array = (2,3,5,7) +Output: 1 +*/ + +#include <iostream> +#include <vector> + +bool is_odd(int n) { + return n%2==1; +} + +int three_consecutive_odds(std::vector<int> nums) { + for (int i = 0; i < static_cast<int>(nums.size()) - 2; i++) + if (is_odd(nums[i]) && is_odd(nums[i+1]) && is_odd(nums[i+2])) + return 1; + return 0; +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc < 2) { + std::cerr << "Usage: ch-1 nums..." << std::endl; + return EXIT_FAILURE; + } + + std::vector<int> nums; + for (int i = 0; i < argc; i++) + nums.push_back(atoi(argv[i])); + + std::cout << three_consecutive_odds(nums) << std::endl; + + return EXIT_SUCCESS; +} diff --git a/challenge-202/paulo-custodio/cpp/ch-2.cpp b/challenge-202/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..f5525a07ce --- /dev/null +++ b/challenge-202/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,85 @@ +/* +Challenge 202 + +Task 2: Widest Valley +Submitted by: E. Choroba + +Given a profile as a list of altitudes, return the leftmost widest valley. +A valley is defined as a subarray of the profile consisting of two parts: +the first part is non-increasing and the second part is non-decreasing. +Either part can be empty. + +Example 1 + +Input: 1, 5, 5, 2, 8 +Output: 5, 5, 2, 8 + +Example 2 + +Input: 2, 6, 8, 5 +Output: 2, 6, 8 + +Example 3 + +Input: 9, 8, 13, 13, 2, 2, 15, 17 +Output: 13, 13, 2, 2, 15, 17 + +Example 4 + +Input: 2, 1, 2, 1, 3 +Output: 2, 1, 2 + +Example 5 + +Input: 1, 3, 3, 2, 1, 2, 3, 3, 2 +Output: 3, 3, 2, 1, 2, 3, 3 +*/ + +#include <iostream> +#include <vector> + +void largest_valey(const std::vector<int>& heights, std::vector<int>& valey) { + int l1 = 0, r1 = -1; // initial interval + + // find largest valey + int heights_size = static_cast<int>(heights.size()); + for (int c = 0; c < heights_size; c++) { // center of valey + + int l = c; // left + while (l-1 >= 0 && heights[l-1] >= heights[l]) + l--; + + int r = c; // right + while (r+1 < heights_size && heights[r+1] >= heights[r]) + r++; + + if (r - l + 1 > r1 - l1 + 1) { + l1 = l; + r1 = r; + } + } + + // return valey in heights array + valey.resize(r1-l1+1); + memmove(&valey[0], &heights[l1], (r1-l1+1)*sizeof(int)); +} + +int main(int argc, char* argv[]) { + argv++; argc--; + if (argc < 2) { + fputs("Usage: ch-2 height...\n", stderr); + return EXIT_FAILURE; + } + + std::vector<int> heights; + for (int i = 0; i < argc; i++) + heights.push_back(atoi(argv[i])); + + std::vector<int> valey; + largest_valey(heights, valey); + for (size_t i = 0; i < valey.size(); i++) + std::cout << valey[i] << " "; + std::cout << std::endl; + + return EXIT_SUCCESS; +} diff --git a/challenge-202/paulo-custodio/forth/ch-1.fs b/challenge-202/paulo-custodio/forth/ch-1.fs new file mode 100644 index 0000000000..5a2a5c81c8 --- /dev/null +++ b/challenge-202/paulo-custodio/forth/ch-1.fs @@ -0,0 +1,69 @@ +#! /usr/bin/env gforth + +\ Challenge 202 +\ +\ Task 1: Consecutive Odds +\ Submitted by: Mohammad S Anwar +\ +\ You are given an array of integers. +\ +\ Write a script to print 1 if there are THREE consecutive odds in the given array +\ otherwise print 0. +\ +\ Example 1 +\ +\ Input: @array = (1,5,3,6) +\ Output: 1 +\ +\ Example 2 +\ +\ Input: @array = (2,6,3,5) +\ Output: 0 +\ +\ Example 3 +\ +\ Input: @array = (1,2,3,4) +\ Output: 0 +\ +\ Example 4 +\ +\ Input: @array = (2,3,5,7) +\ Output: 1 + +CREATE nums 256 CELLS ALLOT +0 VALUE nums_size + +: nums[] ( i -- addr ) + CELLS nums + +; + +: collect_args ( -- ) + BEGIN NEXT-ARG DUP WHILE + 0 0 2SWAP >NUMBER 2DROP DROP + nums_size nums[] ! + nums_size 1+ TO nums_size + REPEAT + 2DROP +; + +: is_odd ( n -- f ) + 2 MOD 1 = +; + +: three_consecutive_odds ( -- n ) + 0 + nums_size 3 >= IF + nums_size 2 - 0 DO + I nums[] @ is_odd + I 1+ nums[] @ is_odd AND + I 2 + nums[] @ is_odd AND + IF + DROP 1 + LEAVE + THEN + LOOP + THEN +; + +collect_args three_consecutive_odds . CR +BYE diff --git a/challenge-202/paulo-custodio/forth/ch-2.fs b/challenge-202/paulo-custodio/forth/ch-2.fs new file mode 100644 index 0000000000..8016c1f1f3 --- /dev/null +++ b/challenge-202/paulo-custodio/forth/ch-2.fs @@ -0,0 +1,86 @@ +#! /usr/bin/env gforth + +\ Challenge 202 +\ +\ Task 2: Widest Valley +\ Submitted by: E. Choroba +\ +\ Given a profile as a list of altitudes, return the leftmost widest valley. +\ A valley is defined as a subarray of the profile consisting of two parts: +\ the first part is non-increasing and the second part is non-decreasing. +\ Either part can be empty. +\ +\ Example 1 +\ +\ Input: 1, 5, 5, 2, 8 +\ Output: 5, 5, 2, 8 +\ +\ Example 2 +\ +\ Input: 2, 6, 8, 5 +\ Output: 2, 6, 8 +\ +\ Example 3 +\ +\ Input: 9, 8, 13, 13, 2, 2, 15, 17 +\ Output: 13, 13, 2, 2, 15, 17 +\ +\ Example 4 +\ +\ Input: 2, 1, 2, 1, 3 +\ Output: 2, 1, 2 +\ +\ Example 5 +\ +\ Input: 1, 3, 3, 2, 1, 2, 3, 3, 2 +\ Output: 3, 3, 2, 1, 2, 3, 3 + +CREATE nums 256 CELLS ALLOT +0 VALUE nums_size + +: nums[] ( i -- addr ) + CELLS nums + +; + +: collect_args ( -- ) + BEGIN NEXT-ARG DUP WHILE + 0 0 2SWAP >NUMBER 2DROP DROP + nums_size nums[] ! + nums_size 1+ TO nums_size + REPEAT + 2DROP +; + +: largest_valey ( -- r+1 l ) + 0 -1 { l1 r1 } \ initial interval + + \ find largest valey + nums_size 0 ?DO \ center of valey + I I { l r } \ left right + + BEGIN l 1- 0 >= l 1- nums[] @ l nums[] @ >= AND WHILE + l 1- TO l + REPEAT + + BEGIN r 1+ nums_size < r 1+ nums[] @ r nums[] @ >= AND WHILE + r 1+ TO r + REPEAT + + r l - r1 l1 - > IF + l TO l1 + r TO r1 + THEN + LOOP + + r1 1+ l1 +; + +: task + collect_args largest_valey ?DO + i nums[] @ . + LOOP + CR +; + +task +BYE diff --git a/challenge-202/paulo-custodio/perl/ch-1.pl b/challenge-202/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..fc90ac398b --- /dev/null +++ b/challenge-202/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +# Challenge 202 +# +# Task 1: Consecutive Odds +# Submitted by: Mohammad S Anwar +# +# You are given an array of integers. +# +# Write a script to print 1 if there are THREE consecutive odds in the given array +# otherwise print 0. +# +# Example 1 +# +# Input: @array = (1,5,3,6) +# Output: 1 +# +# Example 2 +# +# Input: @array = (2,6,3,5) +# Output: 0 +# +# Example 3 +# +# Input: @array = (1,2,3,4) +# Output: 0 +# +# Example 4 +# +# Input: @array = (2,3,5,7) +# Output: 1 + +use Modern::Perl; +my @in = @ARGV; +my $s = join('', map {($_%2)==0 ? "0" : "1"} @in); +say $s =~ /111/ ? 1 : 0; diff --git a/challenge-202/paulo-custodio/perl/ch-2.pl b/challenge-202/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..7e0fa515f3 --- /dev/null +++ b/challenge-202/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +# Challenge 202 +# +# Task 2: Widest Valley +# Submitted by: E. Choroba +# +# Given a profile as a list of altitudes, return the leftmost widest valley. +# A valley is defined as a subarray of the profile consisting of two parts: +# the first part is non-increasing and the second part is non-decreasing. +# Either part can be empty. +# +# Example 1 +# +# Input: 1, 5, 5, 2, 8 +# Output: 5, 5, 2, 8 +# +# Example 2 +# +# Input: 2, 6, 8, 5 +# Output: 2, 6, 8 +# +# Example 3 +# +# Input: 9, 8, 13, 13, 2, 2, 15, 17 +# Output: 13, 13, 2, 2, 15, 17 +# +# Example 4 +# +# Input: 2, 1, 2, 1, 3 +# Output: 2, 1, 2 +# +# Example 5 +# +# Input: 1, 3, 3, 2, 1, 2, 3, 3, 2 +# Output: 3, 3, 2, 1, 2, 3, 3 + +use Modern::Perl; + +sub largest_valey { + my(@in) = @_; + my @valey; + for my $c (0 .. $#in) { # center of valey + my $l = $c; # left + while ($l > 0 && $in[$l-1] >= $in[$l]) { + $l--; + } + my $r = $c; # right + while ($r < $#in && $in[$r+1] >= $in[$r]) { + $r++; + } + if ($r-$l+1 > @valey) { + @valey = @in[$l..$r]; + } + } + return @valey; +} + +say join(" ", largest_valey(@ARGV)); diff --git a/challenge-202/paulo-custodio/t/test-1.yaml b/challenge-202/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..bb6fcbaef9 --- /dev/null +++ b/challenge-202/paulo-custodio/t/test-1.yaml @@ -0,0 +1,40 @@ +- setup: + cleanup: + args: 1 5 3 6 + input: + output: 1 +- setup: + cleanup: + args: 2 6 3 5 + input: + output: 0 +- setup: + cleanup: + args: 1 2 3 4 + input: + output: 0 +- setup: + cleanup: + args: 2 3 5 7 + input: + output: 1 +- setup: + cleanup: + args: 2 4 + input: + output: 0 +- setup: + cleanup: + args: 2 4 6 + input: + output: 0 +- setup: + cleanup: + args: 1 3 + input: + output: 0 +- setup: + cleanup: + args: 1 3 5 + input: + output: 1 diff --git a/challenge-202/paulo-custodio/t/test-2.yaml b/challenge-202/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..4e12b89ab2 --- /dev/null +++ b/challenge-202/paulo-custodio/t/test-2.yaml @@ -0,0 +1,30 @@ +- setup: + cleanup: + args: 1 5 5 2 8 + input: + output: 5 5 2 8 +- setup: + cleanup: + args: 2 6 8 5 + input: + output: 2 6 8 +- setup: + cleanup: + args: 9 8 13 13 2 2 15 17 + input: + output: 13 13 2 2 15 17 +- setup: + cleanup: + args: 2 1 2 1 3 + input: + output: 2 1 2 +- setup: + cleanup: + args: 1 3 3 2 1 2 3 3 2 + input: + output: 3 3 2 1 2 3 3 +- setup: + cleanup: + args: 4 1 5 5 2 3 8 + input: + output: 5 5 2 3 8 |
