aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoger Bell_West <roger@firedrake.org>2020-09-28 12:16:46 +0100
committerRoger Bell_West <roger@firedrake.org>2020-09-28 12:16:46 +0100
commit2dacaeb84251fa615f4b2b283a76e56e8a7102f7 (patch)
tree338749006074b2b7bab06f9451e85fcb717e4b33
parentaa14cbf8342e04b936f40bcc720a23a258137ecd (diff)
downloadperlweeklychallenge-club-2dacaeb84251fa615f4b2b283a76e56e8a7102f7.tar.gz
perlweeklychallenge-club-2dacaeb84251fa615f4b2b283a76e56e8a7102f7.tar.bz2
perlweeklychallenge-club-2dacaeb84251fa615f4b2b283a76e56e8a7102f7.zip
Solutions for challenge #80.
-rwxr-xr-xchallenge-080/roger-bell-west/perl/ch-1.pl20
-rwxr-xr-xchallenge-080/roger-bell-west/perl/ch-2.pl32
-rwxr-xr-xchallenge-080/roger-bell-west/python/ch-1.py23
-rwxr-xr-xchallenge-080/roger-bell-west/python/ch-2.py25
-rwxr-xr-xchallenge-080/roger-bell-west/raku/ch-1.p618
-rwxr-xr-xchallenge-080/roger-bell-west/raku/ch-2.p628
-rwxr-xr-xchallenge-080/roger-bell-west/ruby/ch-1.rb33
-rwxr-xr-xchallenge-080/roger-bell-west/ruby/ch-2.rb31
8 files changed, 210 insertions, 0 deletions
diff --git a/challenge-080/roger-bell-west/perl/ch-1.pl b/challenge-080/roger-bell-west/perl/ch-1.pl
new file mode 100755
index 0000000000..ae075e6ffb
--- /dev/null
+++ b/challenge-080/roger-bell-west/perl/ch-1.pl
@@ -0,0 +1,20 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+
+is(spn([5,2,-2,0]),1,'example 1');
+is(spn([1,8,-1]),2,'example 2');
+is(spn([2,0,-1]),1,'example 3');
+
+sub spn {
+ my @list=@{shift @_};
+ my %r=map {$_ => 1} grep {$_ > 0} @list;
+ my $m=1;
+ while (exists $r{$m}) {
+ $m++;
+ }
+ return $m;
+}
diff --git a/challenge-080/roger-bell-west/perl/ch-2.pl b/challenge-080/roger-bell-west/perl/ch-2.pl
new file mode 100755
index 0000000000..c0a92585e6
--- /dev/null
+++ b/challenge-080/roger-bell-west/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+use List::Util qw(max sum);
+
+use Test::More tests => 2;
+
+is(cc([1,2,2]),4,'example 1');
+is(cc([1,4,3,2]),7,'example 2');
+
+sub cc {
+ my @list=@{shift @_};
+ my @n=sort {$list[$a] <=> $list[$b]} (0..$#list);
+ my @k;
+ foreach my $i (@n) {
+ my @nr=(1);
+ if ($i > 0 && $list[$i-1] < $list[$i]) {
+ if (defined $k[$i-1]) {
+ push @nr,$k[$i-1]+1;
+ }
+ }
+ if ($i < $#list && $list[$i+1] < $list[$i]) {
+ if (defined $k[$i+1]) {
+ push @nr,$k[$i+1]+1;
+ }
+ }
+ $k[$i]=max(@nr);
+ }
+ return sum(@k);
+}
diff --git a/challenge-080/roger-bell-west/python/ch-1.py b/challenge-080/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..482284d1f2
--- /dev/null
+++ b/challenge-080/roger-bell-west/python/ch-1.py
@@ -0,0 +1,23 @@
+#! /usr/bin/python3
+
+import unittest
+
+def spn(list):
+ r=frozenset(list)
+ m=1
+ while m in r:
+ m += 1
+ return m
+
+class TestSpn(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(spn((5,2,-2,0)),1,'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(spn((1,8,-1)),2,'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(spn((2,0,-1)),1,'example 3')
+
+unittest.main()
diff --git a/challenge-080/roger-bell-west/python/ch-2.py b/challenge-080/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..bccad43f27
--- /dev/null
+++ b/challenge-080/roger-bell-west/python/ch-2.py
@@ -0,0 +1,25 @@
+#! /usr/bin/python3
+
+import unittest
+
+def cc(list):
+ n=sorted(range(len(list)), key=lambda x: list[x])
+ k=[0] * len(list)
+ for i in n:
+ nr=[1]
+ if (i > 0 and list[i-1] < list[i]):
+ nr.append(k[i-1]+1)
+ if (i < len(list)-1 and list[i+1] < list[i]):
+ nr.append(k[i+1]+1)
+ k[i]=max(nr)
+ return sum(k)
+
+class TestCc(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(cc((1,2,2)),4,'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(cc((1,4,3,2)),7,'example 2')
+
+unittest.main()
diff --git a/challenge-080/roger-bell-west/raku/ch-1.p6 b/challenge-080/roger-bell-west/raku/ch-1.p6
new file mode 100755
index 0000000000..bb15522373
--- /dev/null
+++ b/challenge-080/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,18 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 3;
+
+is(spn((5,2,-2,0)),1,'example 1');
+is(spn((1,8,-1)),2,'example 2');
+is(spn((2,0,-1)),1,'example 3');
+
+sub spn(@list) {
+ my $r=set grep {$_ > 0}, @list;
+ my $m=1;
+ while ($r{$m}:exists) {
+ $m++;
+ }
+ return $m;
+}
diff --git a/challenge-080/roger-bell-west/raku/ch-2.p6 b/challenge-080/roger-bell-west/raku/ch-2.p6
new file mode 100755
index 0000000000..83bc7d0dbd
--- /dev/null
+++ b/challenge-080/roger-bell-west/raku/ch-2.p6
@@ -0,0 +1,28 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 2;
+
+is(cc((1,2,2)),4,'example 1');
+is(cc((1,4,3,2)),7,'example 2');
+
+sub cc(@list) {
+ my @n=sort {@list[$^a] <=> @list[$^b]}, (0..@list.end);
+ my @k;
+ for @n -> $i {
+ my @nr=(1);
+ if ($i > 0 && @list[$i-1] < @list[$i]) {
+ if (defined @k[$i-1]) {
+ push @nr,@k[$i-1]+1;
+ }
+ }
+ if ($i < @list.end && @list[$i+1] < @list[$i]) {
+ if (defined @k[$i+1]) {
+ push @nr,@k[$i+1]+1;
+ }
+ }
+ @k[$i]=max(@nr);
+ }
+ return sum(@k);
+}
diff --git a/challenge-080/roger-bell-west/ruby/ch-1.rb b/challenge-080/roger-bell-west/ruby/ch-1.rb
new file mode 100755
index 0000000000..9e4d094f1b
--- /dev/null
+++ b/challenge-080/roger-bell-west/ruby/ch-1.rb
@@ -0,0 +1,33 @@
+#! /usr/bin/ruby
+
+def spn(list)
+ r=Hash.new
+ for x in list
+ if x > 0
+ r[x]=1
+ end
+ end
+ m=1
+ while r.has_key?(m)
+ m += 1
+ end
+ return m
+end
+
+require 'test/unit'
+
+class TestSpn < Test::Unit::TestCase
+
+ def test_1
+ assert_equal(1,spn([5,2,-2,0]))
+ end
+
+ def test_2
+ assert_equal(2,spn([1,8,-1]))
+ end
+
+ def test_3
+ assert_equal(1,spn([2,0,-1]))
+ end
+
+end
diff --git a/challenge-080/roger-bell-west/ruby/ch-2.rb b/challenge-080/roger-bell-west/ruby/ch-2.rb
new file mode 100755
index 0000000000..f1476b3af2
--- /dev/null
+++ b/challenge-080/roger-bell-west/ruby/ch-2.rb
@@ -0,0 +1,31 @@
+#! /usr/bin/ruby
+
+def cc(list)
+ n=(0...list.length).sort_by {|x| list[x]}
+ k=Array.new(list.length) {0}
+ for i in n
+ nr=[1]
+ if i > 0 && list[i-1] < list[i]
+ nr.push(k[i-1]+1)
+ end
+ if i < list.length-1 && list[i+1] < list[i]
+ nr.push(k[i+1]+1)
+ end
+ k[i]=nr.max
+ end
+ return k.sum
+end
+
+require 'test/unit'
+
+class TestCc < Test::Unit::TestCase
+
+ def test_1
+ assert_equal(4,cc([1,2,2]))
+ end
+
+ def test_2
+ assert_equal(7,cc([1,4,3,2]))
+ end
+
+end