aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-08 18:54:26 +0000
committerGitHub <noreply@github.com>2021-11-08 18:54:26 +0000
commit844f3eb6e7aedd63c3d88bdf7627b76044535c80 (patch)
treeee8a30a4aec70fccbd3df6227aa3d0215ac51700
parent5992ddac20f8eeba239929d52f71ae94b9aba0d6 (diff)
parent8bd24256e3366742e4c62c6353ad72b678c0f1aa (diff)
downloadperlweeklychallenge-club-844f3eb6e7aedd63c3d88bdf7627b76044535c80.tar.gz
perlweeklychallenge-club-844f3eb6e7aedd63c3d88bdf7627b76044535c80.tar.bz2
perlweeklychallenge-club-844f3eb6e7aedd63c3d88bdf7627b76044535c80.zip
Merge pull request #5182 from Firedrake/rogerbw-challenge-138
Solutions for challenge #138
-rwxr-xr-xchallenge-138/roger-bell-west/perl/ch-1.pl28
-rwxr-xr-xchallenge-138/roger-bell-west/perl/ch-2.pl36
-rw-r--r--challenge-138/roger-bell-west/postscript/ch-1.ps37
-rw-r--r--challenge-138/roger-bell-west/postscript/ch-2.ps51
-rwxr-xr-xchallenge-138/roger-bell-west/python/ch-1.py25
-rwxr-xr-xchallenge-138/roger-bell-west/python/ch-2.py37
-rwxr-xr-xchallenge-138/roger-bell-west/raku/ch-1.p624
-rwxr-xr-xchallenge-138/roger-bell-west/raku/ch-2.p634
-rwxr-xr-xchallenge-138/roger-bell-west/ruby/ch-1.rb31
-rwxr-xr-xchallenge-138/roger-bell-west/ruby/ch-2.rb45
-rwxr-xr-xchallenge-138/roger-bell-west/rust/ch-1.rs28
-rwxr-xr-xchallenge-138/roger-bell-west/rust/ch-2.rs43
12 files changed, 419 insertions, 0 deletions
diff --git a/challenge-138/roger-bell-west/perl/ch-1.pl b/challenge-138/roger-bell-west/perl/ch-1.pl
new file mode 100755
index 0000000000..54379c17d9
--- /dev/null
+++ b/challenge-138/roger-bell-west/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 2;
+
+is(workdays(2021),260,'example 1');
+is(workdays(2020),262,'example 2');
+
+sub p {
+ my $y=shift;
+ return ($y+int($y/4)-int($y/100)+int($y/400))%7;
+}
+
+sub leapyear {
+ my $y=shift;
+ return ($y%4==0 && ($y%100 !=0 || $y%400==0));
+}
+
+sub workdays {
+ my $y=shift;
+ my $i=p($y);
+ if (leapyear($y)) {
+ $i+=7;
+ }
+ return 260+[0,1,1,1,1,0,0,1,2,2,2,2,1]->[$i];
+}
diff --git a/challenge-138/roger-bell-west/perl/ch-2.pl b/challenge-138/roger-bell-west/perl/ch-2.pl
new file mode 100755
index 0000000000..cbd6b73f7d
--- /dev/null
+++ b/challenge-138/roger-bell-west/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+
+is(splnum(81),1,'example 1');
+is(splnum(9801),1,'example 2');
+is(splnum(36),0,'example 3');
+
+sub splnum {
+ my $n=shift;
+ my $k=int(sqrt($n));
+ if ($k*$k != $n) {
+ return 0;
+ }
+ my @d=split('',$n);
+ foreach my $s (1..(1<<($#d))-1) {
+ my @s=(0);
+ foreach my $i (0..$#d-1) {
+ if ($s & 1<<$i) {
+ push @s,$i+1;
+ }
+ }
+ push @s,$#d+1;
+ my $c=0;
+ foreach my $j (0..$#s-1) {
+ $c+=join('',@d[$s[$j]..$s[$j+1]-1]);
+ }
+ if ($c == $k) {
+ return 1;
+ }
+ }
+ return 0;
+}
diff --git a/challenge-138/roger-bell-west/postscript/ch-1.ps b/challenge-138/roger-bell-west/postscript/ch-1.ps
new file mode 100644
index 0000000000..cc37431fa7
--- /dev/null
+++ b/challenge-138/roger-bell-west/postscript/ch-1.ps
@@ -0,0 +1,37 @@
+%!PS
+
+/p {
+ dup dup dup
+ 4 idiv 4 1 roll
+ 100 idiv neg 4 1 roll
+ 400 idiv
+ add add add
+ 7 mod
+} bind def
+
+/leapyear {
+ dup dup
+ 4 mod 0 eq {
+ 100 mod 0 ne exch
+ 400 mod 0 eq or
+ {
+ true
+ } {
+ false
+ } ifelse
+ } {
+ pop pop false
+ } ifelse
+} bind def
+
+/workdays {
+ dup
+ p exch
+ leapyear {
+ 7 add
+ } if
+ [ 0 1 1 1 1 0 0 1 2 2 2 2 1 ] exch get 260 add
+} bind def
+
+2021 workdays 260 eq { (Pass) } { (FAIL) } ifelse print ( ) print
+2020 workdays 262 eq { (Pass) } { (FAIL) } ifelse =
diff --git a/challenge-138/roger-bell-west/postscript/ch-2.ps b/challenge-138/roger-bell-west/postscript/ch-2.ps
new file mode 100644
index 0000000000..56f4a27967
--- /dev/null
+++ b/challenge-138/roger-bell-west/postscript/ch-2.ps
@@ -0,0 +1,51 @@
+%!PS
+
+/apush { % [a b] c -> [a b c]
+ /t exch def
+ [ exch aload pop t ]
+} bind def
+
+/i2s {
+ dup log cvi 1 add string cvs
+} bind def
+
+/splnum {
+ /n exch def
+ /ret 0 def
+ n sqrt cvi /k exch def
+ k k mul n ne {
+ 0 exit
+ } if
+ /d n i2s def
+ /dl d length 1 sub def
+ 1 1 1 dl bitshift 1 sub {
+ /sa [ 0 ] def
+ /s exch def
+ 0 1 dl 1 sub {
+ /i exch def
+ s 1 i bitshift and 0 gt {
+ /sa sa i 1 add apush def
+ } if
+ } for
+ /sa sa dl 1 add apush def
+ /c 0 def
+ 0 1 sa length 2 sub {
+ /j exch def
+ d
+ sa j get
+ sa j 1 add get sa j get sub
+ getinterval cvi
+ c add /c exch def
+ } for
+ c k eq {
+ /ret 1 def
+ exit
+ } if
+ } for
+ ret
+} bind def
+
+81 splnum 1 eq { (Pass) } { (FAIL) } ifelse print ( ) print
+9801 splnum 1 eq { (Pass) } { (FAIL) } ifelse print ( ) print
+36 splnum 0 eq { (Pass) } { (FAIL) }
+ifelse =
diff --git a/challenge-138/roger-bell-west/python/ch-1.py b/challenge-138/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..a952b5741a
--- /dev/null
+++ b/challenge-138/roger-bell-west/python/ch-1.py
@@ -0,0 +1,25 @@
+#! /usr/bin/python3
+
+import unittest
+
+def p(y):
+ return (y+int(y/4)-int(y/100)+int(y/400)) % 7
+
+def leapyear(y):
+ return y%4==0 and (y%100!=0 or y%400==0)
+
+def workdays(y):
+ i=p(y)
+ if leapyear(y):
+ i+=7
+ return 260+[0,1,1,1,1,0,0,1,2,2,2,2,1][i]
+
+class TestWorkdays(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(workdays(2021),260,'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(workdays(2020),262,'example 2')
+
+unittest.main()
diff --git a/challenge-138/roger-bell-west/python/ch-2.py b/challenge-138/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..1f143c7abc
--- /dev/null
+++ b/challenge-138/roger-bell-west/python/ch-2.py
@@ -0,0 +1,37 @@
+#! /usr/bin/python3
+
+import unittest
+
+from math import sqrt
+
+def splnum(n):
+ k=int(sqrt(n))
+ if k*k != n:
+ return 0
+ d=str(n)
+ dl=len(d)-1
+ for s in range(1,(1<<dl)):
+ sa=[0]
+ for i in range(dl):
+ if s & (1<<i):
+ sa.append(i+1)
+ sa.append(dl+1)
+ c=0
+ for j in range(len(sa)-1):
+ c+=int(d[sa[j]:sa[j+1]])
+ if c==k:
+ return 1
+ return 0
+
+class TestSplnum(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(splnum(81),1,'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(splnum(9801),1,'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(splnum(36),0,'example 3')
+
+unittest.main()
diff --git a/challenge-138/roger-bell-west/raku/ch-1.p6 b/challenge-138/roger-bell-west/raku/ch-1.p6
new file mode 100755
index 0000000000..7fec7d73ef
--- /dev/null
+++ b/challenge-138/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,24 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 2;
+
+is(workdays(2021),260,'example 1');
+is(workdays(2020),262,'example 2');
+
+sub p($y) {
+ return ($y+floor($y/4)-floor($y/100)+floor($y/400))%7;
+}
+
+sub leapyear($y) {
+ return ($y%4==0 && ($y%100 != 0 || $y%400 == 0));
+}
+
+sub workdays($y) {
+ my $i=p($y);
+ if (leapyear($y)) {
+ $i+=7;
+ }
+ return 260+[0,1,1,1,1,0,0,1,2,2,2,2,1].[$i];
+}
diff --git a/challenge-138/roger-bell-west/raku/ch-2.p6 b/challenge-138/roger-bell-west/raku/ch-2.p6
new file mode 100755
index 0000000000..1fc0233115
--- /dev/null
+++ b/challenge-138/roger-bell-west/raku/ch-2.p6
@@ -0,0 +1,34 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 3;
+
+is(splnum(81),1,'example 1');
+is(splnum(9801),1,'example 2');
+is(splnum(36),0,'example 3');
+
+sub splnum($n) {
+ my $k=floor(sqrt($n));
+ if ($k*$k != $n) {
+ return 0;
+ }
+ my @d=$n.comb;
+ for (1..(1 +< (@d.end))-1) -> $s {
+ my @s=(0);
+ for (0..@d.end-1) -> $i {
+ if ($s +& (1 +< $i)) {
+ push @s,$i+1;
+ }
+ }
+ push @s,@d.end+1;
+ my $c=0;
+ for (0..@s.end-1) -> $j {
+ $c+=join('',@d[@s[$j]..@s[$j+1]-1]);
+ }
+ if ($c == $k) {
+ return 1;
+ }
+ }
+ return 0;
+}
diff --git a/challenge-138/roger-bell-west/ruby/ch-1.rb b/challenge-138/roger-bell-west/ruby/ch-1.rb
new file mode 100755
index 0000000000..eb91003f89
--- /dev/null
+++ b/challenge-138/roger-bell-west/ruby/ch-1.rb
@@ -0,0 +1,31 @@
+#! /usr/bin/ruby
+
+def p(y)
+ return (y+y.div(4)-y.div(100)+y.div(400)) % 7
+end
+
+def leapyear(y)
+ return y%4==0 && (y%100!=0 || y%400==0)
+end
+
+def workdays(y)
+ i=p(y)
+ if leapyear(y) then
+ i+=7
+ end
+ return 260+[0,1,1,1,1,0,0,1,2,2,2,2,1][i]
+end
+
+require 'test/unit'
+
+class TestWorkdays < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal(260,workdays(2021))
+ end
+
+ def test_ex2
+ assert_equal(262,workdays(2020))
+ end
+
+end
diff --git a/challenge-138/roger-bell-west/ruby/ch-2.rb b/challenge-138/roger-bell-west/ruby/ch-2.rb
new file mode 100755
index 0000000000..bb13f91e00
--- /dev/null
+++ b/challenge-138/roger-bell-west/ruby/ch-2.rb
@@ -0,0 +1,45 @@
+#! /usr/bin/ruby
+
+def splnum(n)
+ k=Integer.sqrt(n)
+ if k*k!=n then
+ return 0
+ end
+ d=n.to_s.split("")
+ dl=d.length-1
+ 1.upto((1<<dl)-1) do |s|
+ sa=[0]
+ 0.upto(dl-1) do |i|
+ if s & (1<<i) > 0 then
+ sa.push(i+1)
+ end
+ end
+ sa.push(dl+1)
+ c=0
+ 0.upto(sa.length()-2) do |j|
+ c+=d.slice(sa[j],sa[j+1]-sa[j]).join("").to_i
+ end
+ if c==k then
+ return 1
+ end
+ end
+ return 0
+end
+
+require 'test/unit'
+
+class TestSplnum < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal(1,splnum(81))
+ end
+
+ def test_ex2
+ assert_equal(1,splnum(9801))
+ end
+
+ def test_ex3
+ assert_equal(0,splnum(36))
+ end
+
+end
diff --git a/challenge-138/roger-bell-west/rust/ch-1.rs b/challenge-138/roger-bell-west/rust/ch-1.rs
new file mode 100755
index 0000000000..54cbbb5a0c
--- /dev/null
+++ b/challenge-138/roger-bell-west/rust/ch-1.rs
@@ -0,0 +1,28 @@
+#! /bin/sh
+//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit
+
+#[test]
+fn test_ex1() {
+ assert_eq!(workdays(2021),260);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(workdays(2020),262);
+}
+
+fn p(y: i32) -> i32 {
+ (y+y/4-y/100+y/400) % 7
+}
+
+fn leapyear(y: i32) -> bool {
+ y%4==0 && (y%100!=0 || y%400==0)
+}
+
+fn workdays(y: i32) -> i32 {
+ let mut i=p(y);
+ if leapyear(y) {
+ i+=7;
+ }
+ 260+vec![0,1,1,1,1,0,0,1,2,2,2,2,1][i as usize]
+}
diff --git a/challenge-138/roger-bell-west/rust/ch-2.rs b/challenge-138/roger-bell-west/rust/ch-2.rs
new file mode 100755
index 0000000000..2e60acb97f
--- /dev/null
+++ b/challenge-138/roger-bell-west/rust/ch-2.rs
@@ -0,0 +1,43 @@
+#! /bin/sh
+//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit
+
+#[test]
+fn test_ex1() {
+ assert_eq!(splnum(81),1);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(splnum(9801),1);
+}
+
+#[test]
+fn test_ex3() {
+ assert_eq!(splnum(36),0);
+}
+
+fn splnum(n: u32) -> u8 {
+ let k=(n as f64).sqrt() as u32;
+ if k*k != n {
+ return 0
+ }
+ let d=n.to_string();
+ let dl=d.len()-1;
+ for s in 1..1<<dl {
+ let mut sa=vec![0];
+ for i in 0..dl {
+ if (s & (1<<i)) > 0 {
+ sa.push(i+1);
+ }
+ }
+ sa.push(dl+1);
+ let mut c=0;
+ for j in 0..sa.len()-1 {
+ c+=d[sa[j]..sa[j+1]].to_string().parse::<u32>().unwrap();
+ }
+ if c==k {
+ return 1;
+ }
+ }
+ 0
+}