diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-16 13:05:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-16 13:05:43 +0100 |
| commit | a360ebe0c50816029098c9c1c8883b457e63227b (patch) | |
| tree | 3d53a8f2358231418123256b2e9b2524b98365a8 | |
| parent | 5cfdaee1baa34c2882ffa096864ac271045e9a6e (diff) | |
| parent | a5da5d091bc987ef2cd89a641a473066b0b05cbb (diff) | |
| download | perlweeklychallenge-club-a360ebe0c50816029098c9c1c8883b457e63227b.tar.gz perlweeklychallenge-club-a360ebe0c50816029098c9c1c8883b457e63227b.tar.bz2 perlweeklychallenge-club-a360ebe0c50816029098c9c1c8883b457e63227b.zip | |
Merge pull request #2082 from LubosKolouch/chal_073_LK
LK Challenge 073 Perl and Python
| -rw-r--r-- | challenge-073/lubos-kolouch/perl/ch-1.pl | 54 | ||||
| -rw-r--r-- | challenge-073/lubos-kolouch/perl/ch-2.pl | 55 | ||||
| -rwxr-xr-x | challenge-073/lubos-kolouch/python/ch-1.py | 27 | ||||
| -rwxr-xr-x | challenge-073/lubos-kolouch/python/ch-2.py | 29 |
4 files changed, 165 insertions, 0 deletions
diff --git a/challenge-073/lubos-kolouch/perl/ch-1.pl b/challenge-073/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..5cf94156c6 --- /dev/null +++ b/challenge-073/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-073/ +# +# Min Sliding Window +# +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: Lubos Kolouch +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 16.8.2020 12:40 +# REVISION: --- +#=============================================================================== + +use strict; +use warnings; +use List::Util qw/min/; + +sub min_window { + my ($a_ref, $s) = @_; + + my @return_array; + + my $pos = 0; + while (1) { + my $last_index = min(scalar @$a_ref-1, $pos+$s); + + push @return_array, min(@$a_ref[$pos..$last_index]); + last if $last_index == scalar(@$a_ref)-1; + + $pos++; + } + + return \@return_array; +} + + +# TESTS + +use Test::More; + +is_deeply(\min_window([1, 5, 0, 2, 9, 3, 7, 6, 4, 8],3),\[0,0,0,2,3,3,4]); +is_deeply(\min_window([1, 2, 3],4),\[1]); + +done_testing; diff --git a/challenge-073/lubos-kolouch/perl/ch-2.pl b/challenge-073/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..c1638b658c --- /dev/null +++ b/challenge-073/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-2.pl +# +# USAGE: ./ch-2.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-073/ +# +# Smallest Neighbour +# +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: Lubos Kolouch +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 16.8.2020 13:40 +# REVISION: --- +#=============================================================================== + +use strict; +use warnings; + +sub get_smallest { + my $a_ref = shift; + + my @return_array = 0; + my $min = @$a_ref[0]; + + for my $i (1 .. scalar @$a_ref-1) { + + if ($min < $a_ref->[$i]) { + push @return_array, $min; + } else { + push @return_array, 0; + } + + $min = $a_ref->[$i] if @$a_ref[$i] < $min; + + } + + return \@return_array; +} + +# TESTS + +use Test::More; + +is_deeply(\get_smallest([7, 8, 3, 12, 10]),\[0, 7, 0, 3, 3]); +is_deeply(\get_smallest([4, 6, 5]),\[0, 4, 4]); + +done_testing; diff --git a/challenge-073/lubos-kolouch/python/ch-1.py b/challenge-073/lubos-kolouch/python/ch-1.py new file mode 100755 index 0000000000..2202e49cce --- /dev/null +++ b/challenge-073/lubos-kolouch/python/ch-1.py @@ -0,0 +1,27 @@ +#! /usr/bin/env python +""" Perl weekly challenge 073 + https://perlweeklychallenge.org/blog/perl-weekly-challenge-073/ Task 1 """ + + +def min_window(arr, s): + """ Return the min sliding window""" + + return_array = list() + + pos = 0 + + while 1: + last_index = min(len(arr)-1, pos + s) + + app_min = min(arr[pos:last_index]) + return_array.append(app_min) + + if last_index == len(arr)-1: + break + + pos += 1 + + return return_array + + +assert min_window([1, 5, 0, 2, 9, 3, 7, 6, 4, 8], 3) == [0, 0, 0, 2, 3, 3, 4] diff --git a/challenge-073/lubos-kolouch/python/ch-2.py b/challenge-073/lubos-kolouch/python/ch-2.py new file mode 100755 index 0000000000..ebf988621b --- /dev/null +++ b/challenge-073/lubos-kolouch/python/ch-2.py @@ -0,0 +1,29 @@ +#! /usr/bin/env python +""" Perl weekly challenge 073 + https://perlweeklychallenge.org/blog/perl-weekly-challenge-073/ Task 2 """ + + +def get_smallest(arr): + """Write a script to create an array that represents the + smallest element to the left of each corresponding index. + If none found then use 0. """ + + return_array = list() + return_array.append(0) + + my_min = arr[0] + + for i in range(1, len(arr)): + if my_min < arr[i]: + return_array.append(my_min) + else: + return_array.append(0) + + if arr[i] < my_min: + my_min = arr[i] + + return return_array + + +assert get_smallest([7, 8, 3, 12, 10]) == [0, 7, 0, 3, 3] +assert get_smallest([4, 6, 5]) == [0, 4, 4] |
