diff options
| author | Roger Bell_West <roger@firedrake.org> | 2025-10-26 22:24:17 +0000 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2025-10-26 22:24:17 +0000 |
| commit | a3303c147580d36d98b4aa6a28b3b528acb39020 (patch) | |
| tree | ec6f1124fee6368eefbf39554b4342ef60d836dd | |
| parent | bf02a9f6acabad45b3ded037d9fd66fe40613e41 (diff) | |
| download | perlweeklychallenge-club-a3303c147580d36d98b4aa6a28b3b528acb39020.tar.gz perlweeklychallenge-club-a3303c147580d36d98b4aa6a28b3b528acb39020.tar.bz2 perlweeklychallenge-club-a3303c147580d36d98b4aa6a28b3b528acb39020.zip | |
RogerBW solutions for challenge no. 344
| -rwxr-xr-x | challenge-344/roger-bell-west/crystal/ch-1.cr | 47 | ||||
| -rwxr-xr-x | challenge-344/roger-bell-west/crystal/ch-2.cr | 51 | ||||
| -rwxr-xr-x | challenge-344/roger-bell-west/perl/ch-1.pl | 40 | ||||
| -rwxr-xr-x | challenge-344/roger-bell-west/perl/ch-2.pl | 47 | ||||
| -rw-r--r-- | challenge-344/roger-bell-west/postscript/ch-1.ps | 148 | ||||
| -rw-r--r-- | challenge-344/roger-bell-west/postscript/ch-2.ps | 153 | ||||
| -rwxr-xr-x | challenge-344/roger-bell-west/ruby/ch-1.rb | 54 | ||||
| -rwxr-xr-x | challenge-344/roger-bell-west/ruby/ch-2.rb | 60 | ||||
| -rwxr-xr-x | challenge-344/roger-bell-west/rust/ch-1.rs | 58 | ||||
| -rwxr-xr-x | challenge-344/roger-bell-west/rust/ch-2.rs | 74 | ||||
| -rw-r--r-- | challenge-344/roger-bell-west/tests.json | 88 | ||||
| -rw-r--r-- | challenge-344/roger-bell-west/typst/ch-1.typ | 50 | ||||
| -rw-r--r-- | challenge-344/roger-bell-west/typst/ch-2.typ | 59 |
13 files changed, 929 insertions, 0 deletions
diff --git a/challenge-344/roger-bell-west/crystal/ch-1.cr b/challenge-344/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..d9a2169c1e --- /dev/null +++ b/challenge-344/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,47 @@ +#! /usr/bin/crystal + +def u2a(a) + p = a + if p > 0 + out = Array(Int32).new + while p > 0 + p, x = p.divmod(10) + out.unshift(x) + end + out + else + [0] + end +end + +def a2u(a) + acc = 0 + a.each do |d| + acc *= 10 + acc += d + end + acc +end + +def arrayformcompute(a, b) + u2a(a2u(a) + b) +end + +require "spec" +describe "arrayformcompute" do + it "test_ex1" do + arrayformcompute([1, 2, 3, 4], 12).should eq [1, 2, 4, 6] + end + it "test_ex2" do + arrayformcompute([2, 7, 4], 181).should eq [4, 5, 5] + end + it "test_ex3" do + arrayformcompute([9, 9, 9], 1).should eq [1, 0, 0, 0] + end + it "test_ex4" do + arrayformcompute([1, 0, 0, 0, 0], 9999).should eq [1, 9, 9, 9, 9] + end + it "test_ex5" do + arrayformcompute([0], 1000).should eq [1, 0, 0, 0] + end +end diff --git a/challenge-344/roger-bell-west/crystal/ch-2.cr b/challenge-344/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..d72185f893 --- /dev/null +++ b/challenge-344/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,51 @@ +#! /usr/bin/crystal + +def arrayformation(src, tgt) + stack = Array(Tuple(Array(Int32), Set(Int32))).new + stack.push({Array(Int32).new, 0.upto(src.size - 1).to_set}) + while stack.size > 0 + c = stack.pop + if c[0].size == tgt.size() + return true + else + c[1].each do |candidate| + offset = c[0].size + nextcandidate = c[1].clone + nextcandidate.delete(candidate) + valid = true + seq = c[0].clone + src[candidate].each_with_index do |x, i| + if x == tgt[i + offset] + seq.push(x) + else + valid = false + break + end + end + if valid + stack.push({seq, nextcandidate}) + end + end + end + end + false +end + +require "spec" +describe "arrayformation" do + it "test_ex1" do + arrayformation([[2, 3], [1], [4]], [1, 2, 3, 4]).should eq true + end + it "test_ex2" do + arrayformation([[1, 3], [2, 4]], [1, 2, 3, 4]).should eq false + end + it "test_ex3" do + arrayformation([[9, 1], [5, 8], [2]], [5, 8, 2, 9, 1]).should eq true + end + it "test_ex4" do + arrayformation([[1], [3]], [1, 2, 3]).should eq false + end + it "test_ex5" do + arrayformation([[7, 4, 6]], [7, 4, 6]).should eq true + end +end diff --git a/challenge-344/roger-bell-west/perl/ch-1.pl b/challenge-344/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..2940175ff0 --- /dev/null +++ b/challenge-344/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,40 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is_deeply(arrayformcompute([1, 2, 3, 4], 12), [1, 2, 4, 6], 'example 1'); +is_deeply(arrayformcompute([2, 7, 4], 181), [4, 5, 5], 'example 2'); +is_deeply(arrayformcompute([9, 9, 9], 1), [1, 0, 0, 0], 'example 3'); +is_deeply(arrayformcompute([1, 0, 0, 0, 0], 9999), [1, 9, 9, 9, 9], 'example 4'); +is_deeply(arrayformcompute([0], 1000), [1, 0, 0, 0], 'example 5'); + +sub u2a($a) { + my $p = $a; + if ($p > 0) { + my @out; + while ($p > 0) { + unshift @out, $p % 10; + $p = int($p / 10); + } + \@out; + } else { + [0]; + } +} + +sub a2u($a) { + my $acc = 0; + foreach my $d (@{$a}) { + $acc *= 10; + $acc += $d; + } + $acc +} + +sub arrayformcompute($a, $b) { + u2a(a2u($a) + $b); +} diff --git a/challenge-344/roger-bell-west/perl/ch-2.pl b/challenge-344/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..da3eafaa7a --- /dev/null +++ b/challenge-344/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,47 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(arrayformation([[2, 3], [1], [4]], [1, 2, 3, 4]), 1, 'example 1'); +is(arrayformation([[1, 3], [2, 4]], [1, 2, 3, 4]), 0, 'example 2'); +is(arrayformation([[9, 1], [5, 8], [2]], [5, 8, 2, 9, 1]), 1, 'example 3'); +is(arrayformation([[1], [3]], [1, 2, 3]), 0, 'example 4'); +is(arrayformation([[7, 4, 6]], [7, 4, 6]), 1, 'example 5'); + +use Storable qw(dclone); + +sub arrayformation($src, $tgt) { + my @stack; + push @stack, [[], {map {$_ => 1} 0 .. $#{$src}}]; + while (scalar @stack > 0) { + my $c = pop @stack; + if ($#{$c->[0]} == $#{$tgt}) { + return 1; + } else { + foreach my $candidate (keys %{$c->[1]}) { + my $offset = scalar @{$c->[0]}; + my $nextcandidate = dclone($c->[1]); + delete $nextcandidate->{$candidate}; + my $valid = 1; + my $seq = dclone($c->[0]); + values @{$src->[$candidate]}; + while (my ($i, $x) = each @{$src->[$candidate]}) { + if ($x == $tgt->[$i + $offset]) { + push @{$seq}, $x; + } else { + $valid = 0; + last; + } + } + if ($valid) { + push @stack, [$seq, $nextcandidate]; + } + } + } + } + 0; +} diff --git a/challenge-344/roger-bell-west/postscript/ch-1.ps b/challenge-344/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..72ba0f8e95 --- /dev/null +++ b/challenge-344/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,148 @@ +%!PS + +% begin included library code +% see https://codeberg.org/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 + +/reverse { + 1 dict begin + dup length /l exch def + [ exch + aload pop + 2 1 l { + -1 roll + } for + ] + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + +/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 + + +% end included library code + +/u2a { + dup 0 eq { + pop + [ 0 ] + } { + [ exch + { + dup 0 eq { + pop + exit + } if + dup 10 mod exch + 10 idiv + } loop + ] + reverse + } ifelse +} bind def + +/a2u { + 0 exch + { + exch 10 mul add + } forall +} bind def + +/arrayformcompute { + exch + a2u + add + u2a +} bind def + +(arrayformcompute) test.start +[1 2 3 4] 12 arrayformcompute [1 2 4 6] deepeq test +[2 7 4] 181 arrayformcompute [4 5 5] deepeq test +[9 9 9] 1 arrayformcompute [1 0 0 0] deepeq test +[1 0 0 0 0] 9999 arrayformcompute [1 9 9 9 9] deepeq test +[0] 1000 arrayformcompute [1 0 0 0] deepeq test +test.end diff --git a/challenge-344/roger-bell-west/postscript/ch-2.ps b/challenge-344/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..8c4697d680 --- /dev/null +++ b/challenge-344/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,153 @@ +%!PS + +% begin included library code +% see https://codeberg.org/Firedrake/postscript-libraries/ +/deepcopy { + 2 dict begin + /a exch def + a type /dicttype eq { + << + a keys { + /k exch def + k + a k get deepcopy + } forall + >> + } { + a type /arraytype eq { + [ + a { + deepcopy + } forall + ] + } { + a type /stringtype eq { + a dup length string cvs + } { + a + } ifelse + } ifelse + } ifelse + end +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} 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 + +/apush.right { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} bind def + +/enumerate.array { + 1 dict begin + /a exch def + [ + 0 1 a length 1 sub { + [ exch dup a exch get ] + } for + ] + end +} bind def + +/apop.right { % [a b c] -> [a b] c + [ exch aload length 1 add 1 roll ] exch +} bind def + +/keys { % dict -> array of dict keys + [ exch + { + pop + } forall + ] +} bind def + +/toset { % array -> dict of (value, true) + << exch + { + true + } forall + >> +} 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 + + +% end included library code + +/arrayformation { + 0 dict begin + /tgt exch def + /src exch def + /stack [ [ 0 array [ 0 1 src length 1 sub { } for ] toset ] ] def + /sol false def + { + stack length 0 eq { + exit + } if + stack apop.right /c exch def /stack exch def + c 0 get length tgt length eq { + /sol true def + /c 0 array def + } { + c 1 get keys { + /candidate exch def + /offset c 0 get length def + /nextcandidate c 1 get deepcopy def + nextcandidate candidate undef + /valid true def + /seq c 0 get deepcopy def + src candidate get enumerate.array { + aload pop + /x exch def + /i exch def + x tgt i offset add get eq { + /seq seq x apush.right def + } { + /valid false def + exit + } ifelse + } forall + valid { + /stack stack [ seq nextcandidate ] apush.right def + } if + } forall + } ifelse + } loop + sol + end +} bind def + +(arrayformation) test.start +[[2 3] [1] [4]] [1 2 3 4] arrayformation test +[[1 3] [2 4]] [1 2 3 4] arrayformation not test +[[9 1] [5 8] [2]] [5 8 2 9 1] arrayformation test +[[1] [3]] [1 2 3] arrayformation not test +[[7 4 6]] [7 4 6] arrayformation test +test.end diff --git a/challenge-344/roger-bell-west/ruby/ch-1.rb b/challenge-344/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..c4e02b9fc3 --- /dev/null +++ b/challenge-344/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,54 @@ +#! /usr/bin/ruby + +def u2a(a) + p = a + if p > 0 + out = [] + while p > 0 + (p, x) = p.divmod(10) + out.unshift(x) + end + out + else + [0] + end +end + +def a2u(a) + acc = 0 + a.each do |d| + acc *= 10 + acc += d + end + acc +end + +def arrayformcompute(a, b) + u2a(a2u(a) + b) +end + +require 'test/unit' + +class TestArrayformcompute < Test::Unit::TestCase + + def test_ex1 + assert_equal([1, 2, 4, 6], arrayformcompute([1, 2, 3, 4], 12)) + end + + def test_ex2 + assert_equal([4, 5, 5], arrayformcompute([2, 7, 4], 181)) + end + + def test_ex3 + assert_equal([1, 0, 0, 0], arrayformcompute([9, 9, 9], 1)) + end + + def test_ex4 + assert_equal([1, 9, 9, 9, 9], arrayformcompute([1, 0, 0, 0, 0], 9999)) + end + + def test_ex5 + assert_equal([1, 0, 0, 0], arrayformcompute([0], 1000)) + end + +end diff --git a/challenge-344/roger-bell-west/ruby/ch-2.rb b/challenge-344/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..20c08bd25c --- /dev/null +++ b/challenge-344/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,60 @@ +#! /usr/bin/ruby + +require 'set' + +def arrayformation(src, tgt) + stack = [] + stack.push([[], 0.upto(src.length - 1).to_set]) + while stack.length > 0 + c = stack.pop + if c[0].length == tgt.length() + return true + else + c[1].each do |candidate| + offset = c[0].length + nextcandidate = c[1].clone + nextcandidate.delete(candidate) + valid = true + seq = c[0].clone + src[candidate].each_with_index do |x, i| + if x == tgt[i + offset] + seq.push(x) + else + valid = false + break + end + end + if valid + stack.push([seq, nextcandidate]) + end + end + end + end + false +end + +require 'test/unit' + +class TestArrayformation < Test::Unit::TestCase + + def test_ex1 + assert_equal(true, arrayformation([[2, 3], [1], [4]], [1, 2, 3, 4])) + end + + def test_ex2 + assert_equal(false, arrayformation([[1, 3], [2, 4]], [1, 2, 3, 4])) + end + + def test_ex3 + assert_equal(true, arrayformation([[9, 1], [5, 8], [2]], [5, 8, 2, 9, 1])) + end + + def test_ex4 + assert_equal(false, arrayformation([[1], [3]], [1, 2, 3])) + end + + def test_ex5 + assert_equal(true, arrayformation([[7, 4, 6]], [7, 4, 6])) + end + +end diff --git a/challenge-344/roger-bell-west/rust/ch-1.rs b/challenge-344/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..e42f5a8755 --- /dev/null +++ b/challenge-344/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,58 @@ +#! /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!(arrayformcompute(vec![1, 2, 3, 4], 12), vec![1, 2, 4, 6]); +} + +#[test] +fn test_ex2() { + assert_eq!(arrayformcompute(vec![2, 7, 4], 181), vec![4, 5, 5]); +} + +#[test] +fn test_ex3() { + assert_eq!(arrayformcompute(vec![9, 9, 9], 1), vec![1, 0, 0, 0]); +} + +#[test] +fn test_ex4() { + assert_eq!( + arrayformcompute(vec![1, 0, 0, 0, 0], 9999), + vec![1, 9, 9, 9, 9] + ); +} + +#[test] +fn test_ex5() { + assert_eq!(arrayformcompute(vec![0], 1000), vec![1, 0, 0, 0]); +} + +fn u2a(a: u32) -> Vec<u32> { + let mut p = a; + if p > 0 { + let mut out = Vec::new(); + while p > 0 { + out.push(p % 10); + p /= 10; + } + out.reverse(); + out + } else { + vec![0] + } +} + +fn a2u(a: Vec<u32>) -> u32 { + let mut acc = 0; + for d in a { + acc *= 10; + acc += d; + } + acc +} + +fn arrayformcompute(a: Vec<u32>, b: u32) -> Vec<u32> { + u2a(a2u(a) + b) +} diff --git a/challenge-344/roger-bell-west/rust/ch-2.rs b/challenge-344/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..372c529573 --- /dev/null +++ b/challenge-344/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,74 @@ +#! /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!( + arrayformation(vec![vec![2, 3], vec![1], vec![4]], vec![1, 2, 3, 4]), + true + ); +} + +#[test] +fn test_ex2() { + assert_eq!( + arrayformation(vec![vec![1, 3], vec![2, 4]], vec![1, 2, 3, 4]), + false + ); +} + +#[test] +fn test_ex3() { + assert_eq!( + arrayformation( + vec![vec![9, 1], vec![5, 8], vec![2]], + vec![5, 8, 2, 9, 1] + ), + true + ); +} + +#[test] +fn test_ex4() { + assert_eq!(arrayformation(vec![vec![1], vec![3]], vec![1, 2, 3]), false); +} + +#[test] +fn test_ex5() { + assert_eq!(arrayformation(vec![vec![7, 4, 6]], vec![7, 4, 6]), true); +} + +use std::collections::HashSet; + +fn arrayformation(src: Vec<Vec<i32>>, tgt: Vec<i32>) -> bool { + let mut stack: Vec<(Vec<i32>, HashSet<usize>)> = Vec::new(); + stack.push(( + Vec::new(), + (0..src.len()).into_iter().collect::<HashSet<usize>>(), + )); + while let Some(c) = stack.pop() { + if c.0.len() == tgt.len() { + return true; + } else { + for candidate in &c.1 { + let offset = c.0.len(); + let mut nextcandidate = c.1.clone(); + nextcandidate.remove(&candidate); + let mut valid = true; + let mut seq = c.0.clone(); + for (i, x) in src[*candidate].iter().enumerate() { + if *x == tgt[i + offset] { + seq.push(*x); + } else { + valid = false; + break; + } + } + if valid { + stack.push((seq, nextcandidate)); + } + } + } + } + false +} diff --git a/challenge-344/roger-bell-west/tests.json b/challenge-344/roger-bell-west/tests.json new file mode 100644 index 0000000000..5f81c249ac --- /dev/null +++ b/challenge-344/roger-bell-west/tests.json @@ -0,0 +1,88 @@ +{ + "ch-1" : [ + { + "function" : "arrayformcompute", + "multiarg" : true, + "arguments" : [ + [ 1, 2, 3, 4 ], + 12 + ], + "result" : [ 1, 2, 4, 6 ] + }, + { + "multiarg" : true, + "arguments" : [ + [ 2, 7, 4 ], + 181 + ], + "result" : [ 4, 5, 5 ] + }, + { + "multiarg" : true, + "arguments" : [ + [ 9, 9, 9 ], + 1 + ], + "result" : [ 1, 0, 0, 0 ] + }, + { + "multiarg" : true, + "arguments" : [ + [ 1, 0, 0, 0, 0 ], + 9999 + ], + "result" : [ 1, 9, 9, 9, 9 ] + }, + { + "multiarg" : true, + "arguments" : [ + [ 0 ], + 1000 + ], + "result" : [ 1, 0, 0, 0 ] + } + ], + "ch-2" : [ + { + "function" : "arrayformation", + "multiarg" : true, + "arguments" : [ + [ [2,3], [1], [4] ], + [ 1, 2, 3, 4 ] + ], + "result" : true + }, + { + "multiarg" : true, + "arguments" : [ + [ [1,3], [2,4] ], + [ 1, 2, 3, 4 ] + ], + "result" : false + }, + { + "multiarg" : true, + "arguments" : [ + [ [9,1], [5,8], [2] ], + [ 5, 8, 2, 9, 1 ] + ], + "result" : true + }, + { + "multiarg" : true, + "arguments" : [ + [ [1], [3] ], + [ 1, 2, 3 ] + ], + "result" : false + }, + { + "multiarg" : true, + "arguments" : [ + [ [7, 4, 6] ], + [ 7, 4, 6 ] + ], + "result" : true + } + ] +} diff --git a/challenge-344/roger-bell-west/typst/ch-1.typ b/challenge-344/roger-bell-west/typst/ch-1.typ new file mode 100644 index 0000000000..a09041fc33 --- /dev/null +++ b/challenge-344/roger-bell-west/typst/ch-1.typ @@ -0,0 +1,50 @@ +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +#let u2a(a) = { + let p = a + if p > 0 { + let out = () + while p > 0 { + out.push(calc.rem-euclid(p, 10)) + p = calc.div-euclid(p, 10) + } + out.rev() + } else { + (0) + } +} + +#let a2u(a) = { + let acc = 0 + for d in a { + acc *= 10 + acc += d + } + acc +} + +#let arrayformcompute(a, b) = { + u2a(a2u(a) + b) +} + +Test 1: + #testresult(arrayformcompute((1, 2, 3, 4), 12) == (1, 2, 4, 6)) + +Test 2: + #testresult(arrayformcompute((2, 7, 4), 181) == (4, 5, 5)) + +Test 3: + #testresult(arrayformcompute((9, 9, 9), 1) == (1, 0, 0, 0)) + +Test 4: + #testresult(arrayformcompute((1, 0, 0, 0, 0), 9999) == (1, 9, 9, 9, 9)) + +Test 5: + #testresult(arrayformcompute((0, ), 1000) == (1, 0, 0, 0)) + diff --git a/challenge-344/roger-bell-west/typst/ch-2.typ b/challenge-344/roger-bell-west/typst/ch-2.typ new file mode 100644 index 0000000000..7f17d7a0f3 --- /dev/null +++ b/challenge-344/roger-bell-west/typst/ch-2.typ @@ -0,0 +1,59 @@ +#let testresult(pass) = { + if pass { + text(fill: green, "Pass") + } else { + text(fill: red, "Fail") + } +} + +#let arrayformation(src, tgt) = { + let stack = () + let d = (:) + for n in range(src.len()) { + d.insert(str(n), true) + } + stack.push(((), d)) + let ret = false + while stack.len() > 0 { + let c = stack.pop() + if c.at(0).len() == tgt.len() { + ret = true + stack = () + } else { + for candidate in c.at(1).keys() { + let offset = c.at(0).len() + let nextcandidate = c.at(1).keys().filter(x => x != candidate).map(x => (x, true)).to-dict() + let valid = true + let seq = c.at(0).map(x => x) + for (i, x) in src.at(int(candidate)).enumerate() { + if x == tgt.at(i + offset) { + seq.push(x) + } else { + valid = false + break + } + } + if valid { + stack.push((seq, nextcandidate)) + } + } + } + } + ret +} + +Test 1: + #testresult(arrayformation(((2, 3), (1, ), (4, )), (1, 2, 3, 4))) + +Test 2: + #testresult(not arrayformation(((1, 3), (2, 4)), (1, 2, 3, 4))) + +Test 3: + #testresult(arrayformation(((9, 1), (5, 8), (2, )), (5, 8, 2, 9, 1))) + +Test 4: + #testresult(not arrayformation(((1, ), (3, )), (1, 2, 3))) + +Test 5: + #testresult(arrayformation(((7, 4, 6), ), (7, 4, 6))) + |
