diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-02-04 14:04:38 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-02-04 14:04:38 +0000 |
| commit | 8d9cfd8d2ac39bb5d0b0f7452bf57d200bbdd62f (patch) | |
| tree | 0e3759046de6e318d1c9e8e305f683b4324d43c6 | |
| parent | 1b0c04f4b45857a5b8024860663bf58f353bf1ed (diff) | |
| download | perlweeklychallenge-club-8d9cfd8d2ac39bb5d0b0f7452bf57d200bbdd62f.tar.gz perlweeklychallenge-club-8d9cfd8d2ac39bb5d0b0f7452bf57d200bbdd62f.tar.bz2 perlweeklychallenge-club-8d9cfd8d2ac39bb5d0b0f7452bf57d200bbdd62f.zip | |
- Added solutions by james Smith.
- Added solutions by Luca Ferrari.
- Added solutions by Aut0exec.
- Added solutions by W. Luis Mochan.
- Added solutions by Bob Lied.
- Added solutions by David Ferrone.
- Added solutions by E. Choroba.
- Added solutions by Mark Anderson.
- Added solutions by Robbie Hatley.
- Added solutions by Dave Jacoby.
- Added solutions by Thomas Kohler.
- Added solutions by Jaldhar H. Vyas.
- Added solutions by Peter Campbell Smith.
- Added solutions by Mariano Spadaccini.
- Added solutions by Jorg Sommrey.
- Added solutions by Pip Stuart.
- Added solutions by Simon Green.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Ulrich Rieke.
- Added solutions by Robert DiCicco.
43 files changed, 3389 insertions, 2003 deletions
diff --git a/challenge-201/aut0exec/perl/Task1.pl b/challenge-201/aut0exec/perl/ch-1.pl index c2a127b36b..c2a127b36b 100644 --- a/challenge-201/aut0exec/perl/Task1.pl +++ b/challenge-201/aut0exec/perl/ch-1.pl diff --git a/challenge-202/e-choroba/erlang/widest_valley.erl b/challenge-202/e-choroba/erlang/ch-2.erl index 5924e46287..5924e46287 100644 --- a/challenge-202/e-choroba/erlang/widest_valley.erl +++ b/challenge-202/e-choroba/erlang/ch-2.erl diff --git a/challenge-202/eric-cheung/python/ch-1.py b/challenge-202/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..b223c4f32c --- /dev/null +++ b/challenge-202/eric-cheung/python/ch-1.py @@ -0,0 +1,16 @@ +
+def IsConsecOdds(arrSubInput):
+ arrSubInput.sort()
+
+ for nIndx in range(0, len(arrSubInput) - 3 + 1):
+ if arrSubInput[nIndx] % 2 == 1 and arrSubInput[nIndx + 1] - arrSubInput[nIndx] == 2 and arrSubInput[nIndx + 2] - arrSubInput[nIndx + 1] == 2:
+ return 1
+
+ return 0
+
+## arrInput = [1, 5, 3, 6] ## Example 1
+## arrInput = [2, 6, 3, 5] ## Example 2
+## arrInput = [1, 2, 3, 4] ## Example 3
+arrInput = [2, 3, 5, 7] ## Example 4
+
+print (IsConsecOdds(arrInput))
diff --git a/challenge-202/eric-cheung/python/ch-2.py b/challenge-202/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..5e7224f51f --- /dev/null +++ b/challenge-202/eric-cheung/python/ch-2.py @@ -0,0 +1,127 @@ +
+## Remarks
+## https://www.geeksforgeeks.org/maximum-count-number-of-valley-elements-in-a-subarray-of-size-k/
+
+
+## Valley
+## First Part: Non-Increasing, Could Be Empty
+## Second Part: Non-Decreasing, Could Be Empty
+
+
+## arrInput = [1, 5, 5, 2, 8] ## Example 1
+## arrInput = [2, 6, 8, 5] ## Example 2
+## arrInput = [9, 8, 13, 13, 2, 2, 15, 17] ## Example 3
+## arrInput = [2, 1, 2, 1, 3] ## Example 4
+arrInput = [1, 3, 3, 2, 1, 2, 3, 3, 2] ## Example 5
+
+
+arrOutput = []
+arrInterOutput = []
+arrSubOutput = []
+arrTrendUpSign = []
+arrEqElem = []
+
+
+bTrendUp = True
+bPrevTrendUp = True
+bChangeSign = True
+bStart = True
+
+
+for nIndx in range(1, len(arrInput)):
+
+ if arrInput[nIndx - 1] < arrInput[nIndx]:
+ bTrendUp = True
+ elif arrInput[nIndx - 1] > arrInput[nIndx]:
+ bTrendUp = False
+ else:
+ arrEqElem.append(arrInput[nIndx])
+
+ if nIndx == 1:
+ bPrevTrendUp = bTrendUp
+ bChangeSign = True
+ elif bTrendUp and bPrevTrendUp or not bTrendUp and not bPrevTrendUp:
+ bChangeSign = False
+ elif len(arrSubOutput) > 0:
+ arrOutput.append(arrSubOutput)
+ arrTrendUpSign.append(not bTrendUp)
+ arrSubOutput = []
+ bChangeSign = True
+
+ if bChangeSign:
+ if len(arrEqElem) > 0:
+ arrSubOutput = arrSubOutput + arrEqElem ## Append List
+ arrEqElem = []
+
+ arrSubOutput.append(arrInput[nIndx - 1])
+
+ arrSubOutput.append(arrInput[nIndx])
+
+ bPrevTrendUp = bTrendUp
+
+
+if len(arrSubOutput) > 0:
+ arrOutput.append(arrSubOutput)
+ arrTrendUpSign.append(bTrendUp)
+ arrSubOutput = []
+
+
+## print (arrOutput)
+## print (arrTrendUpSign)
+
+for nIndx, arrElemLoop in enumerate(arrOutput):
+
+ ## print (bStart, arrTrendUpSign[nIndx])
+
+ if bStart and arrTrendUpSign[nIndx]:
+ if len(arrSubOutput) > 0:
+ for nCount in range(0, len(arrElemLoop)):
+ if arrElemLoop[nCount] != arrSubOutput[-1]:
+ break
+ arrSubOutput = arrSubOutput + arrElemLoop[nCount:] ## Append List
+ else:
+ arrSubOutput = arrSubOutput + arrElemLoop ## Append List
+
+ arrInterOutput.append(arrSubOutput)
+ arrSubOutput = []
+
+ elif bStart and not arrTrendUpSign[nIndx]:
+ if len(arrSubOutput) > 0:
+ for nCount in range(0, len(arrElemLoop)):
+ if arrElemLoop[nCount] != arrSubOutput[-1]:
+ break
+ arrSubOutput = arrSubOutput + arrElemLoop[nCount:] ## Append List
+ else:
+ arrSubOutput = arrSubOutput + arrElemLoop ## Append List
+
+ bStart = False
+
+ elif not bStart and arrTrendUpSign[nIndx]:
+ if len(arrSubOutput) > 0:
+ for nCount in range(0, len(arrElemLoop)):
+ if arrElemLoop[nCount] != arrSubOutput[-1]:
+ break
+
+ arrSubOutput = arrSubOutput + arrElemLoop[nCount:] ## Append List
+ else:
+ arrSubOutput = arrSubOutput + arrElemLoop[1:] ## Append List
+
+ elif not bStart and not arrTrendUpSign[nIndx]:
+ if len(arrSubOutput) > 0:
+ arrInterOutput.append(arrSubOutput)
+ arrSubOutput = []
+ arrSubOutput = arrSubOutput + arrElemLoop ## Append List
+ bStart = True
+
+if len(arrSubOutput) > 0:
+ arrInterOutput.append(arrSubOutput)
+ arrSubOutput = []
+
+
+arrFinalOutput = arrInterOutput[0]
+for arrElemLoop in arrInterOutput[1:]:
+ if len(arrElemLoop) > len(arrFinalOutput):
+ arrFinalOutput = arrElemLoop
+
+## print (arrInterOutput)
+print (arrFinalOutput)
diff --git a/challenge-202/laurent-rosenfeld/blog.txt b/challenge-202/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..4668480cac --- /dev/null +++ b/challenge-202/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/01/perl-weekly-challenge-202-consecutive-odds-and-widest-valley.html diff --git a/challenge-202/laurent-rosenfeld/perl/ch-1.pl b/challenge-202/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..c22fc8bafe --- /dev/null +++ b/challenge-202/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,21 @@ +use strict; +use warnings; +use feature "say"; + +sub three_odd { + my $count = 0; + for my $n (@_) { + if ($n % 2) { # Odd + $count++; + } else { # Even + $count = 0; + } + return 1 if $count >= 3; + } + return 0; +} + +for my $test ([<1 5 3 6>], [<2 6 3 5>], + [<1 2 3 4>], [<2 3 5 7>]) { + say "@$test => ", three_odd @$test; +} diff --git a/challenge-202/laurent-rosenfeld/perl/ch-2.pl b/challenge-202/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..4be0eb6eb0 --- /dev/null +++ b/challenge-202/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,35 @@ +use strict; +use warnings; +use feature "say"; + +sub widest_valley { + my (@valley, @temp); + for my $i (1..$#_) { # valley with no left part + push @valley, $_[$i-1]; + last if $_[$i] < $_[$i-1]; + } + + for my $i (1..$#_) { + my $left = 1; + for my $j ($i..$#_) { + if ($left) { + push @temp, $_[$j - 1]; + push @temp, $_[$j] and $left = 0 + if $_[$j] > $_[$j - 1]; + } else { + last if $_[$j] < $_[$j-1]; + push @temp, $_[$j]; + } + } + @valley = @temp if scalar @temp > scalar @valley; + @temp = (); + } + return @valley; +} + +for my $test ([<1 5 5 2 8>], [<1 5 5 2>], [<2 6 8 5>], + [<9 8 13 13 2 2 15 17>], [<2 1 2 1 3>], + [<1 3 3 2 1 2 3 3 2>]) { + printf "%-20s => ", join " ", @$test; + say join " ", widest_valley @$test; +} diff --git a/challenge-202/laurent-rosenfeld/raku/ch-1.raku b/challenge-202/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..edefb50afd --- /dev/null +++ b/challenge-202/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,16 @@ +sub three-odd (@in) { + my $count = 0; + for @in -> $n { + if $n %% 2 { # Even + $count = 0; + } else { # Odd + $count++; + } + return 1 if $count >= 3; + } + return 0; +} + +for <1 5 3 6>, <2 6 3 5>, <1 2 3 4>, <2 3 5 7> -> @test { + say "@test[] => ", three-odd @test; +} diff --git a/challenge-202/laurent-rosenfeld/raku/ch-2.raku b/challenge-202/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..0afac33392 --- /dev/null +++ b/challenge-202/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,31 @@ +sub widest-valley (@in) { + my (@valley, @temp); + for 1..@in.end -> $i { # valley with no left part + push @valley, @in[$i-1]; + last if @in[$i] < @in[$i-1]; + } + + for 1..@in.end -> $i { + my $left = True; + for $i..@in.end -> $j { + if $left { + push @temp, @in[$j - 1]; + push @temp, @in[$j] and $left = False + if @in[$j] > @in[$j - 1]; + } else { + last if @in[$j] < @in[$j-1]; + push @temp, @in[$j]; + } + } + @valley = @temp if @temp.elems > @valley.elems; + @temp = (); + } + return @valley; +} + +for <1 5 5 2 8>, <1 5 5 2>, <2 6 8 5>, + <9 8 13 13 2 2 15 17>, <2 1 2 1 3>, + <1 3 3 2 1 2 3 3 2> -> @test { + say "@test[]".fmt("%-20s => "), + widest-valley @test; +} diff --git a/challenge-202/peter-campbell-smith/perl/ch-01.pl b/challenge-202/peter-campbell-smith/perl/ch-1.pl index 04e0d39322..04e0d39322 100755 --- a/challenge-202/peter-campbell-smith/perl/ch-01.pl +++ b/challenge-202/peter-campbell-smith/perl/ch-1.pl diff --git a/challenge-202/peter-campbell-smith/perl/ch-02.pl b/challenge-202/peter-campbell-smith/perl/ch-2.pl index 8b623c0f5b..8b623c0f5b 100755 --- a/challenge-202/peter-campbell-smith/perl/ch-02.pl +++ b/challenge-202/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-202/robert-dicicco/julia/ch-1.jl b/challenge-202/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..a4f5e16271 --- /dev/null +++ b/challenge-202/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,69 @@ +#!/usr/bin/env julia + +#= + +AUTHOR: Robert DiCicco + +DATE : 2023-01-30 + +Challenge 202 Consecutive Odds ( Julia ) + +=# + + + +using Printf + + + +arrays = [[1,5,3,6],[2,6,3,5],[1,2,3,4],[2,3,5,7]] + + + +for arr in arrays + + check = ' ' + + @printf("Input: @array = %s\n",arr) + + for x in 1:length(arr) + + arr[x] % 2 == 0 ? check *= 'e' : check *= 'o' + + end + + occursin(r"ooo", check) ? println("Output: 1\n") : println("Output: 0\n") + +end + + + +#= + +SAMPLE OUTPUT + +julia .\ConsecutiveOdds.jl + +Input: @array = [1, 5, 3, 6] + +Output: 1 + + + +Input: @array = [2, 6, 3, 5] + +Output: 0 + + + +Input: @array = [1, 2, 3, 4] + +Output: 0 + + + +Input: @array = [2, 3, 5, 7] + +Output: 1 + +=# diff --git a/challenge-202/robert-dicicco/perl/ch-1.pl b/challenge-202/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..1f221a8292 --- /dev/null +++ b/challenge-202/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,71 @@ +#!/usr/bin/env perl + +=begin pod + +AUTHOR: Robert DiCicco + +DATE : 2023-01-30 + +Challenge 202 Consecutive Odds + +=cut + +use strict; + +use warnings; + +use feature qw/say/; + + + +my @arrays = ([1,5,3,6],[2,6,3,5],[1,2,3,4],[2,3,5,7]); + + + +for my $arr (@arrays) { + + my $check = ''; + + say "Input: \@array = [@$arr]"; + + for (my $x = 0; $x < @$arr; $x++ ) { + + $arr->[$x] % 2 == 0 ? $check .= 'e' : $check .= 'o'; + + } + + $check =~ m/ooo/ ? say "Output : 1\n" : say "Output : 0\n"; + +} + + + +=begin pod + +SAMPLE OUTPUT + +perl .\ConsecutiveOdds.pl + +Input: @array = [1 5 3 6] + +Output : 1 + + + +Input: @array = [2 6 3 5] + +Output : 1 + + + +Input: @array = [1 2 3 4] + +Output : 0 + + + +Input: @array = [2 3 5 7] + +Output : 1 + +=cut diff --git a/challenge-202/robert-dicicco/python/ch-1.py b/challenge-202/robert-dicicco/python/ch-1.py new file mode 100644 index 0000000000..da8ed94478 --- /dev/null +++ b/challenge-202/robert-dicicco/python/ch-1.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python + + + +# AUTHOR: Robert DiCicco + +# DATE : 2023-01-30 + +# Challenge 202 Consecutive Odds ( Python ) + + + +import re + + + +arrays = [[1,5,3,6],[2,6,3,5],[1,2,3,4],[2,3,5,7]] + + + +for arr in arrays : + + print("Input: @array = ",arr) + + check = '' + + for x in range(0,len(arr)) : + + if (arr[x] % 2 == 0) : + + check += 'e' + + else : + + check += 'o' + + if check.find('ooo') >= 0 : + + print("Output: 1\n") + + else: + + print("Output: 0\n") + + + +# SAMPLE OUTPUT + +# python .\ConsecutiveOdds.py + +# Input: @array = [1, 5, 3, 6] + +# Output: 1 + + + +# Input: @array = [2, 6, 3, 5] + +# Output: 0 + + + +# Input: @array = [1, 2, 3, 4] + +# Output: 0 + + + +# Input: @array = [2, 3, 5, 7] + +# Output: 1 diff --git a/challenge-202/robert-dicicco/raku/ch-1.raku b/challenge-202/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..45cdf2ffc5 --- /dev/null +++ b/challenge-202/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,65 @@ +#/usr/bin/env raku + +=begin comment + +AUTHOR: Robert DiCicco + +DATE : 2023-01-30 + +Challenge 202 Consecutive Odds ( Raku ) + +=end comment + + + +my @arrays = ([1,5,3,6],[2,6,3,5],[1,2,3,4],[2,3,5,7]); + + + +for @arrays -> @arr { + + my $check = ''; + + say "Input: \@array = ",@arr; + + loop (my $x = 0; $x < @arr.elems; $x++ ) { + + @arr[$x] % 2 == 0 ?? ($check ~= 'e') !! ($check ~= 'o'); + + } + + $check.match(/ooo/) ?? say "Output: 1\n" !! say "Output: 0\n"; + +} + + + +=begin comment + +SAMPLE OUTPUT + +raku .\ConsecutiveOdds.rk + +Input: @array = [1 5 3 6] + +Output: 1 + + + +Input: @array = [2 6 3 5] + +Output: 0 + + + +Input: @array = [1 2 3 4] + +Output: 0 + + + +Input: @array = [2 3 5 7] + +Output: 1 + +=end comment diff --git a/challenge-202/robert-dicicco/ruby/ch-1.rb b/challenge-202/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..4452ae47c4 --- /dev/null +++ b/challenge-202/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,67 @@ +#!usr/bin/env ruby + +=begin + +AUTHOR: Robert DiCicco + +DATE : 2023-01-30 + +Challenge 202 Consecutive Odds ( Ruby ) + +=end + + + +arrays = [[1,5,3,6],[2,6,3,5],[1,2,3,4],[2,3,5,7]] + + + +arrays.each do |arr| + + check = '' + + puts("Input: @array = #{arr}") + + for x in 0..arr.count - 1 + + arr[x] % 2 == 0 ? check = check + 'e' : check = check + 'o' + + end + + check.match('ooo') ? puts("Output: 1") : puts("Output: 0") + + puts() + +end + + + +=begin + +SAMPLE OUTPUT + +ruby .\ConsecutiveOdds.rb + +Input: @array = [1, 5, 3, 6] + +Output: 1 + + + +Input: @array = [2, 6, 3, 5] + +Output: 0 + + + +Input: @array = [1, 2, 3, 4] + +Output: 0 + + + +Input: @array = [2, 3, 5, 7] + +Output: 1 + +=end diff --git a/challenge-202/ulrich-rieke/cpp/ch-1.cpp b/challenge-202/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..be9c6968c1 --- /dev/null +++ b/challenge-202/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,37 @@ +#include <iostream> +#include <vector> +#include <string> +#include <algorithm> +#include <cstdlib> + +int main( int argc , char* argv[] ) { + if ( argc < 4 ) { + std::cout << "Enter a least 3 integers, separated by a blank!\n" ; + return 1 ; + } + else { + std::vector<int> numbers ; + for ( int i = 1 ; i < argc ; i++ ) { + numbers.push_back( std::atoi( argv[ i ] ) ) ; + } + int result = 0 ; + int len = numbers.size( ) ; + for ( int i = 0 ; i < len - 2 ; i++ ) { + if ( std::all_of( numbers.begin( ) + i , numbers.begin( ) + i + 3 , + [ ]( int n ) { return n % 2 == 1 ; } ) ) + result = 1 ; + } + std::cout << result << std::endl ; + return 0 ; + } +} +------------------------------------------------------------------------------------ + task 1, in Haskell +------------------------------------------------------------------------------------ + +module Challenge202 + where +import Data.List.Split ( divvy ) + +solution :: [Int] -> Int +solution list = if any (\li -> all odd li ) $ divvy 3 1 list then 1 else 0 diff --git a/challenge-202/ulrich-rieke/cpp/ch-2.cpp b/challenge-202/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..74945ca246 --- /dev/null +++ b/challenge-202/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,74 @@ +#include <iostream> +#include <vector> +#include <string> +#include <algorithm> +#include <utility> +#include <iterator> + +std::vector<std::string> split( const std::string & startline , + const std::string & sep ) { + std::vector<std::string> separated ; + std::string::size_type start { 0 } ; + std::string::size_type pos ; + do { + pos = startline.find_first_of( sep , start ) ; + separated.push_back( startline.substr(start , pos - start )) ; + start = pos + 1 ; + } while ( pos != std::string::npos ) ; + return separated ; +} + +std::pair<int, int> findValley( const std::vector<int> & numbers , int pos ) { + int currentpos = pos ; + std::pair<int , int> result ; + int len = numbers.size( ) ; + do { + currentpos++ ; + } while ( currentpos < len && numbers[ currentpos ] <= + numbers[ currentpos - 1 ] ) ; + if ( currentpos == len ) { + result = std::make_pair( pos , currentpos - 1 - pos) ; + } + else { + do { + currentpos++ ; + } while ( currentpos < len && numbers[ currentpos ] >= + numbers[ currentpos - 1 ] ) ; + result = std::make_pair( pos , currentpos - 1 - pos ) ; + } + return result ; +} + +bool myCompare( const std::pair<int , int> & myA , const std::pair<int , int> + & myB ) { + if ( myA.second == myB.second ) { + return myA.first < myB.first ; + } + else { + return myA.second > myB.second ; + } +} + +int main( ) { + std::cout << "Enter some integers, separated by a blank!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector<std::string> numberstrings ( split( line, " " ) ) ; + std::vector<int> numbers ; + for ( auto & s : numberstrings ) { + numbers.push_back( std::stoi( s ) ) ; + } + int len = numbers.size( ) ; + std::vector<std::pair<int , int>> positions ; + for ( int i = 0 ; i < len - 1 ; i++ ) { + std::pair<int , int> result { findValley( numbers , i ) } ; + positions.push_back( result ) ; + } + std::sort( positions.begin( ) , positions.end( ) , myCompare ) ; + int startpos = positions.begin( )->first ; + int stride = positions.begin( )->second ; + std::copy( numbers.begin( ) + startpos , numbers.begin( ) + startpos + + stride + 1 , std::ostream_iterator< |
