From 8d9cfd8d2ac39bb5d0b0f7452bf57d200bbdd62f Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sat, 4 Feb 2023 14:04:38 +0000 Subject: - 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. --- challenge-201/aut0exec/perl/Task1.pl | 22 - challenge-201/aut0exec/perl/ch-1.pl | 22 + challenge-202/e-choroba/erlang/ch-2.erl | 53 + challenge-202/e-choroba/erlang/widest_valley.erl | 53 - challenge-202/eric-cheung/python/ch-1.py | 16 + challenge-202/eric-cheung/python/ch-2.py | 127 ++ challenge-202/laurent-rosenfeld/blog.txt | 1 + challenge-202/laurent-rosenfeld/perl/ch-1.pl | 21 + challenge-202/laurent-rosenfeld/perl/ch-2.pl | 35 + challenge-202/laurent-rosenfeld/raku/ch-1.raku | 16 + challenge-202/laurent-rosenfeld/raku/ch-2.raku | 31 + challenge-202/peter-campbell-smith/perl/ch-01.pl | 26 - challenge-202/peter-campbell-smith/perl/ch-02.pl | 64 - challenge-202/peter-campbell-smith/perl/ch-1.pl | 26 + challenge-202/peter-campbell-smith/perl/ch-2.pl | 64 + challenge-202/robert-dicicco/julia/ch-1.jl | 69 ++ challenge-202/robert-dicicco/perl/ch-1.pl | 71 ++ challenge-202/robert-dicicco/python/ch-1.py | 71 ++ challenge-202/robert-dicicco/raku/ch-1.raku | 65 ++ challenge-202/robert-dicicco/ruby/ch-1.rb | 67 ++ challenge-202/ulrich-rieke/cpp/ch-1.cpp | 37 + challenge-202/ulrich-rieke/cpp/ch-2.cpp | 74 ++ challenge-202/ulrich-rieke/haskell/ch-1.hs | 6 + challenge-202/ulrich-rieke/haskell/ch-2.hs | 41 + challenge-202/ulrich-rieke/perl/ch-1.pl | 25 + challenge-202/ulrich-rieke/perl/ch-2.pl | 43 + challenge-202/ulrich-rieke/raku/ch-1.raku | 20 + challenge-202/ulrich-rieke/raku/ch-2.raku | 39 + challenge-202/ulrich-rieke/rust/ch-1.rs | 20 + challenge-202/ulrich-rieke/rust/ch-2.rs | 56 + stats/pwc-challenge-200.json | 567 ++++----- stats/pwc-challenge-201.json | 680 +++++++++++ stats/pwc-current.json | 471 ++------ stats/pwc-language-breakdown-summary.json | 68 +- stats/pwc-language-breakdown.json | 1365 +++++++++++----------- stats/pwc-leaders.json | 486 ++++---- stats/pwc-summary-1-30.json | 106 +- stats/pwc-summary-121-150.json | 132 +-- stats/pwc-summary-151-180.json | 44 +- stats/pwc-summary-181-210.json | 58 +- stats/pwc-summary-211-240.json | 112 +- stats/pwc-summary-241-270.json | 46 +- stats/pwc-summary-271-300.json | 60 +- stats/pwc-summary-31-60.json | 46 +- stats/pwc-summary-61-90.json | 48 +- stats/pwc-summary-91-120.json | 48 +- stats/pwc-summary.json | 106 +- 47 files changed, 3555 insertions(+), 2169 deletions(-) delete mode 100644 challenge-201/aut0exec/perl/Task1.pl create mode 100644 challenge-201/aut0exec/perl/ch-1.pl create mode 100644 challenge-202/e-choroba/erlang/ch-2.erl delete mode 100644 challenge-202/e-choroba/erlang/widest_valley.erl create mode 100755 challenge-202/eric-cheung/python/ch-1.py create mode 100755 challenge-202/eric-cheung/python/ch-2.py create mode 100644 challenge-202/laurent-rosenfeld/blog.txt create mode 100644 challenge-202/laurent-rosenfeld/perl/ch-1.pl create mode 100644 challenge-202/laurent-rosenfeld/perl/ch-2.pl create mode 100644 challenge-202/laurent-rosenfeld/raku/ch-1.raku create mode 100644 challenge-202/laurent-rosenfeld/raku/ch-2.raku delete mode 100755 challenge-202/peter-campbell-smith/perl/ch-01.pl delete mode 100755 challenge-202/peter-campbell-smith/perl/ch-02.pl create mode 100755 challenge-202/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-202/peter-campbell-smith/perl/ch-2.pl create mode 100644 challenge-202/robert-dicicco/julia/ch-1.jl create mode 100644 challenge-202/robert-dicicco/perl/ch-1.pl create mode 100644 challenge-202/robert-dicicco/python/ch-1.py create mode 100644 challenge-202/robert-dicicco/raku/ch-1.raku create mode 100644 challenge-202/robert-dicicco/ruby/ch-1.rb create mode 100644 challenge-202/ulrich-rieke/cpp/ch-1.cpp create mode 100644 challenge-202/ulrich-rieke/cpp/ch-2.cpp create mode 100644 challenge-202/ulrich-rieke/haskell/ch-1.hs create mode 100644 challenge-202/ulrich-rieke/haskell/ch-2.hs create mode 100644 challenge-202/ulrich-rieke/perl/ch-1.pl create mode 100644 challenge-202/ulrich-rieke/perl/ch-2.pl create mode 100644 challenge-202/ulrich-rieke/raku/ch-1.raku create mode 100644 challenge-202/ulrich-rieke/raku/ch-2.raku create mode 100644 challenge-202/ulrich-rieke/rust/ch-1.rs create mode 100644 challenge-202/ulrich-rieke/rust/ch-2.rs create mode 100644 stats/pwc-challenge-201.json diff --git a/challenge-201/aut0exec/perl/Task1.pl b/challenge-201/aut0exec/perl/Task1.pl deleted file mode 100644 index c2a127b36b..0000000000 --- a/challenge-201/aut0exec/perl/Task1.pl +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env perl -# -# 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. -# - -use strict; -use warnings; - -#~ my @num_array = (2, 1, 4, 3); -my @num_array = (0, 1, 2, 3, 4); -my $array_len = scalar(@num_array); -my %array_hash = map { $_ => 1 } @num_array; - -print("Array length is: $array_len \n"); - -foreach ( 0..$array_len ){ - if (! exists($array_hash{$_})){ - print("Array is missing $_!\n"); - last; - } -} diff --git a/challenge-201/aut0exec/perl/ch-1.pl b/challenge-201/aut0exec/perl/ch-1.pl new file mode 100644 index 0000000000..c2a127b36b --- /dev/null +++ b/challenge-201/aut0exec/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +# +# 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. +# + +use strict; +use warnings; + +#~ my @num_array = (2, 1, 4, 3); +my @num_array = (0, 1, 2, 3, 4); +my $array_len = scalar(@num_array); +my %array_hash = map { $_ => 1 } @num_array; + +print("Array length is: $array_len \n"); + +foreach ( 0..$array_len ){ + if (! exists($array_hash{$_})){ + print("Array is missing $_!\n"); + last; + } +} diff --git a/challenge-202/e-choroba/erlang/ch-2.erl b/challenge-202/e-choroba/erlang/ch-2.erl new file mode 100644 index 0000000000..5924e46287 --- /dev/null +++ b/challenge-202/e-choroba/erlang/ch-2.erl @@ -0,0 +1,53 @@ +-module(widest_valley). +-export([widest_valley/1]). +-record(valley, {from=1, to=1, deepest=1}). + +widest_valley([]) -> + []; +widest_valley([H]) -> + [H]; +widest_valley([H,T]) -> + [H,T]; +widest_valley(L) -> + widest_valley(L, #valley{}, #valley{}, 1, 2). + +widest_valley(L, _Curr, Widest, _FlatFrom, Pos) when Pos > length(L) -> + lists:sublist(L, Widest#valley.from, + 1 + Widest#valley.to - Widest#valley.from); +widest_valley(L, Curr, Widest, FlatFrom, Pos) -> + Before = lists:nth(Pos - 1, L), + AtPos = lists:nth(Pos, L), + Deepest = lists:nth(Curr#valley.deepest, L), + Curr1 = if Before == Deepest -> + Curr#valley{deepest = if AtPos < Deepest -> + Pos; + true -> + Curr#valley.deepest + end}; + AtPos < Before -> + #valley{from=FlatFrom, deepest=Pos}; + true -> + Curr + end, + FlatFrom1 = if AtPos == Before -> + FlatFrom; + true -> + Pos + end, + Widest1 = + if Pos - Curr1#valley.from > Widest#valley.to - Widest#valley.from + -> Curr1#valley{to=Pos}; + true -> + Widest + end, + widest_valley(L, Curr1, Widest1, FlatFrom1, Pos + 1). + +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +widest_valley_test() -> + ?assert(widest_valley([1, 5, 5, 2, 8]) == [5, 5, 2, 8]), + ?assert(widest_valley([2, 6, 8, 5]) == [2, 6, 8]), + ?assert(widest_valley([9, 8, 13, 13, 2, 2, 15, 17]) == [13, 13, 2, 2, 15, 17]), + ?assert(widest_valley([2, 1, 2, 1, 3]) == [2, 1, 2]), + ?assert(widest_valley([1, 3, 3, 2, 1, 2, 3, 3, 2]) == [3, 3, 2, 1, 2, 3, 3]). +-endif. diff --git a/challenge-202/e-choroba/erlang/widest_valley.erl b/challenge-202/e-choroba/erlang/widest_valley.erl deleted file mode 100644 index 5924e46287..0000000000 --- a/challenge-202/e-choroba/erlang/widest_valley.erl +++ /dev/null @@ -1,53 +0,0 @@ --module(widest_valley). --export([widest_valley/1]). --record(valley, {from=1, to=1, deepest=1}). - -widest_valley([]) -> - []; -widest_valley([H]) -> - [H]; -widest_valley([H,T]) -> - [H,T]; -widest_valley(L) -> - widest_valley(L, #valley{}, #valley{}, 1, 2). - -widest_valley(L, _Curr, Widest, _FlatFrom, Pos) when Pos > length(L) -> - lists:sublist(L, Widest#valley.from, - 1 + Widest#valley.to - Widest#valley.from); -widest_valley(L, Curr, Widest, FlatFrom, Pos) -> - Before = lists:nth(Pos - 1, L), - AtPos = lists:nth(Pos, L), - Deepest = lists:nth(Curr#valley.deepest, L), - Curr1 = if Before == Deepest -> - Curr#valley{deepest = if AtPos < Deepest -> - Pos; - true -> - Curr#valley.deepest - end}; - AtPos < Before -> - #valley{from=FlatFrom, deepest=Pos}; - true -> - Curr - end, - FlatFrom1 = if AtPos == Before -> - FlatFrom; - true -> - Pos - end, - Widest1 = - if Pos - Curr1#valley.from > Widest#valley.to - Widest#valley.from - -> Curr1#valley{to=Pos}; - true -> - Widest - end, - widest_valley(L, Curr1, Widest1, FlatFrom1, Pos + 1). - --ifdef(TEST). --include_lib("eunit/include/eunit.hrl"). -widest_valley_test() -> - ?assert(widest_valley([1, 5, 5, 2, 8]) == [5, 5, 2, 8]), - ?assert(widest_valley([2, 6, 8, 5]) == [2, 6, 8]), - ?assert(widest_valley([9, 8, 13, 13, 2, 2, 15, 17]) == [13, 13, 2, 2, 15, 17]), - ?assert(widest_valley([2, 1, 2, 1, 3]) == [2, 1, 2]), - ?assert(widest_valley([1, 3, 3, 2, 1, 2, 3, 3, 2]) == [3, 3, 2, 1, 2, 3, 3]). --endif. 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-01.pl deleted file mode 100755 index 04e0d39322..0000000000 --- a/challenge-202/peter-campbell-smith/perl/ch-01.pl +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/perl - -# Peter Campbell Smith - 2023-01-30 -# PWC 202 task 1 - -use v5.28; -use utf8; -use warnings; - -# Task: 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. - -# Blog: https://pjcs-pwc.blogspot.com/ - -my (@tests, $test, @bits, $string); - -@tests = ([1, 5, 3, 6], [2, 6, 3, 5], [1, 2, 3, 4], [2, 3, 5, 7], [1, 11, 111], [2, 22, 222]); - -# loop over tests -for $test (@tests) { - - # create a string having 1 for an odd number and 0 for an even number - # and see if the string matches 111 - say qq[\nInput: \@array = (] . join(', ', @$test) . ')'; - say qq[Output: ] . (join('', map({ $_ & 1 } @$test)) =~ m|111| ? 1 : 0); -} diff --git a/challenge-202/peter-campbell-smith/perl/ch-02.pl b/challenge-202/peter-campbell-smith/perl/ch-02.pl deleted file mode 100755 index 8b623c0f5b..0000000000 --- a/challenge-202/peter-campbell-smith/perl/ch-02.pl +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/perl - -# Peter Campbell Smith - 2023-01-30 -# PWC 202 task 1 - -use v5.28; -use utf8; -use warnings; - -# Task: 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. - -# Blog: https://pjcs-pwc.blogspot.com/ - -my (@tests, $test, $j, $k, $is_bottom, $vl, $vr, @height, $rightmost, $widest, $width, $range); - -@tests = ([1, 5, 5, 2, 8], [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], - [7, 7, 7, 7, 7], [1, 2, 3, 2, 2], [6, 5, 6, 5, 4, 3], [7, 8, 8, 7], [7], [1, 2, 3, 4], [4, 3, 2, 1]); - -# loop over test values -for $test (@tests) { - @height = @$test; - $rightmost = scalar(@height) - 1; - $widest = -1; - - # scan for low points that are at the bottom of a valley - for $j (0 .. scalar(@height) - 1) { - $is_bottom = 1; - if ($j != 0) { - $is_bottom = 0 if $height[$j - 1] < $height[$j]; - } - if ($j != $rightmost) { - $is_bottom = 0 if $height[$j + 1] < $height[$j]; - } - next unless $is_bottom; - - # expand valley from bottom to the left - $k = $vl = $j; - while (-- $k >= 0) { - last unless ($height[$k] >= $height[$vl]); - $vl = $k; - } - - # and from the bottom to the right - $k = $vr = $j; - while (++ $k <= $rightmost) { - last unless ($height[$k] >= $height[$vr]); - $vr = $k; - } - - # is this the widest so far? - $width = $vr - $vl + 1; - if ($width > $widest) { - $range = ''; - $range .= qq[$height[$_], ] for $vl .. $vr; - $widest = $width; - } - } - - # show results - say qq[\nInput: ] . join(', ', @$test) . qq[\nOutput: ] . substr($range, 0, -2); -} - diff --git a/challenge-202/peter-campbell-smith/perl/ch-1.pl b/challenge-202/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..04e0d39322 --- /dev/null +++ b/challenge-202/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2023-01-30 +# PWC 202 task 1 + +use v5.28; +use utf8; +use warnings; + +# Task: 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. + +# Blog: https://pjcs-pwc.blogspot.com/ + +my (@tests, $test, @bits, $string); + +@tests = ([1, 5, 3, 6], [2, 6, 3, 5], [1, 2, 3, 4], [2, 3, 5, 7], [1, 11, 111], [2, 22, 222]); + +# loop over tests +for $test (@tests) { + + # create a string having 1 for an odd number and 0 for an even number + # and see if the string matches 111 + say qq[\nInput: \@array = (] . join(', ', @$test) . ')'; + say qq[Output: ] . (join('', map({ $_ & 1 } @$test)) =~ m|111| ? 1 : 0); +} diff --git a/challenge-202/peter-campbell-smith/perl/ch-2.pl b/challenge-202/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..8b623c0f5b --- /dev/null +++ b/challenge-202/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2023-01-30 +# PWC 202 task 1 + +use v5.28; +use utf8; +use warnings; + +# Task: 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. + +# Blog: https://pjcs-pwc.blogspot.com/ + +my (@tests, $test, $j, $k, $is_bottom, $vl, $vr, @height, $rightmost, $widest, $width, $range); + +@tests = ([1, 5, 5, 2, 8], [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], + [7, 7, 7, 7, 7], [1, 2, 3, 2, 2], [6, 5, 6, 5, 4, 3], [7, 8, 8, 7], [7], [1, 2, 3, 4], [4, 3, 2, 1]); + +# loop over test values +for $test (@tests) { + @height = @$test; + $rightmost = scalar(@height) - 1; + $widest = -1; + + # scan for low points that are at the bottom of a valley + for $j (0 .. scalar(@height) - 1) { + $is_bottom = 1; + if ($j != 0) { + $is_bottom = 0 if $height[$j - 1] < $height[$j]; + } + if ($j != $rightmost) { + $is_bottom = 0 if $height[$j + 1] < $height[$j]; + } + next unless $is_bottom; + + # expand valley from bottom to the left + $k = $vl = $j; + while (-- $k >= 0) { + last unless ($height[$k] >= $height[$vl]); + $vl = $k; + } + + # and from the bottom to the right + $k = $vr = $j; + while (++ $k <= $rightmost) { + last unless ($height[$k] >= $height[$vr]); + $vr = $k; + } + + # is this the widest so far? + $width = $vr - $vl + 1; + if ($width > $widest) { + $range = ''; + $range .= qq[$height[$_], ] for $vl .. $vr; + $widest = $width; + } + } + + # show results + say qq[\nInput: ] . join(', ', @$test) . qq[\nOutput: ] . substr($range, 0, -2); +} + 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 +#include +#include +#include +#include + +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 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 +#include +#include +#include +#include +#include + +std::vector split( const std::string & startline , + const std::string & sep ) { + std::vector 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 findValley( const std::vector & numbers , int pos ) { + int currentpos = pos ; + std::pair 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 & myA , const std::pair + & 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 numberstrings ( split( line, " " ) ) ; + std::vector numbers ; + for ( auto & s : numberstrings ) { + numbers.push_back( std::stoi( s ) ) ; + } + int len = numbers.size( ) ; + std::vector> positions ; + for ( int i = 0 ; i < len - 1 ; i++ ) { + std::pair 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( std::cout , " , " ) ); + std::cout << std::endl ; + return 0 ; +} diff --git a/challenge-202/ulrich-rieke/haskell/ch-1.hs b/challenge-202/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..fd7583734f --- /dev/null +++ b/challenge-202/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,6 @@ +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/haskell/ch-2.hs b/challenge-202/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..e9281ce2d3 --- /dev/null +++ b/challenge-202/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,41 @@ +module Challenge202_2 + where +import Data.List.Split ( divvy ) +import Data.List ( sortOn , sortBy ) + +isValidList :: [Int] -> Bool +isValidList list = all ( >= 0 ) second +where + differences :: [Int] + differences = map (\li -> last li - head li ) $ divvy 2 1 list + second :: [Int] + (_ , second ) = span ( <= 0 ) differences + +findLargestValidSublist :: [Int] -> [Int] +findLargestValidSublist list = last $ sortOn length $ filter isValidList +$ [take n list | n <- [2..length list ]] + +findIndexedSublists :: [Int] -> [(Int , [Int]) ] +findIndexedSublists list = zip [0 .. l - 2] ( map (\i -> +findLargestValidSublist $ drop i list ) [0..l - 2] ) +where + l :: Int + l = length list + +mySorter :: (Int , [Int] ) -> (Int , [Int] ) -> Ordering +mySorter ( a , aList ) ( b , bList ) + |length aList > length bList = LT + |length aList == length bList = compare a b + |length aList < length bList = GT + +findOrderedSublists :: [(Int , [Int] )] -> [(Int , [Int])] +findOrderedSublists allLists = sortBy mySorter allLists + +main :: IO ( ) +main = do + putStrLn "Enter some integers , separated by blanks!" + numberstrings <- getLine + let numbers = map read $ words numberstrings + indexedSublists = findIndexedSublists numbers + orderedLists = findOrderedSublists indexedSublists + print $ snd $ head orderedLists diff --git a/challenge-202/ulrich-rieke/perl/ch-1.pl b/challenge-202/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..5c59403e02 --- /dev/null +++ b/challenge-202/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( all ) ; + +say "Please enter at least 3 integers, separated by a blank!" ; +my $line = ; +chomp $line ; +my @numbers = split( /\s/ , $line ) ; +while ( scalar( @numbers ) < 3 ) { + say "Enter at least 3 integers, separated by a blank!" ; + $line = ; + chomp $line ; + @numbers = split ( /\s/ , $line ) ; +} +my $len = scalar( @numbers ) ; +my $result = 0 ; +for my $i (0..$len - 3 ) { + if ( all { $_ % 2 == 1 } @numbers[$i..$i + 2] ) { + $result = 1 ; + last ; + } +} +say $result ; diff --git a/challenge-202/ulrich-rieke/perl/ch-2.pl b/challenge-202/ulrich-rieke/perl/ch-2.pl new file mode 100644 index 0000000000..6ea1752ace --- /dev/null +++ b/challenge-202/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +sub findValley { + my $array = shift ; + my $pos = shift ; + my $currentPos = $pos ; + my $result ; + my $len = scalar( @$array ) ; + do { + $currentPos++ ; + } while ( $currentPos < $len && $array->[$currentPos] <= + $array->[ $currentPos - 1 ] ) ; + if ( $currentPos == $len ) { + $result = [$pos , $currentPos - 1 - $pos] ; + } + else { + do { + $currentPos++ ; + } while ( $currentPos < $len && $array->[$currentPos] >= + $array->[$currentPos - 1] ) ; + $result = [ $pos , $currentPos - 1 - $pos ] ; + } + return $result ; +} + +say "Enter some integers, separated by a blank!" ; +my $line = ; +chomp $line ; +my @numbers = split ( /\s/ , $line ) ; +my $len = scalar( @numbers ) ; +my @positions ; +for my $i (0..$len - 2 ) { + my $result = findValley(\@numbers , $i ) ; + push( @positions, $result ) ; +} +my @sorted = sort { $b->[1] <=> $a->[1] || $a->[0] <=> $b->[0] } + @positions ; +my $startpos = $sorted[0]->[0] ; +my $stride = $sorted[0]->[1] ; +say join( ", " , @numbers[$startpos..$startpos + $stride] ) ; diff --git a/challenge-202/ulrich-rieke/raku/ch-1.raku b/challenge-202/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..161cde58c9 --- /dev/null +++ b/challenge-202/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,20 @@ +use v6 ; + +say "Enter at least 3 integers , separated by a blank!" ; +my $line = $*IN.get ; +my @numbers = $line.words.map( {.Int} ) ; +my $len = @numbers.elems ; +while ( $len < 3 ) { + say "Enter at least 3 numbers!" ; + $line = $*IN.get ; + @numbers = $line.words.map( {.Int} ) ; + $len = @numbers.elems ; +} +my $result = 0 ; +for (0..$len - 3 ) -> $i { + if @numbers[$i .. $i + 2].grep( { $_ % 2 == 1 }).elems == 3 { + $result = 1 ; + last ; + } +} +say $result ; diff --git a/challenge-202/ulrich-rieke/raku/ch-2.raku b/challenge-202/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..b9ddd5dd47 --- /dev/null +++ b/challenge-202/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,39 @@ +use v6 ; + +sub findValley( @array, $pos ) { + my $len = @array.elems ; + my $currentPos = $pos ; + repeat { #supposed to be the descent , go until elements get larger + $currentPos++ ; + } until ( $currentPos == $len || @array[$currentPos] > + @array[$currentPos - 1] ) ; + my @result ; + if ( $currentPos == $len ) { #we are at the end of the array, store + #result + @result = ( $pos , $currentPos - 1 - $pos) ; + } + else { #keep going until either the end of the array or until + #elements get smaller again + repeat { + $currentPos++ ; + } until ( $currentPos == $len || @array[ $currentPos ] < + @array[ $currentPos - 1]) ; + @result = ( $pos , $currentPos - 1 - $pos ) ; + } + return @result ; +} + +say "Enter some digits, separated by blanks!" ; +my $line = $*IN.get ; +my @numbers = $line.words.map( {.Int} ) ; +my @positions ; +my $len = @numbers.elems ; +for (0..$len - 2) -> $i { + my @result = findValley( @numbers , $i ) ; + @positions.push( @result ) ; +} +my @sorted = @positions.sort( { @^b[1] <=> @^a[1] || @^a[0] <=> + @^b[0] } ) ; +my $startpos = @sorted[0][0] ; +my $stride = @sorted[0][1] ; +say @numbers[$startpos..$startpos + $stride].join( ", " ) ; diff --git a/challenge-202/ulrich-rieke/rust/ch-1.rs b/challenge-202/ulrich-rieke/rust/ch-1.rs new file mode 100644 index 0000000000..939901a332 --- /dev/null +++ b/challenge-202/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,20 @@ +use std::io ; + +fn main() { + println!("Please print more than 3 integers, separated by a blank!" ) ; + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let numbers : Vec = entered_line.split_whitespace( ).map( + | s | s.trim( ).parse::( ).expect( "Could not parse number!" )). + collect( ) ; + let sv : &[i32] = numbers.as_slice( ) ; + let mut iter = sv.windows( 3 ) ; + let mut result : i32 = 0 ; + while let Some( s ) = iter.next( ) { + if s.iter( ).all( |&n| n % 2 == 1 ) { + result = 1 ; + } + } + println!("{}" , result) ; +} diff --git a/challenge-202/ulrich-rieke/rust/ch-2.rs b/challenge-202/ulrich-rieke/rust/ch-2.rs new file mode 100644 index 0000000000..caed251667 --- /dev/null +++ b/challenge-202/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,56 @@ +use std::io ; +use std::cmp::Ordering ; + +fn find_valley( numbers : &Vec , pos : usize ) -> (usize, usize) { + let mut current_pos : usize = pos ; + let len = numbers.len( ) ; + let result : (usize , usize ) ; + current_pos += 1 ; + while current_pos < len && numbers[ current_pos ] <= + numbers[ current_pos - 1 ] { + current_pos += 1 ; + } + if current_pos == len { + result = (pos , current_pos - 1 - pos) ; + } + else { + current_pos += 1 ; + while current_pos < len && numbers[ current_pos ] >= + numbers[ current_pos - 1 ] { + current_pos += 1 ; + } + result = ( pos , current_pos - 1 - pos ) ; + } + result +} + +fn main() { + println!("Please enter at least 2 integers, separated by blanks"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let numbers : Vec = entered_line.split_whitespace( ) + .map( | s | s.trim( ).parse::( ).expect( "Could not parse number!")) + .collect( ) ; + let len = numbers.len( ) ; + let mut positions : Vec<(usize , usize )> = Vec::new( ) ; + for i in 0..len - 1 { + let result = find_valley( &numbers , i ) ; + positions.push( result ) ; + } + let vec_sli : &mut[(usize, usize)] = positions.as_mut_slice( ) ; + vec_sli.sort_by( | a , b | { + if b.1.cmp(&a.1) == Ordering::Equal { + a.0.cmp(&b.0) + } + else { + b.1.cmp( &a.1 ) + } + }) ; + let ( startpos , stride ) = vec_sli.first( ).unwrap( ) ; + let end = *startpos + *stride ; + for i in *startpos..=end { + print!("{:?} ," , numbers[ i ] ) ; + } + println!( ) ; +} diff --git a/stats/pwc-challenge-200.json b/stats/pwc-challenge-200.json index 08723857b1..22f6f81bd0 100644 --- a/stats/pwc-challenge-200.json +++ b/stats/pwc-challenge-200.json @@ -1,46 +1,219 @@ { - "subtitle" : { - "text" : "[Champions: 36] Last updated at 2023-01-29 13:06:56 GMT" - }, - "legend" : { - "enabled" : 0 - }, "chart" : { "type" : "column" }, - "title" : { - "text" : "The Weekly Challenge - 200" - }, + "series" : [ + { + "name" : "The Weekly Challenge - 200", + "colorByPoint" : 1, + "data" : [ + { + "drilldown" : "Adam Russell", + "y" : 1, + "name" : "Adam Russell" + }, + { + "drilldown" : "Arne Sommer", + "y" : 3, + "name" : "Arne Sommer" + }, + { + "y" : 2, + "name" : "Arpad Toth", + "drilldown" : "Arpad Toth" + }, + { + "name" : "Athanasius", + "y" : 4, + "drilldown" : "Athanasius" + }, + { + "y" : 2, + "name" : "Bob Lied", + "drilldown" : "Bob Lied" + }, + { + "drilldown" : "Bruce Gray", + "y" : 2, + "name" : "Bruce Gray" + }, + { + "drilldown" : "Carlos Oliveira", + "y" : 2, + "name" : "Carlos Oliveira" + }, + { + "drilldown" : "Cheok-Yin Fung", + "y" : 2, + "name" : "Cheok-Yin Fung" + }, + { + "y" : 3, + "name" : "Colin Crain", + "drilldown" : "Colin Crain" + }, + { + "name" : "Dave Jacoby", + "y" : 3, + "drilldown" : "Dave Jacoby" + }, + { + "drilldown" : "David Ferrone", + "name" : "David Ferrone", + "y" : 5 + }, + { + "name" : "Duncan C. White", + "y" : 2, + "drilldown" : "Duncan C. White" + }, + { + "drilldown" : "E. Choroba", + "y" : 2, + "name" : "E. Choroba" + }, + { + "name" : "Flavio Poletti", + "y" : 6, + "drilldown" : "Flavio Poletti" + }, + { + "drilldown" : "Jaldhar H. Vyas", + "y" : 5, + "name" : "Jaldhar H. Vyas" + }, + { + "drilldown" : "James Smith", + "name" : "James Smith", + "y" : 3 + }, + { + "y" : 1, + "name" : "Jan Krnavek", + "drilldown" : "Jan Krnavek" + }, + { + "y" : 2, + "name" : "Jorg Sommrey", + "drilldown" : "Jorg Sommrey" + }, + { + "y" : 1, + "name" : "Lars Balker", + "drilldown" : "Lars Balker" + }, + { + "y" : 5, + "name" : "Laurent Rosenfeld", + "drilldown" : "Laurent Rosenfeld" + }, + { + "drilldown" : "Luca Ferrari", + "name" : "Luca Ferrari", + "y" : 8 + }, + { + "name" : "Mariano Spadaccini", + "y" : 2, + "drilldown" : "Mariano Spadaccini" + }, + { + "y" : 2, + "name" : "Mark Anderson", + "drilldown" : "Mark Anderson" + }, + { + "drilldown" : "Marton Polgar", + "name" : "Marton Polgar", + "y" : 1 + }, + { + "drilldown" : "Matthew Neleigh", + "y" : 2, + "name" : "Matthew Neleigh" + }, + { + "y" : 3, + "name" : "Peter Campbell Smith", + "drilldown" : "Peter Campbell Smith" + }, + { + "drilldown" : "Pip Stuart", + "name" : "Pip Stuart", + "y" : 4 + }, + { + "drilldown" : "Robbie Hatley", + "name" : "Robbie Hatley", + "y" : 3 + }, + { + "drilldown" : "Robert DiCicco", + "y" : 2, + "name" : "Robert DiCicco" + }, + { + "drilldown" : "Robert Ransbottom", + "name" : "Robert Ransbottom", + "y" : 2 + }, + { + "drilldown" : "Roger Bell_West", + "name" : "Roger Bell_West", + "y" : 5 + }, + { + "y" : 3, + "name" : "Simon Green", + "drilldown" : "Simon Green" + }, + { + "drilldown" : "Solathian", + "y" : 2, + "name" : "Solathian" + }, + { + "drilldown" : "Stephen G. Lynn", + "name" : "Stephen G. Lynn", + "y" : 5 + }, + { + "drilldown" : "Thomas Kohler", + "name" : "Thomas Kohler", + "y" : 4 + }, + { + "name" : "Ulrich Rieke", + "y" : 4, + "drilldown" : "Ulrich Rieke" + }, + { + "name" : "W. Luis Mochan", + "y" : 3, + "drilldown" : "W. Luis Mochan" + } + ] + } + ], "tooltip" : { - "headerFormat" : "{series.name}
", "pointFormat" : "{point.name}: {point.y:f}
", - "followPointer" : 1 - }, - "xAxis" : { - "type" : "category" - }, - "plotOptions" : { - "series" : { - "borderWidth" : 0, - "dataLabels" : { - "enabled" : 1, - "format" : "{point.y}" - } - } + "followPointer" : 1, + "headerFormat" : "{series.name}
" }, "drilldown" : { "series" : [ { - "name" : "Adam Russell", - "id" : "Adam Russell", "data" : [ [ "Perl", 1 ] - ] + ], + "id" : "Adam Russell", + "name" : "Adam Russell" }, { + "id" : "Arne Sommer", "name" : "Arne Sommer", "data" : [ [ @@ -51,8 +224,7 @@ "Blog", 1 ] - ], - "id" : "Arne Sommer" + ] }, { "data" : [ @@ -66,6 +238,7 @@ }, { "id" : "Athanasius", + "name" : "Athanasius", "data" : [ [ "Perl", @@ -75,28 +248,27 @@ "Raku", 2 ] - ], - "name" : "Athanasius" + ] }, { - "name" : "Bob Lied", - "id" : "Bob Lied", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Bob Lied", + "id" : "Bob Lied" }, { - "name" : "Bruce Gray", "data" : [ [ "Raku", 2 ] ], - "id" : "Bruce Gray" + "id" : "Bruce Gray", + "name" : "Bruce Gray" }, { "data" : [ @@ -105,21 +277,20 @@ 2 ] ], - "id" : "Carlos Oliveira", - "name" : "Carlos Oliveira" + "name" : "Carlos Oliveira", + "id" : "Carlos Oliveira" }, { - "name" : "Cheok-Yin Fung", - "id" : "Cheok-Yin Fung", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Cheok-Yin Fung", + "name" : "Cheok-Yin Fung" }, { - "id" : "Colin Crain", "data" : [ [ "Perl", @@ -130,6 +301,7 @@ 1 ] ], + "id" : "Colin Crain", "name" : "Colin Crain" }, { @@ -148,6 +320,7 @@ }, { "name" : "David Ferrone", + "id" : "David Ferrone", "data" : [ [ "Perl", @@ -161,30 +334,30 @@ "Blog", 1 ] - ], - "id" : "David Ferrone" + ] }, { + "id" : "Duncan C. White", + "name" : "Duncan C. White", "data" : [ [ "Perl", 2 ] - ], - "id" : "Duncan C. White", - "name" : "Duncan C. White" + ] }, { - "name" : "E. Choroba", "data" : [ [ "Perl", 2 ] ], + "name" : "E. Choroba", "id" : "E. Choroba" }, { + "id" : "Flavio Poletti", "name" : "Flavio Poletti", "data" : [ [ @@ -199,12 +372,29 @@ "Blog", 2 ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] ], - "id" : "Flavio Poletti" + "id" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas" }, { - "name" : "James Smith", "id" : "James Smith", + "name" : "James Smith", "data" : [ [ "Perl", @@ -217,14 +407,14 @@ ] }, { - "name" : "Jan Krnavek", "data" : [ [ "Raku", 1 ] ], - "id" : "Jan Krnavek" + "id" : "Jan Krnavek", + "name" : "Jan Krnavek" }, { "data" : [ @@ -247,7 +437,6 @@ "name" : "Lars Balker" }, { - "id" : "Laurent Rosenfeld", "data" : [ [ "Perl", @@ -262,10 +451,10 @@ 1 ] ], - "name" : "Laurent Rosenfeld" + "name" : "Laurent Rosenfeld", + "id" : "Laurent Rosenfeld" }, { - "id" : "Luca Ferrari", "data" : [ [ "Raku", @@ -276,27 +465,28 @@ 6 ] ], - "name" : "Luca Ferrari" + "name" : "Luca Ferrari", + "id" : "Luca Ferrari" }, { - "name" : "Mariano Spadaccini", - "id" : "Mariano Spadaccini", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Mariano Spadaccini", + "name" : "Mariano Spadaccini" }, { "id" : "Mark Anderson", + "name" : "Mark Anderson", "data" : [ [ "Raku", 2 ] - ], - "name" : "Mark Anderson" + ] }, { "data" : [ @@ -309,8 +499,8 @@ "name" : "Marton Polgar" }, { - "name" : "Matthew Neleigh", "id" : "Matthew Neleigh", + "name" : "Matthew Neleigh", "data" : [ [ "Perl", @@ -319,8 +509,6 @@ ] }, { - "name" : "Peter Campbell Smith", - "id" : "Peter Campbell Smith", "data" : [ [ "Perl", @@ -330,9 +518,12 @@ "Blog", 1 ] - ] + ], + "id" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith" }, { + "id" : "Pip Stuart", "name" : "Pip Stuart", "data" : [ [ @@ -343,11 +534,9 @@ "Raku", 2 ] - ], - "id" : "Pip Stuart" + ] }, { - "name" : "Robbie Hatley", "data" : [ [ "Perl", @@ -358,11 +547,10 @@ 1 ] ], - "id" : "Robbie Hatley" + "id" : "Robbie Hatley", + "name" : "Robbie Hatley" }, { - "name" : "Robert DiCicco", - "id" : "Robert DiCicco", "data" : [ [ "Perl", @@ -372,20 +560,21 @@ "Raku", 1 ] - ] + ], + "id" : "Robert DiCicco", + "name" : "Robert DiCicco" }, { - "id" : "Robert Ransbottom", "data" : [ [ "Raku", 2 ] ], + "id" : "Robert Ransbottom", "name" : "Robert Ransbottom" }, { - "name" : "Roger Bell_West", "data" : [ [ "Perl", @@ -400,10 +589,10 @@ 1 ] ], + "name" : "Roger Bell_West", "id" : "Roger Bell_West" }, { - "name" : "Simon Green", "data" : [ [ "Perl", @@ -414,20 +603,20 @@ 1 ] ], - "id" : "Simon Green" + "id" : "Simon Green", + "name" : "Simon Green" }, { + "id" : "Solathian", "name" : "Solathian", "data" : [ [ "Perl", 2 ] - ], - "id" : "Solathian" + ] }, { - "id" : "Stephen G. Lynn", "data" : [ [ "Perl", @@ -442,10 +631,10 @@ 1 ] ], - "name" : "Stephen G. Lynn" + "name" : "Stephen G. Lynn", + "id" : "Stephen G. Lynn" }, { - "name" : "Thomas Kohler", "data" : [ [ "Perl", @@ -456,9 +645,12 @@ 2 ] ], + "name" : "Thomas Kohler", "id" : "Thomas Kohler" }, { + "name" : "Ulrich Rieke", + "id" : "Ulrich Rieke", "data" : [ [ "Perl", @@ -468,11 +660,10 @@ "Raku", 2 ] - ], - "id" : "Ulrich Rieke", - "name" : "Ulrich Rieke" + ] }, { + "name" : "W. Luis Mochan", "id" : "W. Luis Mochan", "data" : [ [ @@ -483,202 +674,34 @@ "Blog", 1 ] - ], - "name" : "W. Luis Mochan" + ] } ] }, + "xAxis" : { + "type" : "category" + }, + "legend" : { + "enabled" : 0 + }, + "subtitle" : { + "text" : "[Champions: 37] Last updated at 2023-02-04 13:57:12 GMT" + }, "yAxis" : { "title" : { "text" : "Total Solutions" } }, - "series" : [ - { - "name" : "The Weekly Challenge - 200", - "data" : [ - { - "y" : 1, - "name" : "Adam Russell", - "drilldown" : "Adam Russell" - }, - { - "drilldown" : "Arne Sommer", - "name" : "Arne Sommer", - "y" : 3 - }, - { - "y" : 2, - "name" : "Arpad Toth", - "drilldown" : "Arpad Toth" - }, - { - "name" : "Athanasius", - "y" : 4, - "drilldown" : "Athanasius" - }, - { - "drilldown" : "Bob Lied", - "name" : "Bob Lied", - "y" : 2 - }, - { - "drilldown" : "Bruce Gray", - "y" : 2, - "name" : "Bruce Gray" - }, - { - "drilldown" : "Carlos Oliveira", - "name" : "Carlos Oliveira", - "y" : 2 - }, - { - "drilldown" : "Cheok-Yin Fung", - "name" : "Cheok-Yin Fung", - "y" : 2 - }, - { - "drilldown" : "Colin Crain", - "y" : 3, - "name" : "Colin Crain" - }, - { - "y" : 3, - "name" : "Dave Jacoby", - "drilldown" : "Dave Jacoby" - }, - { - "name" : "David Ferrone", - "y" : 5, - "drilldown" : "David Ferrone" - }, - { - "drilldown" : "Duncan C. White", - "name" : "Duncan C. White", - "y" : 2 - }, - { - "y" : 2, - "name" : "E. Choroba", - "drilldown" : "E. Choroba" - }, - { - "name" : "Flavio Poletti", - "y" : 6, - "drilldown" : "Flavio Poletti" - }, - { - "drilldown" : "James Smith", - "y" : 3, - "name" : "James Smith" - }, - { - "y" : 1, - "name" : "Jan Krnavek", - "drilldown" : "Jan Krnavek" - }, - { - "drilldown" : "Jorg Sommrey", - "name" : "Jorg Sommrey", - "y" : 2 - }, - { - "drilldown" : "Lars Balker", - "name" : "Lars Balker", - "y" : 1 - }, - { - "name" : "Laurent Rosenfeld", - "y" : 5, - "drilldown" : "Laurent Rosenfeld" - }, - { - "drilldown" : "Luca Ferrari", - "name" : "Luca Ferrari", - "y" : 8 - }, - { - "drilldown" : "Mariano Spadaccini", - "name" : "Mariano Spadaccini", - "y" : 2 - }, - { - "y" : 2, - "name" : "Mark Anderson", - "drilldown" : "Mark Anderson" - }, - { - "drilldown" : "Marton Polgar", - "y" : 1, - "name" : "Marton Polgar" - }, - { - "name" : "Matthew Neleigh", - "y" : 2, - "drilldown" : "Matthew Neleigh" - }, - { - "drilldown" : "Peter Campbell Smith", - "name" : "Peter Campbell Smith", - "y" : 3 - }, - { - "name" : "Pip Stuart", - "y" : 4, - "drilldown" : "Pip Stuart" - }, - { -