aboutsummaryrefslogtreecommitdiff
path: root/challenge-112
diff options
context:
space:
mode:
authorseaker <3043281+seaker@users.noreply.github.com>2021-05-10 23:36:02 +0800
committerseaker <3043281+seaker@users.noreply.github.com>2021-05-10 23:36:02 +0800
commitb82a359f9ab72a3160797bf2d0aba8a0b23fe6ed (patch)
treea00fb83ed6d531b3592a2501e03a6b02157236f8 /challenge-112
parent144a4fa80fbfa1d1356d05d1da1a1e7ff4cd6ba9 (diff)
parent5e535c759ec1826d95b3f49c4033db88803975ca (diff)
downloadperlweeklychallenge-club-b82a359f9ab72a3160797bf2d0aba8a0b23fe6ed.tar.gz
perlweeklychallenge-club-b82a359f9ab72a3160797bf2d0aba8a0b23fe6ed.tar.bz2
perlweeklychallenge-club-b82a359f9ab72a3160797bf2d0aba8a0b23fe6ed.zip
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-112')
-rw-r--r--challenge-112/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-112/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-112/luca-ferrari/raku/ch-1.p614
-rw-r--r--challenge-112/luca-ferrari/raku/ch-2.p626
-rw-r--r--challenge-112/mark-anderson/raku/ch-1.raku10
-rw-r--r--challenge-112/mark-anderson/raku/ch-2.raku137
-rwxr-xr-xchallenge-112/roger-bell-west/perl/ch-1.pl28
-rwxr-xr-xchallenge-112/roger-bell-west/perl/ch-2.pl20
-rwxr-xr-xchallenge-112/roger-bell-west/python/ch-1.py31
-rwxr-xr-xchallenge-112/roger-bell-west/python/ch-2.py26
-rwxr-xr-xchallenge-112/roger-bell-west/raku/ch-1.p626
-rwxr-xr-xchallenge-112/roger-bell-west/raku/ch-2.p618
-rwxr-xr-xchallenge-112/roger-bell-west/ruby/ch-1.rb39
-rwxr-xr-xchallenge-112/roger-bell-west/ruby/ch-2.rb31
-rwxr-xr-xchallenge-112/roger-bell-west/rust/ch-1.rs40
-rwxr-xr-xchallenge-112/roger-bell-west/rust/ch-2.rs29
16 files changed, 477 insertions, 0 deletions
diff --git a/challenge-112/luca-ferrari/blog-1.txt b/challenge-112/luca-ferrari/blog-1.txt
new file mode 100644
index 0000000000..60c0a57573
--- /dev/null
+++ b/challenge-112/luca-ferrari/blog-1.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/05/10/PerlWeeklyChallenge112.html#task1
diff --git a/challenge-112/luca-ferrari/blog-2.txt b/challenge-112/luca-ferrari/blog-2.txt
new file mode 100644
index 0000000000..89da990e83
--- /dev/null
+++ b/challenge-112/luca-ferrari/blog-2.txt
@@ -0,0 +1 @@
+https://fluca1978.github.io/2021/05/10/PerlWeeklyChallenge112.html#task2
diff --git a/challenge-112/luca-ferrari/raku/ch-1.p6 b/challenge-112/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..b984a31b28
--- /dev/null
+++ b/challenge-112/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,14 @@
+#!raku
+
+sub MAIN( Str :$path ) {
+
+ my @results;
+ for $path.split( '/' ) {
+ next if ! $_ || $_ ~~ '.';
+ @results.push: $_ if ( $_ !~~ '..' );
+ @results = @results[ 0 .. * - 2 ] if $_ ~~ '..';
+
+ }
+
+ ('/' ~ @results.join( '/' )).say;
+}
diff --git a/challenge-112/luca-ferrari/raku/ch-2.p6 b/challenge-112/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..c735eb0a1a
--- /dev/null
+++ b/challenge-112/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,26 @@
+#!raku
+
+sub MAIN( Int $top where { $top > 1 } ) {
+ my @solutions;
+ @solutions.push: 1 xx $top;
+
+ my $current-solution = 1 x $top;
+ while ( $current-solution ~~ / 1 ** 2 / ) {
+ $current-solution ~~ s/ 1 ** 2 / 2 /;
+ $current-solution = $current-solution.split( ' ', :skip-empty ).join( '' );
+ @solutions.push: $_ for $current-solution.split( '', :skip-empty ).permutations.unique( as => { .Str.trim } );
+
+
+ }
+
+
+ for @solutions -> @current-solution {
+ say "\nPossible solution:";
+ "%d step%s ".sprintf( $_, $_ > 1 ?? 's' !! '' ).print if $_ > 0 for @current-solution;
+
+ }
+
+ say "";
+
+}
+
diff --git a/challenge-112/mark-anderson/raku/ch-1.raku b/challenge-112/mark-anderson/raku/ch-1.raku
new file mode 100644
index 0000000000..9e7a0b29ef
--- /dev/null
+++ b/challenge-112/mark-anderson/raku/ch-1.raku
@@ -0,0 +1,10 @@
+#!/usr/bin/env raku
+
+use Test;
+plan 3;
+
+is IO::Spec::Unix.canonpath("/a/"), "/a";
+
+is IO::Spec::Unix.canonpath("/a/b//c/"), "/a/b/c";
+
+is IO::Spec::Unix.canonpath("/a/b/c/../..", :parent), "/a";
diff --git a/challenge-112/mark-anderson/raku/ch-2.raku b/challenge-112/mark-anderson/raku/ch-2.raku
new file mode 100644
index 0000000000..7a4edaffde
--- /dev/null
+++ b/challenge-112/mark-anderson/raku/ch-2.raku
@@ -0,0 +1,137 @@
+#!/usr/bin/env raku
+
+multi MAIN
+{
+ use Test;
+ plan 6;
+
+ is stairs(3).elems, 3;
+
+ is stairs(4).elems, 5;
+
+ is stairs(10).elems, 89;
+
+ is-deeply stairs(3), ((1, 1, 1),
+ (1, 2),
+ (2, 1))>>.Seq;
+
+ is-deeply stairs(4), ((1, 1, 1, 1),
+ (1, 1, 2),
+ (1, 2, 1),
+ (2, 1, 1),
+ (2, 2))>>.Seq;
+
+ is-deeply stairs(10), ((1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
+ (1, 1, 1, 1, 1, 1, 1, 1, 2),
+ (1, 1, 1, 1, 1, 1, 1, 2, 1),
+ (1, 1, 1, 1, 1, 1, 2, 1, 1),
+ (1, 1, 1, 1, 1, 1, 2, 2),
+ (1, 1, 1, 1, 1, 2, 1, 1, 1),
+ (1, 1, 1, 1, 1, 2, 1, 2),
+ (1, 1, 1, 1, 1, 2, 2, 1),
+ (1, 1, 1, 1, 2, 1, 1, 1, 1),
+ (1, 1, 1, 1, 2, 1, 1, 2),
+ (1, 1, 1, 1, 2, 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, 1),
+ (1, 2, 2, 1, 2, 2),
+ (1, 2, 2, 2, 1, 1, 1),
+ (1, 2, 2, 2, 1, 2),
+ (1, 2, 2, 2, 2, 1),
+ (2, 1, 1, 1, 1, 1, 1, 1, 1),
+ (2, 1, 1, 1, 1, 1, 1, 2),
+ (2, 1, 1, 1, 1, 1, 2, 1),
+ (2, 1, 1, 1, 1, 2, 1, 1),
+ (2, 1, 1, 1, 1, 2, 2),
+ (2, 1, 1, 1, 2, 1, 1, 1),
+ (2, 1, 1, 1, 2, 1, 2),
+ (2, 1, 1, 1, 2, 2, 1),
+ (2, 1, 1, 2, 1, 1, 1, 1),
+ (2, 1, 1, 2, 1, 1, 2),
+ (2, 1, 1, 2, 1, 2, 1),
+ (2, 1, 1, 2, 2, 1, 1),
+ (2, 1, 1, 2, 2, 2),
+ (2, 1, 2, 1, 1, 1, 1, 1),
+ (2, 1, 2, 1, 1, 1, 2),
+ (2, 1, 2, 1, 1, 2, 1),
+ (2, 1, 2, 1, 2, 1, 1),
+ (2, 1, 2, 1, 2, 2),
+ (2, 1, 2, 2, 1, 1, 1),
+ (2, 1, 2, 2, 1, 2),
+ (2, 1, 2, 2, 2, 1),
+ (2, 2, 1, 1, 1, 1, 1, 1),
+ (2, 2, 1, 1, 1, 1, 2),
+ (2, 2, 1, 1, 1, 2, 1),
+ (2, 2, 1, 1, 2, 1, 1),
+ (2, 2, 1, 1, 2, 2),
+ (2, 2, 1, 2, 1, 1, 1),
+ (2, 2, 1, 2, 1, 2),
+ (2, 2, 1, 2, 2, 1),
+ (2, 2, 2, 1, 1, 1, 1),
+ (2, 2, 2, 1, 1, 2),
+ (2, 2, 2, 1, 2, 1),
+ (2, 2, 2, 2, 1, 1),
+ (2, 2, 2, 2, 2))>>.Seq;
+}
+
+multi MAIN(UInt $u)
+{
+ my $seq := stairs($u);
+
+ say $seq.elems;
+
+ .say for $seq;
+}
+
+sub stairs(UInt $u)
+{
+ my $head = ('100' x $u).substr(0, $u).parse-base(2);
+ my $tail = ('110' x $u).substr(0, $u).parse-base(2);
+
+ ($head..$tail).map({
+ my $b = .base(2);
+
+ unless $b ~~ /000||111/
+ {
+ $b.comb(/(.)$0?/).map(*.chars)
+ }
+ }).sort
+}
diff --git a/challenge-112/roger-bell-west/perl/ch-1.pl b/challenge-112/roger-bell-west/perl/ch-1.pl
new file mode 100755
index 0000000000..c1bed89bc6
--- /dev/null
+++ b/challenge-112/roger-bell-west/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 4;
+
+is(cp('/a/'),'/a','example 1');
+is(cp('/a/b//c/'),'/a/b/c','example 2');
+is(cp('/a/b/c/../..'),'/a','example 3');
+is(cp('/a/./b'),'/a/b','example 4');
+
+sub cp {
+ my $i=shift;
+ my @p=grep {$_ ne '.'} grep /./,split /\//,$i;
+ my $d=1;
+ while ($d) {
+ $d=0;
+ foreach my $pi (1..$#p) {
+ if ($p[$pi] eq '..') {
+ splice @p,$pi-1,2;
+ $d=1;
+ last;
+ }
+ }
+ }
+ return '/'.join('/',@p);
+}
diff --git a/challenge-112/roger-bell-west/perl/ch-2.pl b/challenge-112/roger-bell-west/perl/ch-2.pl
new file mode 100755
index 0000000000..685630026b
--- /dev/null
+++ b/challenge-112/roger-bell-west/perl/ch-2.pl
@@ -0,0 +1,20 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+
+is(cs(3),3,'example 1');
+is(cs(4),5,'example 2');
+is(cs(20),10946,'example 3');
+
+sub cs {
+ my $i=shift;
+ my ($a,$b,$c)=(0,1,0);
+ foreach (1..$i) {
+ $c=$a+$b;
+ ($a,$b)=($b,$c);
+ }
+ return $c;
+}
diff --git a/challenge-112/roger-bell-west/python/ch-1.py b/challenge-112/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..cf1ecbd798
--- /dev/null
+++ b/challenge-112/roger-bell-west/python/ch-1.py
@@ -0,0 +1,31 @@
+#! /usr/bin/python3
+
+import unittest
+
+def cp(i):
+ p=[x for x in i.split('/') if x != '' and x != '.']
+ d=True
+ while d:
+ d=False
+ for pi in range(1,len(p)):
+ if p[pi] == '..':
+ p=p[0:pi-2]+p[pi+1:-1]
+ d=True
+ break
+ return '/'+'/'.join(p)
+
+class TestCp(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(cp('/a/'),'/a','example 1')
+
+ def test_ex2(self):
+ self.assertEqual(cp('/a/b//c/'),'/a/b/c','example 2')
+
+ def test_ex3(self):
+ self.assertEqual(cp('/a/b/c/../..'),'/a','example 3')
+
+ def test_ex4(self):
+ self.assertEqual(cp('/a/./b'),'/a/b','example 4')
+
+unittest.main()
diff --git a/challenge-112/roger-bell-west/python/ch-2.py b/challenge-112/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..77dcdbca19
--- /dev/null
+++ b/challenge-112/roger-bell-west/python/ch-2.py
@@ -0,0 +1,26 @@
+#! /usr/bin/python3
+
+import unittest
+
+def cs(i):
+ a=0
+ b=1
+ c=0
+ for x in range(i):
+ c=a+b
+ a=b
+ b=c
+ return c
+
+class TestCs(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(cs(3),3,'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(cs(4),5,'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(cs(20),10946,'example 3')
+
+unittest.main()
diff --git a/challenge-112/roger-bell-west/raku/ch-1.p6 b/challenge-112/roger-bell-west/raku/ch-1.p6
new file mode 100755
index 0000000000..4b37ee6062
--- /dev/null
+++ b/challenge-112/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,26 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 4;
+
+is(cp('/a/'),'/a','example 1');
+is(cp('/a/b//c/'),'/a/b/c','example 2');
+is(cp('/a/b/c/../..'),'/a','example 3');
+is(cp('/a/./b'),'/a/b','example 4');
+
+sub cp($i) {
+ my @p=$i.split('/').grep(/./).grep({$_ ne '.'});
+ my $d=1;
+ while ($d) {
+ $d=0;
+ for 1..@p.elems-1 -> $pi {
+ if (@p[$pi] eq '..') {
+ splice @p,$pi-1,2;
+ $d=1;
+ last;
+ }
+ }
+ }
+ return '/' ~ join('/',@p);
+}
diff --git a/challenge-112/roger-bell-west/raku/ch-2.p6 b/challenge-112/roger-bell-west/raku/ch-2.p6
new file mode 100755
index 0000000000..49c6b2c458
--- /dev/null
+++ b/challenge-112/roger-bell-west/raku/ch-2.p6
@@ -0,0 +1,18 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 3;
+
+is(cs(3),3,'example 1');
+is(cs(4),5,'example 2');
+is(cs(20),10946,'example 3');
+
+sub cs($i) {
+ my ($a,$b,$c)=(0,1,0);
+ for 1..$i {
+ $c=$a+$b;
+ ($a,$b)=($b,$c);
+ }
+ return $c;
+}
diff --git a/challenge-112/roger-bell-west/ruby/ch-1.rb b/challenge-112/roger-bell-west/ruby/ch-1.rb
new file mode 100755
index 0000000000..e7b8ee5e56
--- /dev/null
+++ b/challenge-112/roger-bell-west/ruby/ch-1.rb
@@ -0,0 +1,39 @@
+#! /usr/bin/ruby
+
+def cp(i)
+ p=i.split('/').find_all {|i| i != '' && i != '.'}
+ d=true
+ while d
+ d=false
+ 1.upto(p.length-1) do |pi|
+ if p[pi] == '..' then
+ p=p[0..pi-2]+p[pi+1..-1]
+ d=true
+ break
+ end
+ end
+ end
+ return '/'+p.join('/')
+end
+
+require 'test/unit'
+
+class TestCp < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal('/a',cp('/a/'))
+ end
+
+ def test_ex2
+ assert_equal('/a/b/c',cp('/a/b//c/'))
+ end
+
+ def test_ex3
+ assert_equal('/a',cp('/a/b/c/../..'))
+ end
+
+ def test_ex4
+ assert_equal('/a/b',cp('/a/./b'))
+ end
+
+end
diff --git a/challenge-112/roger-bell-west/ruby/ch-2.rb b/challenge-112/roger-bell-west/ruby/ch-2.rb
new file mode 100755
index 0000000000..c4659f54ea
--- /dev/null
+++ b/challenge-112/roger-bell-west/ruby/ch-2.rb
@@ -0,0 +1,31 @@
+#! /usr/bin/ruby
+
+def cs(i)
+ a=0
+ b=1
+ c=0
+ 1.upto(i) do
+ c=a+b
+ a=b
+ b=c
+ end
+ return c
+end
+
+require 'test/unit'
+
+class TestCs < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal(3,cs(3))
+ end
+
+ def test_ex2
+ assert_equal(5,cs(4))
+ end
+
+ def test_ex3
+ assert_equal(10946,cs(20))
+ end
+
+end
diff --git a/challenge-112/roger-bell-west/rust/ch-1.rs b/challenge-112/roger-bell-west/rust/ch-1.rs
new file mode 100755
index 0000000000..2125410393
--- /dev/null
+++ b/challenge-112/roger-bell-west/rust/ch-1.rs
@@ -0,0 +1,40 @@
+#! /bin/sh
+//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit
+
+#[test]
+fn test_ex1() {
+ assert_eq!(cp("/a/"),"/a");
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(cp("/a/b//c"),"/a/b/c");
+}
+
+#[test]
+fn test_ex3() {
+ assert_eq!(cp("/a/b/c/../.."),"/a");
+}
+
+#[test]
+fn test_ex4() {
+ assert_eq!(cp("/a/./b"),"/a/b");
+}
+
+fn cp(i: &str) -> String {
+ let mut p=i.split("/").filter(|x| *x != "" && *x != ".").collect::<Vec<_>>();
+ let mut d=true;
+ while d {
+ d=false;
+ for pi in 1..p.len()-1 {
+ if p[pi] == ".." {
+ p=[&p[0..pi-2],&p[pi+1..p.len()-1]].concat();
+ d=true;
+ break;
+ }
+ }
+ }
+ let mut r="/".to_string();
+ r.push_str(&p.join("/"));
+ return r;
+}
diff --git a/challenge-112/roger-bell-west/rust/ch-2.rs b/challenge-112/roger-bell-west/rust/ch-2.rs
new file mode 100755
index 0000000000..e0a8f27aa1
--- /dev/null
+++ b/challenge-112/roger-bell-west/rust/ch-2.rs
@@ -0,0 +1,29 @@
+#! /bin/sh
+//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit
+
+#[test]
+fn test_ex1() {
+ assert_eq!(cs(3),3);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(cs(4),5);
+}
+
+#[test]
+fn test_ex3() {
+ assert_eq!(cs(20),10946);
+}
+
+fn cs(i: u64) -> u64 {
+ let mut a: u64=0;
+ let mut b: u64=1;
+ let mut c: u64=0;
+ for _x in 0..i {
+ c=a+b;
+ a=b;
+ b=c;
+ }
+ return c;
+}