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 (limited to 'challenge-196') 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 (limited to 'challenge-196') 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 (limited to 'challenge-196') 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 (limited to 'challenge-196') 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 (limited to 'challenge-196') 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 (limited to 'challenge-196') 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 (limited to 'challenge-196') 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 (limited to 'challenge-196') 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(+) (limited to 'challenge-196') 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(-) (limited to 'challenge-196') 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 (limited to 'challenge-196') 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 (limited to 'challenge-196') 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 678ee3b1fabdf844938495b670377ff31e03f926 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 19 Dec 2022 11:15:50 -0500 Subject: Week 196 --- challenge-196/zapwai/perl/ch-1.pl | 28 ++++++++++++++++++++++++++++ challenge-196/zapwai/perl/ch-2.pl | 30 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 challenge-196/zapwai/perl/ch-1.pl create mode 100644 challenge-196/zapwai/perl/ch-2.pl (limited to 'challenge-196') diff --git a/challenge-196/zapwai/perl/ch-1.pl b/challenge-196/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..f7eb7356c5 --- /dev/null +++ b/challenge-196/zapwai/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +my @list; +my @testlist = ( [3,1,4,2], + [1,2,3,4], + [1,3,2,4,6,5], + [1,3,4,2], + ); +for (@testlist) { + @list = @{$_}; + driver(); +} +sub driver { + print "Input: (".join(",",@list).")\n"; + print "Output: "; + + for my $i (0 .. $#list - 2) { + for my $j ($i + 1 .. $#list -1) { + for my $k ($j + 1 .. $#list) { + if ( ($list[$i] < $list[$k]) && + ($list[$k] < $list[$j]) ) { + print "(", join(",",@list[$i, $j, $k]), ")\n"; + return; + } + } + } + } + print "()\n"; +} diff --git a/challenge-196/zapwai/perl/ch-2.pl b/challenge-196/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..640b34688e --- /dev/null +++ b/challenge-196/zapwai/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +my @list1 = (1,3,4,5,7); +my @list2 = (1,2,3,6,7,9); +my @list3 = (0,1,2,4,5,6,8,9); +my @lists = (\@list1, \@list2, \@list3); + +for my $ref (@lists) { + my @list = @{$ref}; + print "Input: (".join(",",@list).")\n"; + print "Output: "; + + my $range_flag = 0; + my $comma_flag = 0; + for my $index (0 .. $#list) { + if ($list[$index + 1] == $list[$index] + 1) { + if (!$range_flag) { + $range_flag = 1; + print ", " if ($comma_flag); + print "[$list[$index],"; + } + } else { + if ($range_flag) { + $range_flag = 0; + $comma_flag = 1; + print "$list[$index]]"; + } + } + } + print "\n"; +} -- cgit From 59e68fd44ac07d019225b01a7af4db0fa9970385 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 19 Dec 2022 17:24:08 +0000 Subject: w196 - Task 1 & 2 --- challenge-196/perlboy1967/perl/ch-1.pl | 55 ++++++++++++++++++++++++++++++++++ challenge-196/perlboy1967/perl/ch-2.pl | 53 ++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100755 challenge-196/perlboy1967/perl/ch-1.pl create mode 100755 challenge-196/perlboy1967/perl/ch-2.pl (limited to 'challenge-196') diff --git a/challenge-196/perlboy1967/perl/ch-1.pl b/challenge-196/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..8587eee3ab --- /dev/null +++ b/challenge-196/perlboy1967/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 196 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-195/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Pattern 132 +Submitted by: Mohammad S Anwar + +You are given a list of integers, @list. + +Write a script to find out subsequence that respect Pattern 132. Return empty array if none found. + +|| Pattern 132 in a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j]. + +=cut + +use v5.16; +use common::sense; + +use Test::More; +use Test::Deep qw(cmp_deeply); + +sub pattern132 { + my @a = @{$_[0]}; + + while (@a >= 3) { + my $a = shift @a; my @b = @a; + while (@b >= 2) { + my $b = shift @b; my @c = @b; + while (@c >= 1) { + my $c = shift @c; + return [$a,$b,$c] if ($a < $c and $c < $b); + } + } + } + + return []; +} + +for ( + [[3, 1, 4, 2], [1, 4, 2]], + [[1, 2, 3, 4], []], + [[1, 3, 2, 4, 6, 5], [1, 3, 2]], + [[1, 3, 4, 2], [1, 3, 2]], +) { + cmp_deeply(pattern132($_->[0]), + $_->[1], + sprintf('test [%s]',join(',',@{$_->[0]}))); +} + +done_testing; diff --git a/challenge-196/perlboy1967/perl/ch-2.pl b/challenge-196/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..1152b277db --- /dev/null +++ b/challenge-196/perlboy1967/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 196 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-195/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Range List +Submitted by: Mohammad S Anwar + +You are given a sorted unique integer array, @array. + +Write a script to find all possible Number Range i.e [x, y] represent range all integers from x and y (both inclusive). + +|| Each subsequence of two or more contiguous integers + +=cut + +use v5.16; +use common::sense; + +use List::MoreUtils qw(slide); + +use Test::More; +use Test::Deep qw(cmp_deeply); + +sub rangeList (@) { + my @r = ([$_[0]]); + + slide { + if ($a == $b - 1) { + push(@{$r[-1]},$b); + } else { + push(@r,[$b]); + } + } @_; + + [map { [$$_[0],$$_[-1]] } grep { scalar @$_ > 1 } @r]; +} + +for ( + [[1,3,4,5,7], [[3,5]]], + [[1,2,3,6,7,9], [[1,3],[6,7]]], + [[0,1,2,4,5,6,8,9], [[0,2],[4,6],[8,9]]], +) { + cmp_deeply(rangeList(@{$_->[0]}), + [@{$_->[1]}], + sprintf('test [%s]',join(',',@{$_->[0]}))); +} + +done_testing; -- cgit From b0069e11a6daea7bcab3f6505855128af03e51d4 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 19 Dec 2022 12:38:24 -0500 Subject: Challenge 196 --- challenge-196/dave-jacoby/blog.txt | 1 + challenge-196/dave-jacoby/perl/ch-1.pl | 35 ++++++++++++++++++++++++++++ challenge-196/dave-jacoby/perl/ch-2.pl | 42 ++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 challenge-196/dave-jacoby/blog.txt create mode 100644 challenge-196/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-196/dave-jacoby/perl/ch-2.pl (limited to 'challenge-196') diff --git a/challenge-196/dave-jacoby/blog.txt b/challenge-196/dave-jacoby/blog.txt new file mode 100644 index 0000000000..234d3bc22f --- /dev/null +++ b/challenge-196/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2022/12/19/insert-clever-title-here-weekly-challenge-196.html diff --git a/challenge-196/dave-jacoby/perl/ch-1.pl b/challenge-196/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..f0e4b87f0d --- /dev/null +++ b/challenge-196/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +my @examples = + ( [ 3, 1, 4, 2 ], [ 1, 2, 3, 4 ], [ 1, 3, 2, 4, 6, 5 ], [ 1, 3, 4, 2 ], ); + +for my $ex (@examples) { + my @e = $ex->@*; + my $e = join ', ', @e; + my @o = pattern_finder(@e); + my $o = join ', ', @o; + say <<"END"; + Input: \@list = ($e) + Output: ($o) +END +} + +sub pattern_finder ( @array ) { + for my $i ( 0 .. -3 + scalar @array ) { + for my $j ( $i + 1 .. -2 + scalar @array ) { + for my $k ( $j + 1 .. -1 + scalar @array ) { + my $ai = $array[$i]; + my $aj = $array[$j]; + my $ak = $array[$k]; + if ( $ai < $ak && $ak < $aj ) { + return ( $ai, $aj, $ak ); + } + } + } + } + return (); +} diff --git a/challenge-196/dave-jacoby/perl/ch-2.pl b/challenge-196/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..22bf5ae5a5 --- /dev/null +++ b/challenge-196/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ uniq }; + +my @examples = + ( [ 1, 3, 4, 5, 7 ], [ 1, 2, 3, 6, 7, 9 ], [ 0, 1, 2, 4, 5, 6, 8, 9 ], ); + +for my $e (@examples) { + my @array = $e->@*; + my @output = range_list(@array); + my $array = join ', ', @array; + my $output = join ', ', + map { qq{[$_]} } map { join ', ', $_->@* } @output; + say <<"END"; + Input: \@array = ($array) + Output: $output +END +} + +sub range_list ( @array ) { + my @output; + my $k = 0; +OUTER: for my $i ( 0 .. -1 + scalar @array ) { + next if $i < $k; + my @block = ( $array[$i], -1 ); + for my $j ( $i + 1 .. -1 + scalar @array ) { + if ( $array[ $j - 1 ] + 1 == $array[$j] ) { + $block[1] = $array[$j]; + } + else { + $k = $j; + next OUTER; + } + push @output, \@block; + } + } + return map { [ split /,/, $_ ] } uniq map { join ',', $_->@* } @output; +} -- cgit From 74bc3a39c66cb12964edec0aba4bbc21c12477f7 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 19 Dec 2022 17:54:10 +0000 Subject: Week 196 challenges --- challenge-196/peter-campbell-smith/blog.txt | 1 + challenge-196/peter-campbell-smith/perl/ch-1.pl | 54 +++++++++++++++++++++++ challenge-196/peter-campbell-smith/perl/ch-2.pl | 57 +++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 challenge-196/peter-campbell-smith/blog.txt create mode 100755 challenge-196/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-196/peter-campbell-smith/perl/ch-2.pl (limited to 'challenge-196') diff --git a/challenge-196/peter-campbell-smith/blog.txt b/challenge-196/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..ee550e78a6 --- /dev/null +++ b/challenge-196/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +https://pjcs-pwc.blogspot.com/2022/12/pattern-132-and-sequential-runs.html diff --git a/challenge-196/peter-campbell-smith/perl/ch-1.pl b/challenge-196/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..55ac2f1585 --- /dev/null +++ b/challenge-196/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2022-12-19 +# PWC 196 task 1 + +use v5.28; +use utf8; +use warnings; + +# You are given a list of integers, @list. Write a script to find out subsequence that respects Pattern 132. +# Pattern 132 is a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j]. +# Return an empty array if no such sequence is found, or the first if several are found. + + +# Blog: https://pjcs-pwc.blogspot.com/2022/12/pattern-132-and-sequential-runs.html + +my (@tests, $test, @list, $j, $last, @hard, $i, $k); + +# Mohammad's examples +@tests = ([3, 1, 4, 2], [1, 2, 3, 4], [1, 3, 2, 4, 6, 5], [1, 3, 4, 2]); + +# a difficult one +for $j (1 .. 10000) { + push @hard, $j; +} +push @hard, 9999; +push @tests, \@hard; + +# loop over tests +TEST: for $test (@tests) { + @list = @$test; + $last = scalar @list - 1; + + # loop over j, which is the largest of the three + J: for $j (1 .. $last - 1) { + + # find a smaller $i to the left of $j + for $i (0 .. $j - 1) { + if ($list[$i] < $list[$j]) { + + # one exists so let's see if there's a smaller $k to the right of $j + for $k ($j + 1 .. $last) { + if ($list[$k] < $list[$j]) { + say qq[\nInput: \@list = (] . join(', ', @list) . qq[)\nOutput: ($list[$i], $list[$j], $list[$k])]; + next TEST; + } + } + next J; + } + } + } + say qq[\nInput: \@list = (] . join(', ', @list) . qq[)\nOutput: none found]; + +} diff --git a/challenge-196/peter-campbell-smith/perl/ch-2.pl b/challenge-196/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..d9992918f7 --- /dev/null +++ b/challenge-196/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2022-12-19 +# PWC 196 task 2 + +use v5.28; +use utf8; +use warnings; + +# We are given a sorted unique integer array, @array. We are asked to find all the slices of this array +# which comprise consecutive integers, and output the first and last element in each such slice. + +# Blog: https://pjcs-pwc.blogspot.com/2022/12/pattern-132-and-sequential-runs.html + +my (@tests, $test, @array, $start, $in_slice, $output, $j); + +# Mohammad's examples +@tests = ([1,3,4,5,7], [1,2,3,6,7,9], [0,1,2,4,5,6,8,9], [1,3,5,7,9,11]); + +# loop over tests +TEST: for $test (@tests) { + @array = @$test; + + # initialise + $in_slice = 0; + $output = ''; + say qq[\nInput: \@array = (] . join(', ', @array) . ')'; + + # add a number at the end of @array which is 2 more than the last one + push @array, $array[scalar @array - 1] + 2; + + # loop over elements in @array + for ($j = 0; $j < (scalar @array) - 1; $j ++) { + + # if we're not already in a sequence and the next element is one more than this, start a sequence + unless ($in_slice) { + if ($array[$j] == $array[$j + 1] - 1) { + $start = $array[$j]; + $in_slice = 1; + } + + # if we are already in a sequence, end it if the following element isn't one more than this + } else { + if ($array[$j] != $array[$j + 1] - 1) { + $output .= qq[[$start, $array[$j]], ]; + $in_slice = 0; + } + } + } + if ($output) { + say qq[Output: ]. substr($output, 0, -2); + } else { + say qq[Output: no consecutive sequence found]; + } +} + + -- cgit From 10e23b2e5cd4b1d4e9fe98685d05ce495bd2528d Mon Sep 17 00:00:00 2001 From: boblied Date: Mon, 19 Dec 2022 12:57:34 -0600 Subject: w196 update README --- challenge-196/bob-lied/README | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'challenge-196') diff --git a/challenge-196/bob-lied/README b/challenge-196/bob-lied/README index 08e4900b33..1eebe9fbd4 100644 --- a/challenge-196/bob-lied/README +++ b/challenge-196/bob-lied/README @@ -1,3 +1,3 @@ -Solutions to weekly challenge 194 by Bob Lied +Solutions to weekly challenge 196 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-194/ +https://perlweeklychallenge.org/blog/perl-weekly-challenge-196/ -- cgit From 452b8f5905ae8b00cb2d4ab90f7c2b28c3a513c1 Mon Sep 17 00:00:00 2001 From: boblied Date: Mon, 19 Dec 2022 14:56:11 -0600 Subject: Bob Lied W 196 Task 1 --- challenge-196/bob-lied/perl/ch-1.pl | 80 +++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100755 challenge-196/bob-lied/perl/ch-1.pl (limited to 'challenge-196') diff --git a/challenge-196/bob-lied/perl/ch-1.pl b/challenge-196/bob-lied/perl/ch-1.pl new file mode 100755 index 0000000000..3bdef2e8fd --- /dev/null +++ b/challenge-196/bob-lied/perl/ch-1.pl @@ -0,0 +1,80 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Perl Weekly Challenge Week 196, Challenge 1 +#============================================================================= +# Copyright (c) 2022, Bob Lied +#============================================================================= +# You are given a list of integers, @list. +# Write a script to find out subsequence that respect Pattern 132. +# Return empty array if none found. +# Pattern 132 in a sequence (a[i], a[j], a[k]) +# such that i < j < k and a[i] < a[k] < a[j]. +# Example 1 Input: @list = (3, 1, 4, 2) +# Output: (1, 4, 2) respect the Pattern 132. +# Example 2 Input: @list = (1, 2, 3, 4) +# Output: () since no susbsequence can be found. +# Example 3 Input: @list = (1, 3, 2, 4, 6, 5) +# Output: (1, 3, 2) more than one subsequence found, return first. +# Example 4 Input: @list = (1, 3, 4, 2) +# Output: (1, 3, 2) +# +#============================================================================= + +use v5.36; + +use List::Util qw/first/; +use List::MoreUtils qw/after_incl/; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +my @list = @ARGV; +my $answer = find132(\@list); +say "(", join(", ", $answer->@*), ")"; +exit 0; + +sub is132($list, $i1, $i2, $i3) +{ + return $list->[$i1] < $list->[$i2] + && $list->[$i1] < $list->[$i3] + && $list->[$i2] < $list->[$i3]; +} + +sub find132($list) +{ + return [] if scalar(@$list) < 3; + + while ( my $first = shift @$list ) + { + my @tail = after_incl { $_ > $first } $list->@*; + while ( my $middle = shift @tail ) + { + my $last = first { $_ > $first && $_ < $middle } @tail; + if ( $last ) + { + return [ $first, $middle, $last ]; + } + } + } + return []; +} + +sub runTest +{ + use Test2::V0; + + is( find132( [ 3,2,1,0 ] ), [ ], "Descending"); + is( find132( [ 1,3,2 ] ), [1,3,2], "Obvious"); + is( find132( [ 3,1,4,2 ] ), [1,4,2], "Example 1"); + is( find132( [ 1,2,3,4 ] ), [ ], "Example 2"); + is( find132( [ 1,3,2,4,6,5 ] ), [1,3,2], "Example 3"); + is( find132( [ 1,3,4,2 ] ), [1,3,2], "Example 4"); + + done_testing; +} + -- cgit From 83c265f226ede0dd8b721ffc3e150aa646af7495 Mon Sep 17 00:00:00 2001 From: boblied Date: Mon, 19 Dec 2022 17:33:17 -0600 Subject: Bob Lied W 196 T 2 --- challenge-196/bob-lied/perl/ch-2.pl | 74 +++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 challenge-196/bob-lied/perl/ch-2.pl (limited to 'challenge-196') diff --git a/challenge-196/bob-lied/perl/ch-2.pl b/challenge-196/bob-lied/perl/ch-2.pl new file mode 100755 index 0000000000..08eccfd031 --- /dev/null +++ b/challenge-196/bob-lied/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#=============================================================================