aboutsummaryrefslogtreecommitdiff
path: root/challenge-126/roger-bell-west
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2021-08-18 15:07:49 -0400
committerDave Jacoby <jacoby.david@gmail.com>2021-08-18 15:07:49 -0400
commit710837b2a38b5fbd4404f8d9be06ef368af0d366 (patch)
tree8796e6c6c7be4f7f935e68f3f0bff99bed04eb57 /challenge-126/roger-bell-west
parent637f7bbeceeaf4a26e1e6ecba8c6fcb308790bb3 (diff)
parent73470591f10490d6385145990c49825266f80b2b (diff)
downloadperlweeklychallenge-club-710837b2a38b5fbd4404f8d9be06ef368af0d366.tar.gz
perlweeklychallenge-club-710837b2a38b5fbd4404f8d9be06ef368af0d366.tar.bz2
perlweeklychallenge-club-710837b2a38b5fbd4404f8d9be06ef368af0d366.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-126/roger-bell-west')
-rwxr-xr-xchallenge-126/roger-bell-west/perl/ch-1.pl34
-rwxr-xr-xchallenge-126/roger-bell-west/perl/ch-2.pl47
-rwxr-xr-xchallenge-126/roger-bell-west/python/ch-1.py36
-rwxr-xr-xchallenge-126/roger-bell-west/python/ch-2.py35
-rwxr-xr-xchallenge-126/roger-bell-west/raku/ch-1.p632
-rwxr-xr-xchallenge-126/roger-bell-west/raku/ch-2.p643
-rwxr-xr-xchallenge-126/roger-bell-west/ruby/ch-1.rb45
-rwxr-xr-xchallenge-126/roger-bell-west/ruby/ch-2.rb40
-rwxr-xr-xchallenge-126/roger-bell-west/rust/ch-1.rs44
-rwxr-xr-xchallenge-126/roger-bell-west/rust/ch-2.rs56
10 files changed, 412 insertions, 0 deletions
diff --git a/challenge-126/roger-bell-west/perl/ch-1.pl b/challenge-126/roger-bell-west/perl/ch-1.pl
new file mode 100755
index 0000000000..fbab7a3346
--- /dev/null
+++ b/challenge-126/roger-bell-west/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 4;
+
+is(cn(15),8,'example 1');
+is(cn(25),13,'example 2');
+is(cn(10000),6560,'example 3');
+is(cn(100000000),43046720,'example 4');
+
+sub cn {
+ my $n=shift;
+ my $k=$n;
+ my @digits;
+ while ($k>0) {
+ my $d=$k%10;
+ if ($d==1) {
+ @digits=(8) x scalar(@digits);
+ }
+ if ($d>0) {
+ $d--;
+ }
+ push @digits,$d;
+ $k=int($k/10);
+ }
+ my $tc=0;
+ foreach my $i (reverse(0..$#digits)) {
+ $tc*=9;
+ $tc+=$digits[$i];
+ }
+ return $tc;
+}
diff --git a/challenge-126/roger-bell-west/perl/ch-2.pl b/challenge-126/roger-bell-west/perl/ch-2.pl
new file mode 100755
index 0000000000..146cd920a0
--- /dev/null
+++ b/challenge-126/roger-bell-west/perl/ch-2.pl
@@ -0,0 +1,47 @@
+#! /usr/bin/perl
+
+use strict;
+
+use List::Util qw(min max);
+
+my @in=(
+ [qw(x * * * x * x x x x)],
+ [qw(* * * * * * * * * x)],
+ [qw(* * * * x * x * x *)],
+ [qw(* * * x x * * * * *)],
+ [qw(x * * * x * * * * x)],
+ );
+
+my $ymax=$#in;
+my $xmax=$#{$in[0]};
+
+my @mn;
+foreach (0..$ymax) {
+ push @mn,[(0) x ($xmax+1)];
+}
+
+foreach my $y (0..$ymax) {
+ my @sy=(max(0,$y-1)..min($ymax,$y+1));
+ foreach my $x (0..$xmax) {
+ my @sx=(max(0,$x-1)..min($xmax,$x+1));
+ if ($in[$y][$x] eq 'x') {
+ foreach my $yi (@sy) {
+ foreach my $xi (@sx) {
+ if ($xi==$x && $yi==$y) {
+ next;
+ }
+ $mn[$yi][$xi]++;
+ }
+ }
+ }
+ }
+}
+
+foreach my $y (0..$ymax) {
+ foreach my $x (0..$xmax) {
+ if ($in[$y][$x] eq 'x') {
+ $mn[$y][$x]='x';
+ }
+ }
+ print join(' ',@{$mn[$y]}),"\n";
+}
diff --git a/challenge-126/roger-bell-west/python/ch-1.py b/challenge-126/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..d50faf6626
--- /dev/null
+++ b/challenge-126/roger-bell-west/python/ch-1.py
@@ -0,0 +1,36 @@
+#! /usr/bin/python3
+
+import unittest
+
+def cn(n):
+ k=n
+ digits=[]
+ while k>0:
+ d=k % 10
+ if d==1:
+ digits=[8] * len(digits)
+ if d>0:
+ d -= 1
+ digits.append(d)
+ k=int(k/10)
+ tc=0
+ for i in range(len(digits)-1,-1,-1):
+ tc *= 9
+ tc += digits[i]
+ return tc
+
+class TestCn(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(cn(15),8,'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(cn(25),13,'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(cn(10000),6560,'example 3')
+
+ def test_ex4(self):
+ self.assertEqual(cn(100000000),43046720,'example 3')
+
+unittest.main()
diff --git a/challenge-126/roger-bell-west/python/ch-2.py b/challenge-126/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..f5163b5adb
--- /dev/null
+++ b/challenge-126/roger-bell-west/python/ch-2.py
@@ -0,0 +1,35 @@
+#! /usr/bin/python3
+
+ina=[
+ ['x','*','*','*','x','*','x','x','x','x'],
+ ['*','*','*','*','*','*','*','*','*','x'],
+ ['*','*','*','*','x','*','x','*','x','*'],
+ ['*','*','*','x','x','*','*','*','*','*'],
+ ['x','*','*','*','x','*','*','*','*','x']
+]
+
+ysiz=len(ina)
+xsiz=len(ina[0])
+
+mn=[]
+for i in range(ysiz):
+ mn.append([0] * xsiz)
+
+for y in range(ysiz):
+ sy=range(max(0,y-1),min(ysiz,y+2))
+ for x in range(xsiz):
+ sx=range(max(0,x-1),min(xsiz,x+2))
+ if ina[y][x] == 'x':
+ for yi in sy:
+ for xi in sx:
+ if xi==x and yi==y:
+ continue
+ mn[yi][xi] += 1
+
+for y in range(ysiz):
+ for x in range(xsiz):
+ if ina[y][x] == 'x':
+ mn[y][x] = 'x'
+ else:
+ mn[y][x] = str(mn[y][x])
+ print(" ".join(mn[y]))
diff --git a/challenge-126/roger-bell-west/raku/ch-1.p6 b/challenge-126/roger-bell-west/raku/ch-1.p6
new file mode 100755
index 0000000000..c3dc09ea03
--- /dev/null
+++ b/challenge-126/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,32 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 4;
+
+is(cn(15),8,'example 1');
+is(cn(25),13,'example 2');
+is(cn(10000),6560,'example 3');
+is(cn(100000000),43046720,'example 4');
+
+sub cn($n) {
+ my $k=$n;
+ my @digits;
+ while ($k>0) {
+ my $d=$k%10;
+ if ($d==1) {
+ @digits=(8) xx @digits.elems;
+ }
+ if ($d>0) {
+ $d--;
+ }
+ push @digits,$d;
+ $k=floor($k/10);
+ }
+ my $tc=0;
+ for (0..@digits.end).reverse() -> $i {
+ $tc*=9;
+ $tc+=@digits[$i];
+ }
+ return $tc;
+}
diff --git a/challenge-126/roger-bell-west/raku/ch-2.p6 b/challenge-126/roger-bell-west/raku/ch-2.p6
new file mode 100755
index 0000000000..3f35a4c54f
--- /dev/null
+++ b/challenge-126/roger-bell-west/raku/ch-2.p6
@@ -0,0 +1,43 @@
+#! /usr/bin/perl6
+
+my @in=(
+ [qw{x * * * x * x x x x}],
+ [qw{* * * * * * * * * x}],
+ [qw{* * * * x * x * x *}],
+ [qw{* * * x x * * * * *}],
+ [qw{x * * * x * * * * x}],
+ );
+
+my $ymax=@in.end;
+my $xmax=@in[0].end;
+
+my @mn;
+for 0..$ymax {
+ push @mn,[(0) xx ($xmax+1)];
+}
+
+for (0..$ymax) -> $y {
+ my @sy=(max(0,$y-1)..min($ymax,$y+1));
+ for (0..$xmax) -> $x {
+ my @sx=(max(0,$x-1)..min($xmax,$x+1));
+ if (@in[$y][$x] eq 'x') {
+ for @sy -> $yi {
+ for @sx -> $xi {
+ if ($xi==$x && $yi==$y) {
+ next;
+ }
+ @mn[$yi][$xi]++;
+ }
+ }
+ }
+ }
+}
+
+for (0..$ymax) -> $y {
+ for (0..$xmax) -> $x {
+ if (@in[$y][$x] eq 'x') {
+ @mn[$y][$x]='x';
+ }
+ }
+ say @mn[$y].join(' ');
+}
diff --git a/challenge-126/roger-bell-west/ruby/ch-1.rb b/challenge-126/roger-bell-west/ruby/ch-1.rb
new file mode 100755
index 0000000000..6fc0fd84cb
--- /dev/null
+++ b/challenge-126/roger-bell-west/ruby/ch-1.rb
@@ -0,0 +1,45 @@
+#! /usr/bin/ruby
+
+require 'test/unit'
+
+def cn(n)
+ k=n
+ digits=[]
+ while (k>0)
+ kd=k.divmod(10)
+ k=kd[0]
+ if kd[1]==1 then
+ digits=Array.new(digits.length(),8)
+ end
+ if kd[1]>0 then
+ kd[1] -= 1
+ end
+ digits.push(kd[1])
+ end
+ tc=0
+ (digits.length()-1).downto(0) do |i|
+ tc *= 9
+ tc += digits[i]
+ end
+ return tc
+end
+
+class TestPt < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal(8,cn(15))
+ end
+
+ def test_ex2
+ assert_equal(13,cn(25))
+ end
+
+ def test_ex3
+ assert_equal(6560,cn(10000))
+ end
+
+ def test_ex4
+ assert_equal(43046720,cn(100000000))
+ end
+
+end
diff --git a/challenge-126/roger-bell-west/ruby/ch-2.rb b/challenge-126/roger-bell-west/ruby/ch-2.rb
new file mode 100755
index 0000000000..44a55a8a98
--- /dev/null
+++ b/challenge-126/roger-bell-west/ruby/ch-2.rb
@@ -0,0 +1,40 @@
+#! /usr/bin/ruby
+
+ina=[
+ ['x','*','*','*','x','*','x','x','x','x'],
+ ['*','*','*','*','*','*','*','*','*','x'],
+ ['*','*','*','*','x','*','x','*','x','*'],
+ ['*','*','*','x','x','*','*','*','*','*'],
+ ['x','*','*','*','x','*','*','*','*','x']
+]
+
+ymax=ina.length()-1
+xmax=ina[0].length()-1
+
+mn=Array.new(ymax+1) {Array.new(xmax+1,0)}
+
+0.upto(ymax) do |y|
+ sy=[0,y-1].max.upto([ymax,y+1].min)
+ 0.upto(xmax) do |x|
+ sx=[0,x-1].max.upto([xmax,x+1].min)
+ if ina[y][x] == 'x' then
+ sy.each do |yi|
+ sx.each do |xi|
+ if xi==x && yi==y then
+ next
+ end
+ mn[yi][xi] += 1
+ end
+ end
+ end
+ end
+end
+
+0.upto(ymax) do |y|
+ 0.upto(xmax) do |x|
+ if ina[y][x] == "x" then
+ mn[y][x]="x"
+ end
+ end
+ print(mn[y].join(" ")+"\n")
+end
diff --git a/challenge-126/roger-bell-west/rust/ch-1.rs b/challenge-126/roger-bell-west/rust/ch-1.rs
new file mode 100755
index 0000000000..e10f631917
--- /dev/null
+++ b/challenge-126/roger-bell-west/rust/ch-1.rs
@@ -0,0 +1,44 @@
+#! /bin/sh
+//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit
+
+#[test]
+fn test_ex1() {
+ assert_eq!(cn(15),8);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(cn(25),13);
+}
+
+#[test]
+fn test_ex3() {
+ assert_eq!(cn(10000),6560);
+}
+
+#[test]
+fn test_ex4() {
+ assert_eq!(cn(100000000),43046720);
+}
+
+fn cn(n: i64) -> i64 {
+ let mut k=n;
+ let mut digits: Vec<i64>=vec![];
+ while k>0 {
+ let mut d=k % 10;
+ if d==1 {
+ digits=vec![8;digits.len()];
+ }
+ if d>0 {
+ d -= 1;
+ }
+ digits.push(d);
+ k /= 10;
+ }
+ let mut tc: i64=0;
+ for i in (0..=digits.len()-1).rev() {
+ tc *= 9;
+ tc += digits[i];
+ }
+ return tc;
+}
diff --git a/challenge-126/roger-bell-west/rust/ch-2.rs b/challenge-126/roger-bell-west/rust/ch-2.rs
new file mode 100755
index 0000000000..e9e10695d6
--- /dev/null
+++ b/challenge-126/roger-bell-west/rust/ch-2.rs
@@ -0,0 +1,56 @@
+#! /bin/sh
+//usr/bin/env rustc $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit
+
+fn main() {
+ let ina=vec![
+ vec!['x','*','*','*','x','*','x','x','x','x'],
+ vec!['*','*','*','*','*','*','*','*','*','x'],
+ vec!['*','*','*','*','x','*','x','*','x','*'],
+ vec!['*','*','*','x','x','*','*','*','*','*'],
+ vec!['x','*','*','*','x','*','*','*','*','x']
+ ];
+ let ymax=ina.len()-1;
+ let xmax=ina[0].len()-1;
+ let mut mn: Vec<Vec<u8>>=vec![vec![0;xmax+1];ymax+1];
+ for y in 0..=ymax {
+ for x in 0..=xmax {
+ if ina[y][x] == 'x' {
+ let mut yl=0;
+ if y>1 {
+ yl=y-1;
+ }
+ let mut yh=y+1;
+ if yh>ymax {
+ yh=ymax;
+ }
+ for yi in yl ..= yh {
+ let mut xl=0;
+ if x>1 {
+ xl=x-1;
+ }
+ let mut xh=x+1;
+ if xh>xmax {
+ xh=xmax;
+ }
+ for xi in xl ..= xh {
+ if xi==x && yi==y {
+ continue;
+ }
+ mn[yi][xi] += 1;
+ }
+ }
+ }
+ }
+ }
+ for y in 0..=ymax {
+ let mut line: Vec<String>=vec![];
+ for x in 0..=xmax {
+ if ina[y][x] == 'x' {
+ line.push("x".to_string());
+ } else {
+ line.push(mn[y][x].to_string());
+ }
+ }
+ println!("{}",line.join(" "));
+ }
+}