diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-08-08 11:50:37 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-08-08 11:50:37 +0200 |
| commit | ec6da3d64ef535445c423ac7fd687d7074df0d79 (patch) | |
| tree | 1b62d497dbf11090b9732a0897eb502e92b713a0 | |
| parent | 267f197dc9dd52a0a7dac2d59bccc642abe542a0 (diff) | |
| download | perlweeklychallenge-club-ec6da3d64ef535445c423ac7fd687d7074df0d79.tar.gz perlweeklychallenge-club-ec6da3d64ef535445c423ac7fd687d7074df0d79.tar.bz2 perlweeklychallenge-club-ec6da3d64ef535445c423ac7fd687d7074df0d79.zip | |
feat(challenge-196/lubos-kolouch/perl,python/): Challenge 196 LK Perl Python
| -rw-r--r-- | challenge-196/lubos-kolouch/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-196/lubos-kolouch/perl/ch-2.pl | 35 | ||||
| -rw-r--r-- | challenge-196/lubos-kolouch/python/ch-1.py | 29 | ||||
| -rw-r--r-- | challenge-196/lubos-kolouch/python/ch-2.py | 30 |
4 files changed, 123 insertions, 0 deletions
diff --git a/challenge-196/lubos-kolouch/perl/ch-1.pl b/challenge-196/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..36404c8783 --- /dev/null +++ b/challenge-196/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,29 @@ +use strict; +use warnings; +use Test::More; + +sub pattern_132 { + my @list = @_; + my $n = scalar @list; + + # Iterate through the list and try to find the pattern + for my $i ( 0 .. $n - 3 ) { + for my $j ( $i + 1 .. $n - 2 ) { + for my $k ( $j + 1 .. $n - 1 ) { + if ( $list[$i] < $list[$k] && $list[$k] < $list[$j] ) { + return ( $list[$i], $list[$j], $list[$k] ); + } + } + } + } + + return (); +} + +# Test cases +is_deeply( [ pattern_132( 3, 1, 4, 2 ) ], [ 1, 4, 2 ], 'Example 1' ); +is_deeply( [ pattern_132( 1, 2, 3, 4 ) ], [], 'Example 2' ); +is_deeply( [ pattern_132( 1, 3, 2, 4, 6, 5 ) ], [ 1, 3, 2 ], 'Example 3' ); +is_deeply( [ pattern_132( 1, 3, 4, 2 ) ], [ 1, 3, 2 ], 'Example 4' ); + +done_testing(); diff --git a/challenge-196/lubos-kolouch/perl/ch-2.pl b/challenge-196/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..d29b10a621 --- /dev/null +++ b/challenge-196/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,35 @@ +use strict; +use warnings; +use Test::More; + +sub find_ranges { + my @array = @_; + my @ranges; + my ( $start, $end ); + + for my $i ( 0 .. $#array ) { + if ( !defined $start ) { + $start = $array[$i]; + $end = $array[$i]; + } + elsif ( $array[$i] == $end + 1 ) { + $end = $array[$i]; + } + else { + push @ranges, [ $start, $end ] if $end > $start; + $start = $array[$i]; + $end = $array[$i]; + } + } + + push @ranges, [ $start, $end ] if defined $start && $end > $start; + + return @ranges; +} + +# Test cases +is_deeply( [ find_ranges( 1, 3, 4, 5, 7 ) ], [ [ 3, 5 ] ], 'Example 1' ); +is_deeply( [ find_ranges( 1, 2, 3, 6, 7, 9 ) ], [ [ 1, 3 ], [ 6, 7 ] ], 'Example 2' ); +is_deeply( [ find_ranges( 0, 1, 2, 4, 5, 6, 8, 9 ) ], [ [ 0, 2 ], [ 4, 6 ], [ 8, 9 ] ], 'Example 3' ); + +done_testing(); diff --git a/challenge-196/lubos-kolouch/python/ch-1.py b/challenge-196/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..2a53fbee40 --- /dev/null +++ b/challenge-196/lubos-kolouch/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def find_pattern_132(nums: List[int]) -> List[int]: + + for i in range(len(nums)): + + for j in range(i+1, len(nums)): + + for k in range(j+1, len(nums)): + + if nums[i] < nums[k] < nums[j]: + + return [nums[i], nums[j], nums[k]] + + return [] + + +# Test cases +print(find_pattern_132([3, 1, 4, 2])) # Expected: [1, 4, 2] + +print(find_pattern_132([1, 2, 3, 4])) # Expected: [] + +print(find_pattern_132([1, 3, 2, 4, 6, 5])) # Expected: [1, 3, 2] + +print(find_pattern_132([1, 3, 4, 2])) # Expected: [1, 3, 2] diff --git a/challenge-196/lubos-kolouch/python/ch-2.py b/challenge-196/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..fe83254399 --- /dev/null +++ b/challenge-196/lubos-kolouch/python/ch-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def find_ranges(array): + ranges = [] + start, end = None, None + + for num in array: + if start is None: + start = end = num + elif num == end + 1: + end = num + else: + if end > start: + ranges.append([start, end]) + start = end = num + + if start is not None and end > start: + ranges.append([start, end]) + + return ranges + + +# Test cases +assert find_ranges([1, 3, 4, 5, 7]) == [[3, 5]], "Example 1" +assert find_ranges([1, 2, 3, 6, 7, 9]) == [[1, 3], [6, 7]], "Example 2" +assert find_ranges([0, 1, 2, 4, 5, 6, 8, 9]) == [ + [0, 2], [4, 6], [8, 9]], "Example 3" + +print("All tests passed!") |
