diff options
| author | Roger Bell_West <roger@firedrake.org> | 2022-11-14 09:33:51 +0000 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2022-11-14 09:33:51 +0000 |
| commit | c5d7bbdf41bd9632858245f3afd93319186a14d9 (patch) | |
| tree | 2b698b3b8804f1ea209bb5f9d5b29ecfd4d50e96 | |
| parent | 377a415bb53d1ed0b16b70d190bb444241216036 (diff) | |
| download | perlweeklychallenge-club-c5d7bbdf41bd9632858245f3afd93319186a14d9.tar.gz perlweeklychallenge-club-c5d7bbdf41bd9632858245f3afd93319186a14d9.tar.bz2 perlweeklychallenge-club-c5d7bbdf41bd9632858245f3afd93319186a14d9.zip | |
Solutions for challenge #191
18 files changed, 901 insertions, 0 deletions
diff --git a/challenge-191/roger-bell-west/javascript/ch-1.js b/challenge-191/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..7daf99853d --- /dev/null +++ b/challenge-191/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,36 @@ +#! /usr/bin/node + +"use strict" + +function twicelargest(l0) { + let l = [...l0]; + l.sort(function(a,b) { + return b-a; + }); + return l[0] >= l[1] * 2; +} + +if (!twicelargest([1, 2, 3, 4])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (twicelargest([1, 2, 0, 5])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (twicelargest([2, 6, 3, 1])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (!twicelargest([4, 5, 2, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-191/roger-bell-west/javascript/ch-2.js b/challenge-191/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..424b4641d5 --- /dev/null +++ b/challenge-191/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,62 @@ +#! /usr/bin/node + +"use strict" + +function cutelist(n) { + let tab = [[false]]; + let t = []; + for (let x = 1; x <= n; x++) { + tab.push(new Array(n+1).fill(false)); + t.push(x); + } + for (let x = 1; x <= n; x++) { + for (let y = 1; y <= x; y++) { + if (x % y != 0 && y % x != 0) { + tab[x][y] = true; + tab[y][x] = true; + } + } + } + let count = 0; + let stackl = [[]]; + let stackc = [t]; + while (stackl.length != 0) { + let l = stackl.pop(); + let c = stackc.pop(); + if (c.length == 0 && l.length == n) { + count++; + } else { + let place = l.length + 1; + for (let candidate of c) { + if (!tab[place][candidate]) { + let q = Array.from(l); + q.push(candidate); + stackl.push(q); + stackc.push(Array.from(c.filter(i => i != candidate))); + } + } + } + } + return count; +} + +if (cutelist(2) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (cutelist(10) == 700) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (cutelist(15) == 24679) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-191/roger-bell-west/kotlin/ch-1.kt b/challenge-191/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..a5b535b011 --- /dev/null +++ b/challenge-191/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,32 @@ +fun twicelargest(l0: List<Int>): Boolean { + var l = ArrayList(l0) + l.sort() + return l.last() >= 2*l[l.size - 2] +} + +fun main() { + if (!twicelargest(listOf(1, 2, 3, 4))) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (twicelargest(listOf(1, 2, 0, 5))) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (twicelargest(listOf(2, 6, 3, 1))) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (!twicelargest(listOf(4, 5, 2, 3))) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-191/roger-bell-west/kotlin/ch-2.kt b/challenge-191/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..9c445a2027 --- /dev/null +++ b/challenge-191/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,65 @@ +fun cutelist(n: Int): Int { + var tab = ArrayList<ArrayList<Boolean>>() + tab.add(arrayListOf(false)) + for (x in 1..n) { + var row = ArrayList<Boolean>() + for (y in 1..n+1) { + row.add(false) + } + tab.add(row) + } + for (x in 1..n) { + for (y in 1..x) { + if (x % y != 0 && y % x != 0) { + tab[x][y] = true + tab[y][x] = true + } + } + } + var count = 0 + var stackl = ArrayList<ArrayList<Int>>() + stackl.add(arrayListOf()) + var stackc = ArrayList<ArrayList<Int>>() + stackc.add(ArrayList((1..n).toList())) + while (stackl.size > 0) { + val l = stackl.last() + stackl = ArrayList(stackl.dropLast(1)) + val c = stackc.last() + stackc = ArrayList(stackc.dropLast(1)) + if (c.size == 0 && l.size == n) { + count += 1 + } else { + val place = l.size + 1 + for (candidate in c) { + if (!tab[place][candidate]) { + var q = ArrayList(l) + q.add(candidate) + stackl.add(q) + stackc.add(ArrayList(c.filter{it != candidate})) + } + } + } + } + return count +} + +fun main() { + if (cutelist(2) == 2) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (cutelist(10) == 700) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + if (cutelist(15) == 24679) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-191/roger-bell-west/lua/ch-1.lua b/challenge-191/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..67a03fccb7 --- /dev/null +++ b/challenge-191/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,35 @@ +#! /usr/bin/lua + +function twicelargest(l0) + local l = l0 + table.sort(l) + return l[#l] >= 2 * l[#l-1] +end + +if not twicelargest({1, 2, 3, 4}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if twicelargest({1, 2, 0, 5}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if twicelargest({2, 6, 3, 1}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not twicelargest({4, 5, 2, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-191/roger-bell-west/lua/ch-2.lua b/challenge-191/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..f05338a714 --- /dev/null +++ b/challenge-191/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,73 @@ +#! /usr/bin/lua + +function cutelist(n) + local tab = {} + local tmp = {} + for x = 1,n do + local row = {} + for y = 1,n do + table.insert(row, false) + end + table.insert(tab, row) + table.insert(tmp, x) + end + for x = 1,n do + for y = 1,x do + if x % y ~= 0 and y % x ~= 0 then + tab[x][y] = true + tab[y][x] = true + end + end + end + local count = 0 + local stackl = {{}} + local stackc = {tmp} + while #stackl > 0 do + local l = table.remove(stackl) + local c = table.remove(stackc) + if #c == 0 and #l == n then + count = count + 1 + else + local place = #l + 1 + for i, candidate in ipairs(c) do + if not tab[place][candidate] then + local ql = {} + for j, qx in ipairs(l) do + table.insert(ql, qx) + end + table.insert(ql, candidate) + table.insert(stackl, ql) + local qc = {} + for j, qx in ipairs(c) do + if qx ~= candidate then + table.insert(qc, qx) + end + end + table.insert(stackc, qc) + end + end + end + end + return count +end + +if cutelist(2) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if cutelist(10) == 700 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if cutelist(15) == 24679 then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-191/roger-bell-west/perl/ch-1.pl b/challenge-191/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..5a46a22aac --- /dev/null +++ b/challenge-191/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,17 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 4; + +is(twicelargest(1, 2, 3, 4), 0, 'example 1'); +is(twicelargest(1, 2, 0, 5), 1, 'example 2'); +is(twicelargest(2, 6, 3, 1), 1, 'example 3'); +is(twicelargest(4, 5, 2, 3), 0, 'example 4'); + +sub twicelargest(@l0) { + my @l = sort @l0; + return ($l[-1] >= 2*$l[-2])?1:0; +} diff --git a/challenge-191/roger-bell-west/perl/ch-2.pl b/challenge-191/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..e2cf99800a --- /dev/null +++ b/challenge-191/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,44 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 3; + +is(cutelist(2), 2, 'example 1'); +is(cutelist(10), 700, 'example 2'); +is(cutelist(15), 24679, 'example 3'); + +sub cutelist($n) { + my @tab = ([]); + foreach (1..$n) { + push @tab,[(0) x ($n+1)]; + } + foreach my $x (1..$n) { + foreach my $y (1..$x) { + if ($x % $y != 0 && $y % $x != 0) { + $tab[$x][$y] = $tab[$y][$x] = 1; + } + } + } + my $count = 0; + my @stackl = ([]); + my @stackc = ([1..$n]); + while (@stackl) { + my $l = pop @stackl; + my $c = pop @stackc; + if (scalar @{$c} == 0 && scalar @{$l} == $n) { + $count++; + } else { + my $place = scalar @{$l} + 1; + foreach my $candidate (@{$c}) { + unless ($tab[$place][$candidate]) { + push @stackl,[@{$l},$candidate]; + push @stackc,[grep {$_ != $candidate} @{$c}]; + } + } + } + } + return $count; +} diff --git a/challenge-191/roger-bell-west/postscript/ch-1.ps b/challenge-191/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..fd0cebc81c --- /dev/null +++ b/challenge-191/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,121 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/quicksort.swap { + 2 dict begin + /bi exch def + /ai exch def + arr ai get + arr bi get + arr exch ai exch put + arr exch bi exch put + end +} bind def + +/quicksort.main { % lo hi -> (null) + 3 dict begin + /hi exch def + /lo exch def + /xit false def + lo 0 lt { + /xit true def + } if + hi 0 lt { + /xit true def + } if + lo hi ge { + /xit true def + } if + xit not { + /p quicksort.partition def + lo p quicksort.main + p 1 add hi quicksort.main + } if + end +} 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.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + +/quicksort.partition { + 3 dict begin + /pivot arr hi lo add 2 idiv get def + /i lo 1 sub def + /j hi 1 add def + { + { + /i i 1 add def + arr i get pivot ge { + exit + } if + } loop + { + /j j 1 sub def + arr j get pivot le { + exit + } if + } loop + i j ge { + j + exit + } if + i j quicksort.swap + } loop + end +} 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 + +/quicksort { % [ a c b ] -> [ a b c ] + 1 dict begin + /arr exch def + arr length 0 gt { + 0 arr length 1 sub quicksort.main + } if + arr + end +} bind def + + +% end included library code + +/twicelargest { + 1 dict begin + quicksort /l exch def + l l length 1 sub get l l length 2 sub get 2 mul ge + end +} bind def + +(twicelargest) test.start +[ 1 2 3 4 ] twicelargest not test +[ 1 2 0 5 ] twicelargest test +[ 2 6 3 1 ] twicelargest test +[ 4 5 2 3 ] twicelargest not test +test.end diff --git a/challenge-191/roger-bell-west/postscript/ch-2.ps b/challenge-191/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..3f7bc021c2 --- /dev/null +++ b/challenge-191/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,116 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/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 + +/apop.right { % [a b c] -> [a b] c + [ exch aload length 1 add 1 roll ] exch +} 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 + +/filter { % array proc(bool) -> array + 1 dict begin + /p exch def + [ exch + { + dup p not + { + pop + } if + } forall + ] + end +} bind def + +/apush.right { % [a b] c -> [a b c] + exch + [ exch aload length 2 add -1 roll ] +} bind def + + +% end included library code + +/cutelist { + 11 dict begin + /n exch def + /tab [ + [ ] + n { + [ + false + n { + dup + } repeat + ] + } repeat + ] def + 1 1 n { + /x exch def + 1 1 x { + /y exch def + x y mod 0 ne y x mod 0 ne and { + tab x get y true put + tab y get x true put + } if + } for + } for + /ct 0 def + /stackl [ [ ] ] def + /stackc [ [ 1 1 n {} for ] ] def + { + stackl length 0 eq { + exit + } if + stackl apop.right /l exch def /stackl exch def + stackc apop.right /c exch def /stackc exch def + c length 0 eq l length n eq and { + /ct ct 1 add def + } { + /place l length 1 add def + c { + /candidate exch def + tab place get candidate get not { + /stackl stackl [ l aload pop candidate ] apush.right def + /stackc stackc [ c { candidate ne } filter aload pop ] apush.right def + } if + } forall + } ifelse + } loop + ct + end +} bind def + +(cutelist) test.start +2 cutelist 2 eq test +10 cutelist 700 eq test +15 cutelist 24679 eq test +test.end diff --git a/challenge-191/roger-bell-west/python/ch-1.py b/challenge-191/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..0b0c0c6ba1 --- /dev/null +++ b/challenge-191/roger-bell-west/python/ch-1.py @@ -0,0 +1,25 @@ +#! /usr/bin/python3 + +import unittest +import re + +def twicelargest(l0): + l = l0 + l.sort() + return l[-1] >= (2 * l[-2]) + +class TestTwicelargest(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(twicelargest([1, 2, 3, 4]), False, "example 1") + + def test_ex2(self): + self.assertEqual(twicelargest([1, 2, 0, 5]), True, "example 2") + + def test_ex3(self): + self.assertEqual(twicelargest([2, 6, 3, 1]), True, "example 3") + + def test_ex4(self): + self.assertEqual(twicelargest([4, 5, 2, 3]), False, "example 4") + +unittest.main() diff --git a/challenge-191/roger-bell-west/python/ch-2.py b/challenge-191/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..a06335557f --- /dev/null +++ b/challenge-191/roger-bell-west/python/ch-2.py @@ -0,0 +1,43 @@ +#! /usr/bin/python3 + +import unittest + +def cutelist(n): + tab = [[False]] + for x in range(1, n+1): + tab.append([False] * (n+1)) + for x in range(1, n+1): + for y in range(1, x+1): + if x % y != 0 and y % x != 0: + tab[x][y] = True + tab[y][x] = True + count = 0 + stackl = [[]] + stackc = [[x for x in range(1,n+1)]] + while len(stackl) > 0: + l = stackl.pop() + c = stackc.pop() + if len(c) == 0 and len(l) == n: + count += 1 + else: + place = len(l) + 1 + for candidate in c: + if not tab[place][candidate]: + q = l.copy() + q.append(candidate) + stackl.append(q) + stackc.append([i for i in c if i != candidate]) + return count + +class TestCutelist(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(cutelist(2), 2, 'example 1') + + def test_ex2(self): + self.assertEqual(cutelist(10), 700, 'example 2') + + def test_ex3(self): + self.assertEqual(cutelist(15), 24679, 'example 3') + +unittest.main() diff --git a/challenge-191/roger-bell-west/raku/ch-1.p6 b/challenge-191/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..cd79a8e17d --- /dev/null +++ b/challenge-191/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,15 @@ +#! /usr/bin/perl6 + +use Test; + +plan 4; + +is(twicelargest([1, 2, 3, 4]), False, 'example 1'); +is(twicelargest([1, 2, 0, 5]), True, 'example 2'); +is(twicelargest([2, 6, 3, 1]), True, 'example 3'); +is(twicelargest([4, 5, 2, 3]), False, 'example 4'); + +sub twicelargest(@l0) { + my @l = @l0.sort; + return @l[*-1] >= 2*@l[*-2]; +} diff --git a/challenge-191/roger-bell-west/raku/ch-2.p6 b/challenge-191/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..c8588cf1f8 --- /dev/null +++ b/challenge-191/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,45 @@ +#! /usr/bin/perl6 + +use Test; + +plan 3; + +is(cutelist(2), 2, 'example 1'); +is(cutelist(10), 700, 'example 2'); +is(cutelist(15), 24679, 'example 3'); + +sub cutelist($n) { + my @tab = ([]); + for (1..$n) { + @tab.push([False xx ($n+1)]); + } + for (1..$n) -> $x { + for (1..$x) -> $y { + if ($x % $y != 0 && $y % $x != 0) { + @tab[$x][$y] = @tab[$y][$x] = True; + } + } + } + my $count = 0; + my @stackl = [[],]; + my @stackc = [[1..$n],]; + while (@stackl.elems > 0) { + my @l = @stackl.pop.flat; + my @c = @stackc.pop.flat; + if (@c.elems == 0 && @l.elems == $n) { + $count++; + } else { + my $place = @l.elems + 1; + for @c -> $candidate { + unless (@tab[$place][$candidate]) { + my @q = @l.clone; + @q.push($candidate); + @stackl.push(@q); + my @qc = @c.grep({$_ != $candidate}); + @stackc.push(@qc); + } + } + } + } + return $count; +} diff --git a/challenge-191/roger-bell-west/ruby/ch-1.rb b/challenge-191/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..9aabe23c24 --- /dev/null +++ b/challenge-191/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,27 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def twicelargest(l0) + l = l0.sort + return l[-1] >= (2 * l[-2]) +end + +class TestDivisiblepairs < Test::Unit::TestCase + + def test_ex1 + assert_equal(false, twicelargest([1, 2, 3, 4])) + end + + def test_ex2 + assert_equal(true, twicelargest([1, 2, 0, 5])) + end + + def test_ex3 + assert_equal(true, twicelargest([2, 6, 3, 1])) + end + + def test_ex4 + assert_equal(false, twicelargest([4, 5, 2, 3])) + end +end diff --git a/challenge-191/roger-bell-west/ruby/ch-2.rb b/challenge-191/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..735ac64d30 --- /dev/null +++ b/challenge-191/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,56 @@ +#! /usr/bin/ruby + +require 'test/unit' + +require 'set' + +def cutelist(n) + tab = [[false]] + 1.upto(n) do + tab.push([false] * (n+1)) + end + 1.upto(n) do |x| + 1.upto(x) do |y| + if x % y != 0 && y % x != 0 then + tab[x][y] = true + tab[y][x] = true + end + end + end + count = 0 + stackl = [[]] + stackc = [(1..n).to_a] + while stackl.length > 0 do + l = stackl.pop + c = stackc.pop + if c.length == 0 && l.length == n then + count += 1 + else + place = l.length + 1 + c.each do |candidate| + if !tab[place][candidate] then + q = l[0..-1] + q.push(candidate) + stackl.push(q) + stackc.push(c.reject {|i| i == candidate}) + end + end + end + end + return count +end + +class TestCutelist < Test::Unit::TestCase + + def test_ex1 + assert_equal(2, cutelist(2)); + end + + def test_ex2 + assert_equal(700, cutelist(10)); + end + + def test_ex3 + assert_equal(24679, cutelist(15)); + end +end diff --git a/challenge-191/roger-bell-west/rust/ch-1.rs b/challenge-191/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..12cd0c5e4d --- /dev/null +++ b/challenge-191/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,29 @@ +#! /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!(twicelargest(vec![1, 2, 3, 4]), false); +} + +#[test] +fn test_ex2() { + assert_eq!(twicelargest(vec![1, 2, 0, 5]), true); +} + +#[test] +fn test_ex3() { + assert_eq!(twicelargest(vec![2, 6, 3, 1]), true); +} + +#[test] +fn test_ex4() { + assert_eq!(twicelargest(vec![4, 5, 2, 3]), false); +} + +fn twicelargest(l0: Vec<u64>) -> bool { + let mut l = l0.clone(); + l.sort(); + l.reverse(); + l[0] >= 2 * l[1] +} diff --git a/challenge-191/roger-bell-west/rust/ch-2.rs b/challenge-191/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..25875b2184 --- /dev/null +++ b/challenge-191/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,60 @@ +#! /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!(cutelist(2), 2); +} + +#[test] +fn test_ex2() { + assert_eq!(cutelist(10), 700); +} + +#[test] +fn test_ex3() { + assert_eq!(cutelist(15), 24679); +} + +fn cutelist(n: usize) -> usize { + let mut tab: Vec<Vec<bool>> = Vec::new(); + tab.push(Vec::new()); + for _ in 1..=n { + tab.push(vec![false; n + 1]); + } + for x in 1..=n { + for y in 1..=x { + if x % y != 0 && y % x != 0 { + tab[x][y] = true; + tab[y][x] = true; + } + } + } + let mut count = 0; + let mut stackl: Vec<Vec<usize>> = vec![Vec::new()]; + let mut stackc: Vec<Vec<usize>> = Vec::new(); + stackc.push((1..=n).collect::<Vec<usize>>()); + while stackl.len() > 0 { + let l = stackl.pop().unwrap(); + let c = stackc.pop().unwrap(); + if c.len() == 0 && l.len() == n { + count += 1; + } else { + let place = l.len() + 1; + for &candidate in &c { + if !tab[place][candidate] { + let mut q = l.clone(); + q.push(candidate); + stackl.push(q); + stackc.push( + c.iter() + .filter(|i| **i != candidate) + .map(|i| *i) + .collect::<Vec<usize>>(), + ); + } + } + } + } + count +} |
