From 7de47c639d3bc4fef60d8df2128c14049f47b1fd Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Wed, 14 Dec 2022 11:13:00 +0800 Subject: challenge 195, raku solutions --- challenge-195/feng-chang/raku/ch-1.raku | 5 +++++ challenge-195/feng-chang/raku/ch-2.raku | 5 +++++ 2 files changed, 10 insertions(+) create mode 100755 challenge-195/feng-chang/raku/ch-1.raku create mode 100755 challenge-195/feng-chang/raku/ch-2.raku diff --git a/challenge-195/feng-chang/raku/ch-1.raku b/challenge-195/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..bd2d46d759 --- /dev/null +++ b/challenge-195/feng-chang/raku/ch-1.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(UInt:D \n); + +put (1..n).grep({ .comb.unique.elems == .chars }).elems; diff --git a/challenge-195/feng-chang/raku/ch-2.raku b/challenge-195/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..0d127d657a --- /dev/null +++ b/challenge-195/feng-chang/raku/ch-2.raku @@ -0,0 +1,5 @@ +#!/bin/env raku + +unit sub MAIN(*@N); + +put @N».Int.grep(* %% 2).BagHash.sort({ -.value, .key }).first.key // -1; -- cgit From 5062b75e8b1cd965b5f6f80f4edac8b6e4121cf1 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 19 Dec 2022 09:49:35 +0100 Subject: Task 1 done --- challenge-196/luca-ferrari/raku/ch-1.p6 | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 challenge-196/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-196/luca-ferrari/raku/ch-1.p6 b/challenge-196/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..d416479573 --- /dev/null +++ b/challenge-196/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,15 @@ +#!raku + +# Perl Weekly Challenge 196 + +sub MAIN( *@list where { @list.grep( * ~~ Int ).elems == @list.elems } ) { + my @found; + my $last = 0; + + for @list.rotor( 3, :partial ) -> $triplet { + next if $triplet.elems != 3; + @found.push: $triplet if ( $triplet[ 0 ] < $triplet[ 1 ] < $triplet[ 2 ] ); + } + + @found.join( "\n" ).say; +} -- cgit From f8e4d9c51244dbc1c7bdcfc95977493a3e5f9ce0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 19 Dec 2022 10:10:43 +0100 Subject: Task 2 done --- challenge-196/luca-ferrari/raku/ch-2.p6 | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 challenge-196/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-196/luca-ferrari/raku/ch-2.p6 b/challenge-196/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..69289313d7 --- /dev/null +++ b/challenge-196/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,22 @@ +#!raku + +# Perl Weekly Challenge 196 + +sub MAIN( *@list where { @list.grep( * ~~ Int ).elems == @list.elems } ) { + + my @ranges; + my $start = -1; + my $end = -1; + for 0 .. @list.elems { + next if ! $_; + next if $_ <= $end; + + $start = $_; + $end = $start; + + $end++ while ( $end < @list.elems && @list[ $end + 1 ] == @list[ $end ] + 1 ); + @ranges.push: [ $start, $end ] if ( $start < $end ); + } + + @ranges.join( "\n" ).say; +} -- cgit From 5625ad93926298ceb5bcca92c5e7bdf9ccfd51a2 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 19 Dec 2022 10:23:41 +0100 Subject: Task 1 plperl done --- challenge-196/luca-ferrari/postgresql/ch-1.plperl | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 challenge-196/luca-ferrari/postgresql/ch-1.plperl diff --git a/challenge-196/luca-ferrari/postgresql/ch-1.plperl b/challenge-196/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..834e9828ab --- /dev/null +++ b/challenge-196/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,24 @@ +-- Perl Weekly Challenge 196 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc196; + +CREATE OR REPLACE FUNCTION +pwc196.task1_plperl( int[] ) +RETURNS SETOF int[] +AS $CODE$ + + my ( $array ) = $_[ 0 ]; + my $index = 1; + + while ( $index < $array->@* ) { + my @triplet = ( $array->[ $index - 1 ], $array->[ $index ], $array->[ $index + 1 ] ); + $index += 2 and return_next( [ @triplet ] ) if ( $tripet[ 0 ] < $triplet[ 1 ] + && $triplet[ 1 ] < $triplet[ 2 ] ); + $index++; + } + +return undef; + +$CODE$ +LANGUAGE plperl; -- cgit From 4ceffc0526ec27e141debda11936c854940ec637 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 19 Dec 2022 10:28:09 +0100 Subject: Task 2 done --- challenge-196/luca-ferrari/postgresql/ch-2.plperl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenge-196/luca-ferrari/postgresql/ch-2.plperl diff --git a/challenge-196/luca-ferrari/postgresql/ch-2.plperl b/challenge-196/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..2608c1a841 --- /dev/null +++ b/challenge-196/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,23 @@ +-- Perl Weekly Challenge 196 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc196; + +CREATE OR REPLACE FUNCTION +pwc196.task2_plperl( int[] ) +RETURNS SETOF int[] +AS $CODE$ + my ( $array ) = $_[0]; + my ( $start, $end ) = ( 0, 0 ); + + while ( $start < $array->@* ) { + $end = $start; + $end++ while ( $end < $array->@* && $array->[ $end + 1 ] == $array->[ $end ] + 1 ); + return_next( [ $start, $end ] ) if ( $end > $start ); + $start += $end + 1; + } + +return undef; + +$CODE$ +LANGUAGE plperl; -- cgit From e0a24a516ba1e989a07d48737c483bdd744fb48f Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 19 Dec 2022 10:33:20 +0100 Subject: Task 1 plpgsql done --- challenge-196/luca-ferrari/postgresql/ch-1.sql | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 challenge-196/luca-ferrari/postgresql/ch-1.sql diff --git a/challenge-196/luca-ferrari/postgresql/ch-1.sql b/challenge-196/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..370cbe363d --- /dev/null +++ b/challenge-196/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,27 @@ +-- Perl Weekly Challenge 196 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc196; + +CREATE OR REPLACE FUNCTION +pwc196.task1_plpgsql( l int[] ) +RETURNS SETOF int[] +AS $CODE$ +DECLARE + last_index int := 0; +BEGIN + + FOR i IN 1 .. array_length(l,1) - 1 LOOP + IF i <= last_index THEN + CONTINUE; + END IF; + + IF l[ i - 1 ] < l[ i ] AND l[ i ] < l[ i + 1 ] THEN + RETURN NEXT ARRAY[ l[i-1], l[i], l[i + 1] ]::int[]; + last_index := i + 1; + END IF; + END LOOP; +RETURN; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 22609ecbfcc4c5678a2a4c828f04ec8f3fb89bc1 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 19 Dec 2022 10:41:23 +0100 Subject: Task 2 plpgsql done --- challenge-196/luca-ferrari/postgresql/ch-2.sql | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 challenge-196/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-196/luca-ferrari/postgresql/ch-2.sql b/challenge-196/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..434ac42b31 --- /dev/null +++ b/challenge-196/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,35 @@ +-- Perl Weekly Challenge 196 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc196; + +CREATE OR REPLACE FUNCTION +pwc196.task2_plpgsql( l int[]) +RETURNS SETOF int[] +AS $CODE$ +DECLARE + c_start int := 0; + c_end int := 0; +BEGIN + + FOR i IN 0 .. array_length( l, 1 ) LOOP + IF i < c_end THEN + CONTINUE; + END IF; + + c_start := i; + c_end := c_start; + + WHILE c_end < array_length( l, 1 ) AND l[ c_end + 1 ] = l[ c_end ] + 1 LOOP + c_end := c_end + 1; + END LOOP; + + IF c_start < c_end THEN + RETURN NEXT ARRAY[ c_start, c_end ]::int[]; + END IF; + END LOOP; + +RETURN; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 93aa81cec3d670d325af56ec86d47adc2a677c2a Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 19 Dec 2022 10:57:11 +0000 Subject: Challenge 196 Solutions (Raku) --- challenge-196/mark-anderson/raku/ch-1.raku | 22 ++++++++++++++++++++++ challenge-196/mark-anderson/raku/ch-2.raku | 28 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 challenge-196/mark-anderson/raku/ch-1.raku create mode 100644 challenge-196/mark-anderson/raku/ch-2.raku diff --git a/challenge-196/mark-anderson/raku/ch-1.raku b/challenge-196/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..7b844a207b --- /dev/null +++ b/challenge-196/mark-anderson/raku/ch-1.raku @@ -0,0 +1,22 @@ +#!/usr/bin/env raku +use Test; + +is-deeply pattern-acb(3,1,4,2), (1,4,2); +is-deeply pattern-acb(1,2,3,4), (); +is-deeply pattern-acb(1,3,2,4,6,5), (1,3,2); + +sub pattern-acb(+$list) +{ + for 0..$list-1 -> $a + { + for $a+1..$list-1 -> $b + { + for $b+1..$list-1 -> $c + { + return $list[$a,$b,$c] if $list[$a] < $list[$c] < $list[$b] + } + } + } + + return () +} diff --git a/challenge-196/mark-anderson/raku/ch-2.raku b/challenge-196/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..64ead6051b --- /dev/null +++ b/challenge-196/mark-anderson/raku/ch-2.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku +use Test; + +is-deeply range-list(1,3,4,5,7), ((3,5),); +is-deeply range-list(1,2,3,6,7,9), ((1,3), (6,7)); +is-deeply range-list(0,1,2,4,5,6,8,9), ((0,2), (4,6), (8,9)); + +sub range-list(+$list) +{ + my ($h, $t) = (0, 0); + + my $a := gather for $list.rotor(2 => -1, :partial) + { + if .tail - .head == 1 + { + $t++ + } + + else + { + take $h, $t; + $t++; + $h = $t; + } + } + + $list[ $a.grep({ .head < .tail }) ] +} -- cgit From 56167154e412f67b86fe19cd6fd201868c0d0476 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 19 Dec 2022 10:58:05 +0000 Subject: Solutions for challenge #196 --- challenge-196/roger-bell-west/javascript/ch-1.js | 81 ++++++++++++++ challenge-196/roger-bell-west/javascript/ch-2.js | 70 ++++++++++++ challenge-196/roger-bell-west/kotlin/ch-1.kt | 52 +++++++++ challenge-196/roger-bell-west/kotlin/ch-2.kt | 41 +++++++ challenge-196/roger-bell-west/lua/ch-1.lua | 65 +++++++++++ challenge-196/roger-bell-west/lua/ch-2.lua | 62 +++++++++++ challenge-196/roger-bell-west/perl/ch-1.pl | 30 ++++++ challenge-196/roger-bell-west/perl/ch-2.pl | 31 ++++++ challenge-196/roger-bell-west/postscript/ch-1.ps | 132 +++++++++++++++++++++++ challenge-196/roger-bell-west/postscript/ch-2.ps | 124 +++++++++++++++++++++ challenge-196/roger-bell-west/python/ch-1.py | 34 ++++++ challenge-196/roger-bell-west/python/ch-2.py | 30 ++++++ challenge-196/roger-bell-west/raku/ch-1.p6 | 28 +++++ challenge-196/roger-bell-west/raku/ch-2.p6 | 29 +++++ challenge-196/roger-bell-west/ruby/ch-1.rb | 45 ++++++++ challenge-196/roger-bell-west/ruby/ch-2.rb | 37 +++++++ challenge-196/roger-bell-west/rust/ch-1.rs | 39 +++++++ challenge-196/roger-bell-west/rust/ch-2.rs | 39 +++++++ 18 files changed, 969 insertions(+) create mode 100755 challenge-196/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-196/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-196/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-196/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-196/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-196/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-196/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-196/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-196/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-196/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-196/roger-bell-west/python/ch-1.py create mode 100755 challenge-196/roger-bell-west/python/ch-2.py create mode 100755 challenge-196/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-196/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-196/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-196/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-196/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-196/roger-bell-west/rust/ch-2.rs diff --git a/challenge-196/roger-bell-west/javascript/ch-1.js b/challenge-196/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..1e849d686e --- /dev/null +++ b/challenge-196/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,81 @@ +#! /usr/bin/node + +"use strict" + +// by Frank Tan +// https://stackoverflow.com/questions/38400594/javascript-deep-comparison +function deepEqual(a,b) +{ + if( (typeof a == 'object' && a != null) && + (typeof b == 'object' && b != null) ) + { + var count = [0,0]; + for( var key in a) count[0]++; + for( var key in b) count[1]++; + if( count[0]-count[1] != 0) {return false;} + for( var key in a) + { + if(!(key in b) || !deepEqual(a[key],b[key])) {return false;} + } + for( var key in b) + { + if(!(key in a) || !deepEqual(b[key],a[key])) {return false;} + } + return true; + } + else + { + return a === b; + } +} + +function pattern132(l) { + let o = []; + for (let a = 0; a < l.length-2; a++) { + for (let b = a+1; b < l.length-1; b++) { + if (l[a] < l[b]) { + for (let c = b+1; c < l.length; c++) { + if (l[b] > l[c] && l[a] < l[c]) { + o = [l[a], l[b], l[c]] + break + } + } + } + if (o.length > 0) { + break + } + } + if (o.length > 0) { + break + } + } + return o; +} + +if (deepEqual(pattern132([3, 1, 4, 2]), [1, 4, 2])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (deepEqual(pattern132([1, 2, 3, 4]), [])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (deepEqual(pattern132([1, 3, 2, 4, 6, 5]), [1, 3, 2])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (deepEqual(pattern132([1, 3, 4, 2]), [1, 3, 2])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-196/roger-bell-west/javascript/ch-2.js b/challenge-196/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..6a6650fa70 --- /dev/null +++ b/challenge-196/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,70 @@ +#! /usr/bin/node + +"use strict" + +// by Frank Tan +// https://stackoverflow.com/questions/38400594/javascript-deep-comparison +function deepEqual(a,b) +{ + if( (typeof a == 'object' && a != null) && + (typeof b == 'object' && b != null) ) + { + var count = [0,0]; + for( var key in a) count[0]++; + for( var key in b) count[1]++; + if( count[0]-count[1] != 0) {return false;} + for( var key in a) + { + if(!(key in b) || !deepEqual(a[key],b[key])) {return false;} + } + for( var key in b) + { + if(!(key in a) || !deepEqual(b[key],a[key])) {return false;} + } + return true; + } + else + { + return a === b; + } +} + +function rangelist(l) { + let o = []; + let start = l[0]; + let prev = start; + for (let v of l.slice(1)) { + if (v != prev + 1) { + if (prev > start) { + o.push([start, prev]); + } + start = v; + } + prev = v; + } + if (prev > start) { + o.push([start, prev]); + } + return o; +} + +if (deepEqual(rangelist([1, 3, 4, 5, 7]), [[3, 5]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (deepEqual(rangelist([1, 2, 3, 6, 7, 9]), [[1, 3], [6, 7]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (deepEqual(rangelist([0, 1, 2, 4, 5, 6, 8, 9]), [[0, 2], [4, 6], [8, 9]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-196/roger-bell-west/kotlin/ch-1.kt b/challenge-196/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..c50967a018 --- /dev/null +++ b/challenge-196/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,52 @@ +fun pattern132(l: List): List { + var o = ArrayList() + for (a in 0..l.size-3) { + for (b in a+1..l.size-2) { + if (l[a] < l[b]) { + for (c in b+1..l.size-1) { + if (l[b] > l[c] && l[a] < l[c]) { + o = arrayListOf(l[a], l[b], l[c]) + break + } + } + } + if (o.size > 0) { + break + } + } + if (o.size > 0) { + break + } + } + return o.toList() +} + +fun main() { + if (pattern132(listOf(3, 1, 4, 2)) == listOf(1, 4, 2)) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + + if (pattern132(listOf(1, 2, 3, 4)) == emptyList()) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + + if (pattern132(listOf(1, 3, 2, 4, 6, 5)) == listOf(1, 3, 2)) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + + if (pattern132(listOf(1, 3, 4, 2)) == listOf(1, 3, 2)) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-196/roger-bell-west/kotlin/ch-2.kt b/challenge-196/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..88d9015059 --- /dev/null +++ b/challenge-196/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,41 @@ +fun rangelist(l: List): List> { + var o = ArrayList>() + var start = l[0] + var prev = start + for (v in l.drop(1)) { + if (v != prev + 1) { + if (prev > start) { + o.add(listOf(start, prev)) + } + start = v + } + prev = v + } + if (prev > start) { + o.add(listOf(start, prev)) + } + return o.toList() +} + +fun main() { + if (rangelist(listOf(1, 3, 4, 5, 7)) == listOf(listOf(3, 5))) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (rangelist(listOf(1, 2, 3, 6, 7, 9)) == + listOf(listOf(1, 3), listOf(6, 7))) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (rangelist(listOf(0, 1, 2, 4, 5, 6, 8, 9)) == + listOf(listOf(0, 2), listOf(4, 6), listOf(8, 9))) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-196/roger-bell-west/lua/ch-1.lua b/challenge-196/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..df1f8cbced --- /dev/null +++ b/challenge-196/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,65 @@ +#! /usr/bin/lua +function recursive_compare(t1,t2) + if t1==t2 then return true end + if (type(t1)~="table") then return false end + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + for k1,v1 in pairs(t1) do + local v2 = t2[k1] + if( not recursive_compare(v1,v2) ) then return false end + end + for k2,v2 in pairs(t2) do + local v1 = t1[k2] + if( not recursive_compare(v1,v2) ) then return false end + end + return true +end + +function pattern132(l) + local o = {} + for a = 1, #l-2 do + for b = a+1, #l-1 do + if l[a] < l[b] then + for c = b+1, #l do + if l[b] > l[c] and l[a] < l[c] then + o = {l[a], l[b], l[c]} + break + end + end + end + if #o > 0 then + break + end + end + if #o > 0 then + break + end + end + return o +end + +if recursive_compare(pattern132({3, 1, 4, 2}), {1, 4, 2}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") +if recursive_compare(pattern132({1, 2, 3, 4}), {}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") +if recursive_compare(pattern132({1, 3, 2, 4, 6, 5}), {1, 3, 2}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") +if recursive_compare(pattern132({1, 3, 4, 2}), {1, 3, 2}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-196/roger-bell-west/lua/ch-2.lua b/challenge-196/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..720f832be0 --- /dev/null +++ b/challenge-196/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,62 @@ +#! /usr/bin/lua + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +function recursive_compare(t1,t2) + if t1==t2 then return true end + if (type(t1)~="table") then return false end + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + for k1,v1 in pairs(t1) do + local v2 = t2[k1] + if( not recursive_compare(v1,v2) ) then return false end + end + for k2,v2 in pairs(t2) do + local v1 = t1[k2] + if( not recursive_compare(v1,v2) ) then return false end + end + return true +end + +function rangelist(l) + local o = {} + local start = l[1] + local prev = start + for i, v in ipairs(l) do + if i > 1 then + if v ~= prev + 1 then + if prev > start then + table.insert(o, {start, prev}) + end + start = v + end + prev = v + end + end + if prev > start then + table.insert(o, {start, prev}) + end + return o +end + +if recursive_compare(rangelist({1, 3, 4, 5, 7}), {{3, 5}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(rangelist({1, 2, 3, 6, 7, 9}), {{1, 3}, {6, 7}}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(rangelist({0, 1, 2, 4, 5, 6, 8, 9}), {{0, 2}, {4, 6}, {8, 9}}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-196/roger-bell-west/perl/ch-1.pl b/challenge-196/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..e94392a764 --- /dev/null +++ b/challenge-196/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,30 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 4; + +is_deeply(pattern132([3, 1, 4, 2]), [1, 4, 2], 'example 1'); +is_deeply(pattern132([1, 2, 3, 4]), [], 'example 2'); +is_deeply(pattern132([1, 3, 2, 4, 6, 5]), [1, 3, 2], 'example 3'); +is_deeply(pattern132([1, 3, 4, 2]), [1, 3, 2], 'example 4'); + +sub pattern132($l) { + my $o = []; + OUTER: + foreach my $a (0..$#{$l}-2) { + foreach my $b ($a+1..$#{$l}-1) { + if ($l->[$a] < $l->[$b]) { + foreach my $c ($b+1..$#{$l}) { + if ($l->[$b] > $l->[$c] && $l->[$a] < $l->[$c]) { + $o = [$l->[$a], $l->[$b], $l->[$c]]; + last OUTER; + } + } + } + } + } + return $o; +} diff --git a/challenge-196/roger-bell-west/perl/ch-2.pl b/challenge-196/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..c1c43fb17a --- /dev/null +++ b/challenge-196/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,31 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is_deeply(rangelist([1, 3, 4, 5, 7]), [[3, 5]], 'example 1'); +is_deeply(rangelist([1, 2, 3, 6, 7, 9]), [[1, 3], [6, 7]], 'example 2'); +is_deeply(rangelist([0, 1, 2, 4, 5, 6, 8, 9]), + [[0, 2], [4, 6], [8, 9]], 'example 3'); + +sub rangelist($l) { + my @o; + my $start = $l->[0]; + my $prev = $start; + foreach my $v (@{$l}[1..$#{$l}]) { + if ($v != $prev + 1) { + if ($prev > $start) { + push @o, [$start, $prev]; + } + $start = $v; + } + $prev = $v; + } + if ($prev > $start) { + push @o, [$start, $prev]; + } + return \@o; +} diff --git a/challenge-196/roger-bell-west/postscript/ch-1.ps b/challenge-196/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..2d3d58636b --- /dev/null +++ b/challenge-196/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,132 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/deepeq { + 2 dict begin + /a exch def + /b exch def + a type b type eq { + a type /dicttype eq { + a length b length eq { + << + a { + pop + true + } forall + b { + pop + true + } forall + >> + true exch + { + pop + dup a exch known { + dup b exch known { + dup a exch get exch b exch get deepeq not { + pop false + } if + } { + false + } ifelse + } { + false + } ifelse + } forall + } { + false + } ifelse + } { + a type dup /arraytype eq exch /stringtype eq or { + a length b length eq { + true + 0 1 a length 1 sub { + dup a exch get exch b exch get deepeq not { + pop false + exit + } if + } for + } { + false + } ifelse + } { + a b eq + } ifelse + } ifelse + } { + false + } ifelse + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + + +% end included library code + +/pattern132 { + 5 dict begin + /l exch def + /o 0 array def + 0 1 l length 3 sub { + /a exch def + a 1 add 1 l length 2 sub { + /b exch def + l a get l b get lt { + b 1 add 1 l length 1 sub { + /c exch def + l b get l c get gt + l a get l c get lt and { + /o [ l a get l b get l c get ] def + exit + } if + } for + } if + o length 0 gt { + exit + } if + } for + o length 0 gt { + exit + } if + } for + o + end +} bind def + +(pattern132) test.start +[ 3 1 4 2 ] pattern132 [ 1 4 2 ] deepeq test +[ 1 2 3 4 ] pattern132 [ ] deepeq test +[ 1 3 2 4 6 5 ] pattern132 [ 1 3 2 ] deepeq test +[ 1 3 4 2 ] pattern132 [ 1 3 2 ] deepeq test +test.end diff --git a/challenge-196/roger-bell-west/postscript/ch-2.ps b/challenge-196/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..141fbb5737 --- /dev/null +++ b/challenge-196/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,124 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/deepeq { + 2 dict begin + /a exch def + /b exch def + a type b type eq { + a type /dicttype eq { + a length b length eq { + << + a { + pop + true + } forall + b { + pop + true + } forall + >> + true exch + { + pop + dup a exch known { + dup b exch known { + dup a exch get exch b exch get deepeq not { + pop false + } if + } { + false + } ifelse + } { + false + } ifelse + } forall + } { + false + } ifelse + } { + a type dup /arraytype eq exch /stringtype eq or { + a length b length eq { + true + 0 1 a length 1 sub { + dup a exch get exch b exch get deepeq not { + pop false + exit + } if + } for + } { + false + } ifelse + } { + a b eq + } ifelse + } ifelse + } { + false + } ifelse + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + + +% end included library code + +/rangelist { + 4 dict begin + /l exch def + /start l 0 get def + /prev start def + [ + l 1 l length 1 sub getinterval { + /v exch def + v prev 1 add ne { + prev start gt { + [ start prev ] + } if + /start v def + } if + /prev v def + } forall + prev start gt { + [ start prev ] + } if + ] + end +} bind def + +(rangelist) test.start +[ 1 3 4 5 7 ] rangelist [ [ 3 5 ] ] deepeq test +[ 1 2 3 6 7 9 ] rangelist [ [ 1 3 ] [ 6 7 ] ] deepeq test +[ 0 1 2 4 5 6 8 9 ] rangelist [ [ 0 2 ] [ 4 6 ] [ 8 9 ] ] deepeq test +test.end diff --git a/challenge-196/roger-bell-west/python/ch-1.py b/challenge-196/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..8cdc462919 --- /dev/null +++ b/challenge-196/roger-bell-west/python/ch-1.py @@ -0,0 +1,34 @@ +#! /usr/bin/python3 + +import unittest + +def pattern132(l): + o = [] + for a in range(0, len(l) - 2): + for b in range(a + 1, len(l) - 1): + if l[a] < l[b]: + for c in range(b + 1, len(l)): + if l[b] > l[c] and l[a] < l[c]: + o = [l[a], l[b], l[c]] + break + if len(o) > 0: + break + if len(o) > 0: + break + return o + +class TestPattern132(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(pattern132([3, 1, 4, 2]), [1, 4, 2], 'example 1') + + def test_ex2(self): + self.assertEqual(pattern132([1, 2, 3, 4]), [], 'example 2') + + def test_ex3(self): + self.assertEqual(pattern132([1, 3, 2, 4, 6, 5]), [1, 3, 2], 'example 3') + + def test_ex4(self): + self.assertEqual(pattern132([1, 3, 4, 2]), [1, 3, 2], 'example 4') + +unittest.main() diff --git a/challenge-196/roger-bell-west/python/ch-2.py b/challenge-196/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..caece39ab6 --- /dev/null +++ b/challenge-196/roger-bell-west/python/ch-2.py @@ -0,0 +1,30 @@ +#! /usr/bin/python3 + +import unittest + +def rangelist(l): + o = [] + start = l[0] + prev = start + for v in l[1:]: + if v != prev + 1: + if prev > start: + o.append([start, prev]) + start = v + prev = v + if prev > start: + o.append([start, prev]) + return o + +class TestRangelist(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(rangelist([1, 3, 4, 5, 7]), [[3, 5]], 'example 1') + + def test_ex2(self): + self.assertEqual(rangelist([1, 2, 3, 6, 7, 9]), [[1, 3], [6, 7]], 'example 2') + + def test_ex3(self): + self.assertEqual(rangelist([0, 1, 2, 4, 5, 6, 8, 9]), [[0, 2], [4, 6], [8, 9]], 'example 3') + +unittest.main() diff --git a/challenge-196/roger-bell-west/raku/ch-1.p6 b/challenge-196/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..76b893074b --- /dev/null +++ b/challenge-196/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,28 @@ +#! /usr/bin/perl6 + +use Test; + +plan 4; + +is-deeply(pattern132([3, 1, 4, 2]), [1, 4, 2], 'example 1'); +is-deeply(pattern132([1, 2, 3, 4]), [], 'example 2'); +is-deeply(pattern132([1, 3, 2, 4, 6, 5]), [1, 3, 2], 'example 3'); +is-deeply(pattern132([1, 3, 4, 2]), [1, 3, 2], 'example 4'); + +sub pattern132(@l) { + my @o = []; + OLOOP: + for (0..@l.elems-3) -> $a { + for ($a+1..@l.elems-2) -> $b { + if (@l[$a] < @l[$b]) { + for ($b+1..@l.elems-1) -> $c { + if (@l[$b] > @l[$c] && @l[$a] < @l[$c]) { + @o = [@l[$a], @l[$b], @l[$c]]; + last OLOOP; + } + } + } + } + } + return @o; +} diff --git a/challenge-196/roger-bell-west/raku/ch-2.p6 b/challenge-196/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..4ca03d53d5 --- /dev/null +++ b/challenge-196/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,29 @@ +#! /usr/bin/perl6 + +use Test; + +plan 3; + +is-deeply(rangelist([1, 3, 4, 5, 7]), [[3, 5],], 'example 1'); +is-deeply(rangelist([1, 2, 3, 6, 7, 9]), [[1, 3], [6, 7]], 'example 2'); +is-deeply(rangelist([0, 1, 2, 4, 5, 6, 8, 9]), + [[0, 2], [4, 6], [8, 9]], 'example 3'); + +sub rangelist(@l) { + my @o; + my $start = @l[0]; + my $prev = $start; + for (@l[1..*-1]) -> $v { + if ($v != $prev + 1) { + if ($prev > $start) { + push @o, [$start, $prev]; + } + $start = $v; + } + $prev = $v; + } + if ($prev > $start) { + push @o, [$start, $prev]; + } + return @o; +} diff --git a/challenge-196/roger-bell-west/ruby/ch-1.rb b/challenge-196/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..68aa927a4d --- /dev/null +++ b/challenge-196/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,45 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def pattern132(l) + o = [] + 0.upto(l.length-3) do |a| + (a+1).upto(l.length-2) do |b| + if l[a] < l[b] then + (b+1).upto(l.length-1) do |c| + if l[b] > l[c] && l[a] < l[c] then + o = [l[a], l[b], l[c]] + break + end + end + end + if o.length > 0 then + break + end + end + if o.length > 0 then + break + end + end + return o +end + +class TestPattern132 < Test::Unit::TestCase + + def test_ex1 + assert_equal([1, 4, 2], pattern132([3, 1, 4, 2])) + end + + def test_ex2 + assert_equal([], pattern132([1, 2, 3, 4])) + end + + def test_ex3 + assert_equal([1, 3, 2], pattern132([1, 3, 2, 4, 6, 5])) + end + + def test_ex4 + assert_equal([1, 3, 2], pattern132([1, 3, 4, 2])) + end +end diff --git a/challenge-196/roger-bell-west/ruby/ch-2.rb b/challenge-196/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..f33c64d4bb --- /dev/null +++ b/challenge-196/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,37 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def rangelist(l) + o = [] + start = l[0] + prev = start + l.slice(1, l.length-1).each do |v| + if v != prev + 1 then + if prev > start then + o.push([start, prev]) + end + start = v + end + prev = v + end + if prev > start then + o.push([start, prev]) + end + return o +end + +class TestRangeList < Test::Unit::TestCase + + def test_ex1 + assert_equal([[3, 5]], rangelist([1, 3, 4, 5, 7])) + end + + def test_ex2 + assert_equal([[1, 3], [6, 7]], rangelist([1, 2, 3, 6, 7, 9])) + end + + def test_ex3 + assert_equal([[0, 2], [4, 6], [8, 9]], rangelist([0, 1, 2, 4, 5, 6, 8, 9])) + end +end diff --git a/challenge-196/roger-bell-west/rust/ch-1.rs b/challenge-196/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..9b0db5eb4d --- /dev/null +++ b/challenge-196/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,39 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(pattern132(vec![3, 1, 4, 2]), vec![1, 4, 2]); +} + +#[test] +fn test_ex2() { + assert_eq!(pattern132(vec![1, 2, 3, 4]), vec![]); +} + +#[test] +fn test_ex3() { + assert_eq!(pattern132(vec![1, 3, 2, 4, 6, 5]), vec![1, 3, 2]); +} + +#[test] +fn test_ex4() { + assert_eq!(pattern132(vec![1, 3, 4, 2]), vec![1, 3, 2]); +} + +fn pattern132(l: Vec) -> Vec { + let mut o = Vec::new(); + 'outer: for a in 0..=l.len() - 3 { + for b in a + 1..=l.len() - 2 { + if l[a] < l[b] { + for c in b + 1..=l.len() - 1 { + if l[b] > l[c] && l[a] < l[c] { + o = vec![l[a], l[b], l[c]]; + break 'outer; + } + } + } + } + } + o +} diff --git a/challenge-196/roger-bell-west/rust/ch-2.rs b/challenge-196/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..6ebff610fc --- /dev/null +++ b/challenge-196/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,39 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(rangelist(vec![1, 3, 4, 5, 7]), vec![vec![3, 5]]); +} + +#[test] +fn test_ex2() { + assert_eq!(rangelist(vec![1, 2, 3, 6, 7, 9]), vec![vec![1, 3], vec![6, 7]]); +} + +#[test] +fn test_ex3() { + assert_eq!( + rangelist(vec![0, 1, 2, 4, 5, 6, 8, 9]), + vec![vec![0, 2], vec![4, 6], vec![8, 9]] + ); +} + +fn rangelist(l: Vec) -> Vec> { + let mut o = Vec::new(); + let mut start = l[0]; + let mut prev = start; + for &v in l.iter().skip(1) { + if v != prev + 1 { + if prev > start { + o.push(vec![start, prev]); + } + start = v; + } + prev = v; + } + if prev > start { + o.push(vec![start, prev]); + } + o +} -- cgit From 7d898e4caefaf4233111fdbe7e60ead3c50c6406 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 19 Dec 2022 11:03:19 +0000 Subject: Challenge 196 Solutions (Raku) --- challenge-196/mark-anderson/raku/ch-1.raku | 1 + 1 file changed, 1 insertion(+) diff --git a/challenge-196/mark-anderson/raku/ch-1.raku b/challenge-196/mark-anderson/raku/ch-1.raku index 7b844a207b..80b4f5fc45 100644 --- a/challenge-196/mark-anderson/raku/ch-1.raku +++ b/challenge-196/mark-anderson/raku/ch-1.raku @@ -4,6 +4,7 @@ use Test; is-deeply pattern-acb(3,1,4,2), (1,4,2); is-deeply pattern-acb(1,2,3,4), (); is-deeply pattern-acb(1,3,2,4,6,5), (1,3,2); +is-deeply pattern-acb(1,3,4,2), (1,3,2); sub pattern-acb(+$list) { -- cgit From b62bc69dd1a6d74e70775b4d6bbbe78ce1a4c7c8 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 19 Dec 2022 11:21:02 +0000 Subject: Challenge 196 Solutions (Raku) --- challenge-196/mark-anderson/raku/ch-2.raku | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-196/mark-anderson/raku/ch-2.raku b/challenge-196/mark-anderson/raku/ch-2.raku index 64ead6051b..763f3ed2f3 100644 --- a/challenge-196/mark-anderson/raku/ch-2.raku +++ b/challenge-196/mark-anderson/raku/ch-2.raku @@ -18,11 +18,11 @@ sub range-list(+$list) else { - take $h, $t; + take $h, $t if $h < $t; $t++; - $h = $t; + $h = $t } } - $list[ $a.grep({ .head < .tail }) ] + $list[$a] } -- cgit From bf09dffab19998192a22f0bffe40301fdddba555 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 19 Dec 2022 13:26:47 +0100 Subject: Blog references --- challenge-196/luca-ferrari/blog-1.txt | 1 + challenge-196/luca-ferrari/blog-2.txt | 1 + challenge-196/luca-ferrari/blog-3.txt | 1 + challenge-196/luca-ferrari/blog-4.txt | 1 + challenge-196/luca-ferrari/blog-5.txt | 1 + challenge-196/luca-ferrari/blog-6.txt | 1 + 6 files changed, 6 insertions(+) create mode 100644 challenge-196/luca-ferrari/blog-1.txt create mode 100644 challenge-196/luca-ferrari/blog-2.txt create mode 100644 challenge-196/luca-ferrari/blog-3.txt create mode 100644 challenge-196/luca-ferrari/blog-4.txt create mode 100644 challenge-196/luca-ferrari/blog-5.txt create mode 100644 challenge-196/luca-ferrari/blog-6.txt diff --git a/challenge-196/luca-ferrari/blog-1.txt b/challenge-196/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..a7710451a5 --- /dev/null +++ b/challenge-196/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/19/PerlWeeklyChallenge196.html#task1 diff --git a/challenge-196/luca-ferrari/blog-2.txt b/challenge-196/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..89664b33ac --- /dev/null +++ b/challenge-196/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/19/PerlWeeklyChallenge196.html#task2 diff --git a/challenge-196/luca-ferrari/blog-3.txt b/challenge-196/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..17c5988125 --- /dev/null +++ b/challenge-196/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/19/PerlWeeklyChallenge196.html#task1plperl diff --git a/challenge-196/luca-ferrari/blog-4.txt b/challenge-196/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..cd7e04a15d --- /dev/null +++ b/challenge-196/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/19/PerlWeeklyChallenge196.html#task2plperl diff --git a/challenge-196/luca-ferrari/blog-5.txt b/challenge-196/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..b8a724806e --- /dev/null +++ b/challenge-196/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/19/PerlWeeklyChallenge196.html#task1plpgsql diff --git a/challenge-196/luca-ferrari/blog-6.txt b/challenge-196/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..b9a2882050 --- /dev/null +++ b/challenge-196/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/12/19/PerlWeeklyChallenge196.html#task2plpgsql -- cgit From 009d9d87a5283900836fae97ca0550e03d912b87 Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 19 Dec 2022 15:13:06 +0100 Subject: Add solution for challenge 196. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-196/jeanluc2020/blog.txt | 1 + challenge-196/jeanluc2020/perl/ch-1.pl | 37 +++++++++++++++++++++++ challenge-196/jeanluc2020/perl/ch-2.pl | 54 ++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 challenge-196/jeanluc2020/blog.txt create mode 100755 challenge-196/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-196/jeanluc2020/perl/ch-2.pl diff --git a/challenge-196/jeanluc2020/blog.txt b/challenge-196/jeanluc2020/blog.txt new file mode 100644 index 0000000000..02069106c4 --- /dev/null +++ b/challenge-196/jeanluc2020/blog.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-196.html diff --git a/challenge-196/jeanluc2020/perl/ch-1.pl b/challenge-196/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..0a7d0dadee --- /dev/null +++ b/challenge-196/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl +use strict; +use warnings; + +# put all examples into an array +my $lists = [ + [3, 1, 4, 2], + [1, 2, 3, 4], + [1, 3, 2, 4, 6, 5], + [1, 3, 4, 2], + [], + [1, 2] +]; + +# iterate over all examples and get the results, then print them +foreach my $list (@$lists) { + my @result = pattern_132(@$list); + print "(" . join(", ", @$list) . ") returns (" . join(", ", @result) . ")\n"; +} + +# function to get the output for a given list +sub pattern_132 { + my @list = @_; + my $last = $#list; # get index of last element + # by iterating $i from 0 to $last, $j from $i+1 to $last and $k from $j+1 to + # $last, we have $i<$j<$k automatically. + foreach my $i (0..$last) { + foreach my $j ($i+1..$last) { + foreach my $k ($j+1..$last) { + # if both $list[$i] < $list[$k] and $list[$k] < $list[$j] we have + # found the first solution, so return it since we're done + return ($list[$i], $list[$j], $list[$k]) if $list[$i] < $list[$k] && $list[$k] < $list[$j]; + } + } + } + return (); +} diff --git a/challenge-196/jeanluc2020/perl/ch-2.pl b/challenge-196/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..ce88eb9a6d --- /dev/null +++ b/challenge-196/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl +use strict; +use warnings; + +# put all examples into an array +my @lists = ( + [1,3,4,5,7], + [1,2,3,6,7,9], + [0,1,2,4,5,6,8,9] +); + +# iterate over all examples and get the results, then print them +foreach my $list (@lists) { + my @ranges = ranges(@$list); + print "(" . join(", ", @$list) . ") has ranges " . join(", ", @ranges) . "\n"; +} + +# function to get the output for a given list +sub ranges { + my @array = @_; + my @result; + my $last = undef; # for last element during iteration + my $begin = undef; # for last begin of a range + foreach my $elem (@array) { + if(defined($last)) { + if($last+1 == $elem) { + # We're in a range. + # If $begin wasn't defined, we can set it to $last + $begin //= $last; + # Since we're in a range, we can set $last to the current element + # for the next iteration + $last = $elem; + } else { + # we're no longer in a range, or we weren't in one in the + # first place + if($begin != $last) { + # we were in a range, so put the range into the @result + push @result, "[$begin,$last]"; + } + # Since we're now not in a range, let's start from scratch + $begin = $last = $elem; + } + } else { + # $last not defined, so first element in the list + # initialize both $last and $begin with the current element + $begin = $last = $elem; + } + } + if($begin != $last) { + # we didn't save the last range in the @result yet, so let's do it now + push @result, "[$begin,$last]"; + } + return @result; +} -- cgit From 80b02f0b8318ed6894e5f80f8fce6df973288d44 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 19 Dec 2022 14:33:36 +0000 Subject: - Added solutions by James Smith. - Added solutions by Roger Bell_West. - Added solutions by Mark Anderson. - Added solutions by Luca Ferarri. - Added solutions by Thomas Kohler. --- stats/pwc-challenge-195.json | 646 +++++++++++ stats/pwc-current.json | 583 +--------- stats/pwc-language-breakdown-summary.json | 68 +- stats/pwc-language-breakdown.json | 1439 ++++++++++++------------ stats/pwc-leaders.json | 388 +++---- stats/pwc-summary-1-30.json | 54 +- stats/pwc-summary-121-150.json | 122 +- stats/pwc-summary-151-180.json | 38 +- stats/pwc-summary-181-210.json | 28 +- stats/pwc-summary-211-240.json | 44 +- stats/pwc-summary-241-270.json | 104 +- stats/pwc-summary-271-300.json | 64 +- stats/pwc-summary-31-60.json | 34 +- stats/pwc-summary-61-90.json | 106 +- stats/pwc-summary-91-120.json | 50 +- stats/pwc-summary.json | 1728 ++++++++++++++--------------- 16 files changed, 2829 insertions(+), 2667 deletions(-) create mode 100644 stats/pwc-challenge-195.json diff --git a/stats/pwc-challenge-195.json b/stats/pwc-challenge-195.json new file mode 100644 index 0000000000..5c5ca17a01 --- /dev/null +++ b/stats/pwc-challenge-195.json @@ -0,0 +1,646 @@ +{ + "chart" : { + "type" : "column" + }, + "legend" : { + "enabled" : 0 + }, + "drilldown" : { + "series" : [ + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 2 + ] + ], + "name" : "Adam Russell", + "id" : "Adam Russell" + }, + { + "id" : "Ali Moradi", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "name" : "Ali Moradi" + }, + { + "name" : "Arne Sommer", + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Arne Sommer" + }, + { + "id" : "Athanasius", + "name" : "Athanasius", + "data" : [ + [ + "Perl", + 1 + ], + [ + "Raku", + 1 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Bruce Gray", + "id" : "Bruce Gray" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Carlos Oliveira", + "id" : "Carlos Oliveira" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Cheok-Yin Fung", + "id" : "Cheok-Yin Fung" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Colin Crain", + "id" : "Colin Crain" + }, + { + "id" : "Daniel Pfeiffer", + "name" : "Daniel Pfeiffer", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "name" : "Dave Jacoby", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Dave Jacoby" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "David Ferrone", + "id" : "David Ferrone" + }, + { + "id" : "Duncan C. White", + "name" : "Duncan C. White", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "id" : "E. Choroba", + "name" : "E. Choroba", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "id" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "James Smith", + "id" : "James Smith" + }, + { + "data" : [ + [ + "Raku", + 1 + ] + ], + "name" : "Jan Krnavek", + "id" : "Jan Krnavek" + }, + { + "name" : "Jorg Sommrey", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Jorg Sommrey" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Laurent Rosenfeld", + "id" : "Laurent Rosenfeld" + }, + { + "id" : "Luca Ferrari", + "name" : "Luca Ferrari", + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 6 + ] + ] + }, + { + "id" : "Mark Anderson", + "name" : "Mark Anderson", + "data" : [ + [ + "Raku", + 2 + ] + ] + }, + { + "name" : "Marton Polgar", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Marton Polgar" + }, + { + "name" : "Niels van Dijke", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Niels van Dijke" + }, + { + "name" : "Olivier Delouya", + "data" : [ + [ + "Perl", + 1 + ] + ], + "id" : "Olivier Delouya" + }, + { + "id" : "Peter Campbell Smith", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Peter Campbell Smith" + }, + { + "id" : "Pip Stuart", + "name" : "Pip Stuart", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "name" : "Robbie Hatley", + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Robbie Hatley" + }, + { + "name" : "Robert DiCicco", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 1 + ] + ], + "id" : "Robert DiCicco" + }, + { + "name" : "Robert Ransbottom", + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Robert Ransbottom" + }, + { + "name" : "Roger Bell_West", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Roger Bell_West" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Solathian", + "id" : "Solathian" + }, + { + "id" : "Stephen G. Lynn", + "name" : "Stephen G. Lynn", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Thomas Kohler", + "id" : "Thomas Kohler" + }, + { + "id" : "Ulrich Rieke", + "name" : "Ulrich Rieke", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ] + }, + { + "id" : "W. Luis Mochan", + "name" : "W. Luis Mochan", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + } + ] + }, + "tooltip" : { + "headerFormat" : "{series.name}
", + "followPointer" : 1, + "pointFormat" : "{point.name}: {point.y:f}
" + }, + "xAxis" : { + "type" : "category" + }, + "series" : [ + { + "data" : [ + { + "drilldown" : "Adam Russell", + "name" : "Adam Russell", + "y" : 4 + }, + { + "drilldown" : "Ali Moradi", + "name" : "Ali Moradi", + "y" : 4 + }, + { + "y" : 3, + "name" : "Arne Sommer", + "drilldown" : "Arne Sommer" + }, + { + "drilldown" : "Athanasius", + "name" : "Athanasius", + "y" : 2 + }, + { + "drilldown" : "Bruce Gray", + "name" : "Bruce Gray", + "y" : 5 + }, + { + "y" : 2, + "drilldown" : "Carlos Oliveira", + "name" : "Carlos Oliveira" + }, + { + "y" : 2, + "drilldown" : "Cheok-Yin Fung", + "name" : "Cheok-Yin Fung" + }, + { + "name" : "Colin Crain", + "drilldown" : "Colin Crain", + "y" : 3 + }, + { + "drilldown" : "Daniel Pfeiffer", + "name" : "Daniel Pfeiffer", + "y" : 3 + }, + { + "drilldown" : "Dave Jacoby", + "name" : "Dave Jacoby", + "y" : 2 + }, + { + "drilldown" : "David Ferrone", + "name" : "David Ferrone", + "y" : 2 + }, + { + "drilldown" : "Duncan C. White", + "name" : "Duncan C. White", + "y" : 2 + }, + { + "name" : "E. Choroba", + "drilldown" : "E. Choroba", + "y" : 2 + }, + { + "y" : 5, + "name" : "Jaldhar H. Vyas", + "drilldown" : "Jaldhar H. Vyas" + }, + { + "y" : 3, + "name" : "James Smith", + "drilldown" : "James Smith" + }, + { + "y" : 1, + "drilldown" : "Jan Krnavek", + "name" : "Jan Krnavek" + }, + { + "y" : 2, + "drilldown" : "Jorg Sommrey", + "name" : "Jorg Sommrey" + }, + { + "y" : 5, + "name" : "Laurent Rosenfeld", + "drilldown" : "Laurent Rosenfeld" + }, + { + "name" : "Luca Ferrari", + "drilldown" : "Luca Ferrari", + "y" : 8 + }, + { + "drilldown" : "Mark Anderson", + "name" : "Mark Anderson", + "y" : 2 + }, + { + "name" : "Marton Polgar", + "drilldown" : "Marton Polgar", + "y" : 2 + }, + { + "y" : 2, + "drilldown" : "Niels van Dijke", + "name" : "Niels van Dijke" + }, + { + "drilldown" : "Olivier Delouya", + "name" : "Olivier Delouya", + "y" : 1 + }, + { + "drilldown" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith", + "y" : 3 + }, + { + "name" : "Pip Stuart", + "drilldown" : "Pip Stuart", + "y" : 2 + }, + { + "name" : "Robbie Hatley", + "drilldown" : "Robbie Hatley", + "y" : 2 + }, + { + "y" : 3, + "name" : "Robert DiCicco", + "drilldown" : "Robert DiCicco" + }, + { + "y" : 2, + "drilldown" : "Robert Ransbottom", + "name" : "Robert Ransbottom" + }, + { + "y" : 5, + "drilldown" : "Roger Bell_West", + "name" : "Roger Bell_West" + }, + { + "name" : "Solathian", + "drilldown" : "Solathian", + "y" : 2 + }, + { + "name" : "Stephen G. Lynn", + "drilldown" : "Stephen G. Lynn", + "y" : 5 + }, + { + "name" : "Thomas Kohler", + "drilldown" : "Thomas Kohler", + "y" : 2 + }, + { + "name" : "Ulrich Rieke", + "drilldown" : "Ulrich Rieke", + "y" : 4 + }, + { + "name" : "W. Luis Mochan", + "drilldown" : "W. Luis Mochan", + "y" : 3 + } + ], + "name" : "The Weekly Challenge - 195", + "colorByPoint" : 1 + } + ], + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "title" : { + "text" : "The Weekly Challenge - 195" + }, + "plotOptions" : { + "series" : { + "dataLabels" : { + "format" : "{point.y}", + "enabled" : 1 + }, + "borderWidth" : 0 + } + }