diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-12-23 09:14:12 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-12-23 09:14:12 +0000 |
| commit | cfa0021bbaa682829341bf134823454b9c4d148f (patch) | |
| tree | d9b384d5ee572516dbcc8c453ac1d1f1faa86924 | |
| parent | 38b011e273c9123f777a1f2b0da3b0b488db03da (diff) | |
| download | perlweeklychallenge-club-cfa0021bbaa682829341bf134823454b9c4d148f.tar.gz perlweeklychallenge-club-cfa0021bbaa682829341bf134823454b9c4d148f.tar.bz2 perlweeklychallenge-club-cfa0021bbaa682829341bf134823454b9c4d148f.zip | |
- Added solutions by Stephen G. Lynn.
- Added solutions by Pip Stuart.
- Added solutions by Robert DiCicco.
- Added solutions by Laurent Rosenfeld.
24 files changed, 3017 insertions, 2288 deletions
diff --git a/challenge-196/laurent-rosenfeld/blog.txt b/challenge-196/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..9692dfd5c1 --- /dev/null +++ b/challenge-196/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2022/12/perl-weekly-challenge-196-pattern-132-and-range-list.html diff --git a/challenge-196/laurent-rosenfeld/perl/ch-1.pl b/challenge-196/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..04af5d864d --- /dev/null +++ b/challenge-196/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,25 @@ +use strict; +use warnings; +use feature qw/say/; + +sub find_132 { + my @in = @_; + for my $i (0..$#in) { + my @out = ($in[$i]); + for my $j ($i+1..$#in) { + next unless $in[$j] > $out[0]; + my @out2 = (@out, $in[$j]); + for my $k ($j+1..$#in) { + if ($in[$k] > $out2[0] + and $in[$k] < $out2[1]) { + return @out2, $in[$k]; + } + } + } + } + return "()"; # no solution if we've got here +} +for my $test ( [<3 1 4 2>], [<1 2 3 4>], + [<1 3 2 4 6 5>], [<1 3 4 2>] ) { + say "@$test \t=> ", join " ", find_132 @$test; +} diff --git a/challenge-196/laurent-rosenfeld/perl/ch-2.pl b/challenge-196/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..6bb0e07798 --- /dev/null +++ b/challenge-196/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; +use feature qw/say/; + +sub find_ranges { + my @in = @_; + my ($start, $curr); + my @result; + $start = $curr = $in[0]; + for my $i (1..$#in) { + next if $in[$i] == $start; + if ($in[$i] == $curr + 1) { + $curr = $in[$i]; + } else { + push @result, "[$start $curr] " + if $curr > $start; + $start = $in[$i]; + $curr = $start; + } + } + push @result, "[$start $curr]" if $curr > $start; + return @result > 0 ? @result : "[]"; +} +for my $test ([<1 3 4 5 7>], [<1 2 3 6 7 9>], + [<0 1 2 4 5 6 8 9>], [<1 3 4 6 7 11 12 13>], + [<1 3 4 5 7 9>], [<1 3 5>]) { + say sprintf("%-25s", "@$test => "), find_ranges @$test; +} diff --git a/challenge-196/laurent-rosenfeld/raku/ch-1.raku b/challenge-196/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..cb2024538e --- /dev/null +++ b/challenge-196/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,33 @@ +sub find_132 (@input, @part-result) { + # say @input, " - ", @part-result; + given @part-result.elems { + when 3 { return @part-result } + when 2 { + for 0..@input.end -> $i { + my $ret = find_132 @input[$i^..@input.end], + (@part-result, @input[$i]).flat + if @input[$i] > @part-result[0] + && @input[$i] < @part-result[1]; + return $ret if $ret; + } + } + when 1 { + for 0..@input.end -> $i { + my $ret =find_132 @input[$i^..@input.end], + (@part-result, @input[$i]).flat + if @input[$i] > @part-result[0]; + return $ret if $ret; + } + } + when 0 { + for 0..@input.end -> $i { + my $ret = find_132(@input[$i^..@input.end], + (@input[$i],)); + return $ret if $ret; + } + } + } +} +for <3 1 4 2>, <1 2 3 4>, <1 3 2 4 6 5>, <1 3 4 2> -> @test { + say @test, "\t=> ", (find_132 @test, ()) // "()"; +} diff --git a/challenge-196/laurent-rosenfeld/raku/ch-2.raku b/challenge-196/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..9c5924d5dc --- /dev/null +++ b/challenge-196/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,22 @@ +sub find-ranges (@in) { + my ($start, $curr); + my @result; + $start = $curr = @in[0]; + for 1..@in.end -> $i { + next if @in[$i] == $start; + if @in[$i] == $curr + 1 { + $curr = @in[$i]; + } else { + push @result, "[$start $curr]" + if $curr - $start > 0; + $start = @in[$i]; + $curr = $start; + } + } + push @result, "[$start $curr]" if $curr > $start; + return @result.elems > 0 ?? @result !! "[]"; +} +for <1 3 4 5 7>, <1 2 3 6 7 9>, <0 1 2 4 5 6 8 9>, + <1 3 4 6 7 11 12 13>, <1 3 4 5 7 9>, <1 3 5> -> @test { + printf "%-20s => %s\n", ~@test, ~find-ranges @test; +} diff --git a/challenge-196/robert-dicicco/julia/ch-1.jl b/challenge-196/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..ee61fc2159 --- /dev/null +++ b/challenge-196/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,147 @@ +#!/usr/bin/env julia + +#= + +---------------------------------------------- + +AUTHOR: Robert DiCicco + +DATE : 2022-12-20 + +Challenge 196 Pattern 132 ( Julia ) + +SAMPLE OUTPUT + +julia .\Pattern132.jl + +Input: @list = [3, 1, 4, 2] + +Any[1, 4, 2] + + + +Input: @list = [1, 2, 3, 4] + +Output: -1 + + + +Input: @list = [1, 3, 2, 4, 6, 5] + +Any[1, 3, 2] + + + +Input: @list = [1, 3, 4, 2] + +Any[1, 3, 2] + +---------------------------------------------- + +=# + +using Printf + + + +lists = [[3, 1, 4, 2],[1, 2, 3, 4],[1, 3, 2, 4, 6, 5],[1, 3, 4, 2]] + +out = [] + + + +function findFirst(list) + + for fi in 1:length(list) + + if list[fi] == 1 + + push!(out, list[fi]) + + return fi + + end + + end + +end + + + +function findSecond(list, fi) + + fi += 1 + + while fi < length(list) + + if list[fi] >= 3 + + push!(out, list[fi]) + + return fi + + end + + fi += 1 + + end + + return -1 + +end + + + +function findThird(list, fi) + + fi += 1 + + while fi <= length(list) + + if list[fi] in [1,2,3,4,5,6,7,8,9] + + if list[fi] < out[2] + + push!(out, list[fi]) + + end + + end + + fi += 1 + + end + + return -1 + +end + + + +for list in lists + + global out + + out = [] + + @printf("Input: @list = %s\n",list) + + retval = findFirst(list); + + secval = findSecond(list, retval) + + thirdval = findThird(list, secval) + + if length(out) == 3 + + println(out) + + else + + println("Output: -1") + + end + + print("\n") + +end diff --git a/challenge-196/robert-dicicco/python/ch-1.py b/challenge-196/robert-dicicco/python/ch-1.py new file mode 100644 index 0000000000..a22a84ef53 --- /dev/null +++ b/challenge-196/robert-dicicco/python/ch-1.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python + +''' + +---------------------------------------------- + +AUTHOR: Robert DiCicco + +DATE : 2022-12-22 + +Challenge 196 Pattern 132 ( Python ) + +SAMPLE OUTPUT + +python .\Pattern132.py + +Input: @list = [3, 1, 4, 2] + +Output: [1, 4, 2] + + + +Input: @list = [1, 2, 3, 4] + +Output: -1 + + + +Input: @list = [1, 3, 2, 4, 6, 5] + +Output: [1, 3, 2] + + + +Input: @list = [1, 3, 4, 2] + +Output: [1, 3, 2] + +---------------------------------------------- + +''' + +import array + + + +lists = [[3, 1, 4, 2],[1, 2, 3, 4],[1, 3, 2, 4, 6, 5],[1, 3, 4, 2]] + +out = [] + + + +def findFirst(list): + + for fi in range(len(list)): + + if list[fi] == 1: + + #print(fi) + + out.append(list[fi]) + + return fi + + + +def findSecond(list, fi): + + fi += 1 + + while fi < len(list): + + if list[fi] >= 3: + + out.append(list[fi]) + + return fi + + fi += 1 + + return -1 + + + +def findThird(list, fi): + + fi += 1 + + while fi <= len(list) - 1: + + if list[fi] in [1,2,3,4,5,6,7,8,9] : + + if list[fi] < out[1]: + + out.append(list[fi]) + + fi += 1 + + return -1 + + + +for list in lists: + + out = [] + + print(f"Input: @list = {list}") + + retval = findFirst(list) + + secval = findSecond(list, retval) + + thirdval = findThird(list, secval) + + if len(out) == 3: + + print(f"Output: {out}\n") + + else: + + print("Output: -1\n") diff --git a/challenge-196/robert-dicicco/raku/ch-1.raku b/challenge-196/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..79517b8cd7 --- /dev/null +++ b/challenge-196/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,131 @@ +#!/usr/bin/env raku + +#`{ + +---------------------------------------------- + +AUTHOR: Robert DiCicco + +DATE : 2022-12-20 + +Challenge 196 Pattern 132 ( Raku ) + +SAMPLE OUTPUT + +raku .\Pattern132.rk + +Input: @n = [3 1 4 2] + +Output: 1 4 2 + + + +Input: @n = [1 2 3 4] + +Output: -1 + + + +Input: @n = [1 3 2 4 6 5] + +Output: 1 3 2 + + + +Input: @n = [1 3 4 2] + +Output: 1 3 2 + +---------------------------------------------- + +} + + + +my @lists = [[3, 1, 4, 2],[1, 2, 3, 4],[1, 3, 2, 4, 6, 5],[1, 3, 4, 2]]; + + + +my @out = []; + + + +sub findFirst(@list) { + + for 0..(@list.elems - 1) -> $fi { + + if @list[$fi] == 1 { + + @out.push: @list[$fi]; + + return $fi + + } + + } + +} + + + +sub findSecond(@list, $fi is copy) { + + $fi++; + + while $fi < @list.elems { + + if @list[$fi] >= 3 { + + @out.push: @list[$fi]; + + return $fi; + + } + + $fi++; + + } + + return -1; + +} + + + +sub findThird(@list, $fi is copy) { + + $fi++; + + while $fi < @list.elems { + + if @list[$fi] ~~ any [1,2,3,4,5,6,7,8,9] { + + if @list[$fi] < @out[1] { (@out.push: @list[$fi])} + + } + + $fi++; + + } + + return -1; + +} + + + +for @lists -> @list { + + @out = []; + + put "Input: @n = \[" ~ @list ~ "]"; + + my $retval = findFirst(@list); + + my $secval = findSecond(@list, $retval); + + my $thirdval = findThird(@list, $secval); + + (@out.elems == 3) ?? put("Output: ",@out,"\n") !! put("Output: -1\n"); + +} diff --git a/challenge-196/robert-dicicco/ruby/ch-1.rb b/challenge-196/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..ca5bf79c8f --- /dev/null +++ b/challenge-196/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,145 @@ +#!/usr/bin/env ruby + +=begin + +---------------------------------------------- + +AUTHOR: Robert DiCicco + +DATE : 2022-12-20 + +Challenge 196 Pattern 132 ( Ruby ) + +SAMPLE OUTPUT + +ruby .\Pattern132.rb + + + +Input: @list = [3, 1, 4, 2] + +Output: [1, 4, 2] + + + +Input: @list = [1, 2, 3, 4] + +Output: -1 + + + +Input: @list = [1, 3, 2, 4, 6, 5] + +Output: [1, 3, 2] + + + +Input: @list = [1, 3, 4, 2] + +Output: [1, 3, 2] + +---------------------------------------------- + +=end + + + +lists = [[3, 1, 4, 2],[1, 2, 3, 4],[1, 3, 2, 4, 6, 5],[1, 3, 4, 2]] + + + +def findFirst(arr) + + $out = Array.new + + for fi in 0..arr.length() - 1 + + if arr[fi] == 1 + + $out.push(arr[fi]) + + return fi + + end + + end + + return -1 + +end + + + +def findSecond(arr, fi) + + fi += 1 + + while ( fi < arr.length() - 1) + + if arr[fi] >= 3 + + $out.push(arr[fi]) + + return fi + + end + + fi += 1 + + end + + return -1 + +end + + + +def findThird(arr, fi) + + fi += 1 + + while (fi <= arr.length() - 1) + + if arr[fi] !~ /\D/ + + if arr[fi] < $out[1] + + $out.push(arr[fi]) + + end + + end + + fi += 1 + + end + + return -1 + +end + + + +lists.each do |list| + + $out = () + + puts("\nInput: @list = #{list}") + + retval = findFirst(list); + + secval = findSecond(list, retval); + + thirdval = findThird(list, secval) + + if $out.length == 3 + + puts("Output: #{$out}") + + else + + puts("Output: -1") + + end + +end diff --git a/stats/pwc-current.json b/stats/pwc-current.json index ffdd0e9d91..33b2fad2e2 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,100 +1,102 @@ { - "subtitle" : { - "text" : "[Champions: 17] Last updated at 2022-12-22 11:23:27 GMT" - }, - "legend" : { - "enabled" : 0 - }, - "xAxis" : { - "type" : "category" - }, - "tooltip" : { - "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", - "followPointer" : 1, - "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>" - }, - "chart" : { - "type" : "column" - }, "series" : [ { - "name" : "The Weekly Challenge - 196", "colorByPoint" : 1, "data" : [ { - "name" : "Athanasius", + "drilldown" : "Athanasius", "y" : 4, - "drilldown" : "Athanasius" + "name" : "Athanasius" }, { - "name" : "Bob Lied", + "drilldown" : "Bob Lied", "y" : 2, - "drilldown" : "Bob Lied" + "name" : "Bob Lied" }, { - "y" : 2, "name" : "Carlos Oliveira", + "y" : 2, "drilldown" : "Carlos Oliveira" }, { - "drilldown" : "Dave Jacoby", + "name" : "Dave Jacoby", "y" : 3, - "name" : "Dave Jacoby" + "drilldown" : "Dave Jacoby" }, { - "name" : "David Ferrone", + "drilldown" : "David Ferrone", "y" : 2, - "drilldown" : "David Ferrone" + "name" : "David Ferrone" }, { - "drilldown" : "E. Choroba", + "name" : "E. Choroba", "y" : 2, - "name" : "E. Choroba" + "drilldown" : "E. Choroba" }, { - "drilldown" : "Feng Chang", "y" : 2, + "drilldown" : "Feng Chang", "name" : "Feng Chang" }, { - "drilldown" : "James Smith", "name" : "James Smith", + "drilldown" : "James Smith", "y" : 3 }, { + "name" : "Laurent Rosenfeld", + "y" : 5, + "drilldown" : "Laurent Rosenfeld" + }, + { + "drilldown" : "Luca Ferrari", "y" : 8, - "name" : "Luca Ferrari", - "drilldown" : "Luca Ferrari" + "name" : "Luca Ferrari" }, { + "drilldown" : "Mark Anderson", "y" : 2, - "name" : "Mark Anderson", - "drilldown" : "Mark Anderson" + "name" : "Mark Anderson" }, { + "y" : 2, "drilldown" : "Niels van Dijke", - "name" : "Niels van Dijke", - "y" : 2 + "name" : "Niels van Dijke" }, { "y" : 3, - "name" : "Peter Campbell Smith", - "drilldown" : "Peter Campbell Smith" + "drilldown" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith" + }, + { + "y" : 2, + "drilldown" : "Pip Stuart", + "name" : "Pip Stuart" }, { - "drilldown" : "Robbie Hatley", "name" : "Robbie Hatley", + "drilldown" : "Robbie Hatley", "y" : 2 }, { - "name" : "Roger Bell_West", + "name" : "Robert DiCicco", + "drilldown" : "Robert DiCicco", + "y" : 1 + }, + { "y" : 4, - "drilldown" : "Roger Bell_West" + "drilldown" : "Roger Bell_West", + "name" : "Roger Bell_West" + }, + { + "name" : "Stephen G. Lynn", + "y" : 5, + "drilldown" : "Stephen G. Lynn" }, { - "drilldown" : "Thomas Kohler", "name" : "Thomas Kohler", - "y" : 3 + "y" : 3, + "drilldown" : "Thomas Kohler" }, { "name" : "Ulrich Rieke", @@ -102,13 +104,42 @@ "drilldown" : "Ulrich Rieke" }, { - "y" : 3, "name" : "W. Luis Mochan", - "drilldown" : "W. Luis Mochan" + "drilldown" : "W. Luis Mochan", + "y" : 3 } - ] + ], + "name" : "The Weekly Challenge - 196" } ], + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "tooltip" : { + "followPointer" : 1, + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>" + }, + "chart" : { + "type" : "column" + }, + "title" : { + "text" : "The Weekly Challenge - 196" + }, + "xAxis" : { + "type" : "category" + }, + "plotOptions" : { + "series" : { + "borderWidth" : 0, + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + } + } + }, "drilldown" : { "series" : [ { @@ -122,8 +153,8 @@ 2 ] ], - "name" : "Athanasius", - "id" : "Athanasius" + "id" : "Athanasius", + "name" : "Athanasius" }, { "data" : [ @@ -142,8 +173,8 @@ 2 ] ], - "name" : "Carlos Oliveira", - "id" : "Carlos Oliveira" + "id" : "Carlos Oliveira", + "name" : "Carlos Oliveira" }, { "id" : "Dave Jacoby", @@ -170,14 +201,14 @@ "id" : "David Ferrone" }, { - "id" : "E. Choroba", - "name" : "E. Choroba", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "E. Choroba", + "id" : "E. Choroba" }, { "data" : [ @@ -190,14 +221,32 @@ "id" : "Feng Chang" }, { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], "id" : "James Smith", - "name" : "James Smith", + "name" : "James Smith" + }, + { + "id" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld", "data" : [ [ "Perl", 2 ], [ + "Raku", + 2 + ], + [ "Blog", 1 ] @@ -214,8 +263,8 @@ 6 ] ], - "id" : "Luca Ferrari", - "name" : "Luca Ferrari" + "name" : "Luca Ferrari", + "id" : "Luca Ferrari" }, { "id" : "Mark Anderson", @@ -234,8 +283,8 @@ 2 ] ], - "name" : "Niels van Dijke", - "id" : "Niels van Dijke" + "id" : "Niels van Dijke", + "name" : "Niels van Dijke" }, { "data" : [ @@ -258,10 +307,30 @@ 2 ] ], + "id" : "Pip Stuart", + "name" : "Pip Stuart" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], "id" : "Robbie Hatley", "name" : "Robbie Hatley" }, { + "name" : "Robert DiCicco", + "id" : "Robert DiCicco", + "data" : [ + [ + "Raku", + 1 + ] + ] + }, + { "data" : [ [ "Perl", @@ -282,12 +351,30 @@ 2 ], [ + "Raku", + 2 + ], + [ "Blog", 1 ] ], + "id" : "Stephen G. Lynn", + "name" : "Stephen G. Lynn" + }, + { + "name" : "Thomas Kohler", "id" : "Thomas Kohler", - "name" : "Thomas Kohler" + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] }, { "data" : [ @@ -319,21 +406,10 @@ } ] }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" - } - }, - "title" : { - "text" : "The Weekly Challenge - 196" + "subtitle" : { + "text" : "[Champions: 21] Last updated at 2022-12-23 09:08:36 GMT" }, - "plotOptions" : { - "series" : { - "borderWidth" : 0, - "dataLabels" : { - "enabled" : 1, - "format" : "{point.y}" - } - } + "legend" : { + "enabled" : 0 } } diff --git a/stats/pwc-language-breakdown-summary.json b/stats/pwc-language-breakdown-summary.json index abdcd45936..86221ad59e 100644 --- a/stats/pwc-language-breakdown-summary.json +++ b/stats/pwc-language-breakdown-summary.json @@ -1,63 +1,63 @@ { - "tooltip" : { - "pointFormat" : "<b>{point.y:.0f}</b>" - }, - "xAxis" : { - "type" : "category", - "labels" : { - "style" : { - "fontFamily" : "Verdana, sans-serif", - "fontSize" : "13px" - } - } - }, - "legend" : { - "enabled" : "false" - }, - "subtitle" : { - "text" : "Last updated at 2022-12-22 11:23:26 GMT" - }, |
