diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-03-30 01:19:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-30 01:19:13 +0100 |
| commit | b84169619a0967f016ee478a9735379f002ea19f (patch) | |
| tree | 880ab20afaab5e535b02c032dd6d7750dd68786b | |
| parent | 0beed338bde9c2ee3c13c79878d4225103c346e8 (diff) | |
| parent | f7723047407536508f99a718713d5dbcfd6aea55 (diff) | |
| download | perlweeklychallenge-club-b84169619a0967f016ee478a9735379f002ea19f.tar.gz perlweeklychallenge-club-b84169619a0967f016ee478a9735379f002ea19f.tar.bz2 perlweeklychallenge-club-b84169619a0967f016ee478a9735379f002ea19f.zip | |
Merge pull request #3801 from Firedrake/rogerbw-challenge-106
Rogerbw challenge 106
| -rw-r--r-- | challenge-105/roger-bell-west/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-106/roger-bell-west/perl/ch-1.pl | 23 | ||||
| -rwxr-xr-x | challenge-106/roger-bell-west/perl/ch-2.pl | 43 | ||||
| -rwxr-xr-x | challenge-106/roger-bell-west/python/ch-1.py | 26 | ||||
| -rwxr-xr-x | challenge-106/roger-bell-west/python/ch-2.py | 39 | ||||
| -rwxr-xr-x | challenge-106/roger-bell-west/raku/ch-1.p6 | 21 | ||||
| -rwxr-xr-x | challenge-106/roger-bell-west/raku/ch-2.p6 | 40 | ||||
| -rwxr-xr-x | challenge-106/roger-bell-west/ruby/ch-1.rb | 31 | ||||
| -rwxr-xr-x | challenge-106/roger-bell-west/ruby/ch-2.rb | 53 | ||||
| -rwxr-xr-x | challenge-106/roger-bell-west/rust/ch-1.rs | 30 | ||||
| -rwxr-xr-x | challenge-106/roger-bell-west/rust/ch-2.rs | 62 |
11 files changed, 369 insertions, 0 deletions
diff --git a/challenge-105/roger-bell-west/blog.txt b/challenge-105/roger-bell-west/blog.txt new file mode 100644 index 0000000000..75e4da28e3 --- /dev/null +++ b/challenge-105/roger-bell-west/blog.txt @@ -0,0 +1 @@ +https://blog.firedrake.org/archive/2021/03/Perl_Weekly_Challenge_105__Name_Root.html diff --git a/challenge-106/roger-bell-west/perl/ch-1.pl b/challenge-106/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..853d2be13c --- /dev/null +++ b/challenge-106/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,23 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 3; + +is(mg(2,9,3,5),4,'example 1'); +is(mg(1,3,8,2,0),5,'example 2'); +is(mg(5),0,'example 3'); + +sub mg { + my @a=sort @_; + my $g=0; + foreach my $i (0..$#a-1) { + my $d=abs($a[$i]-$a[$i+1]); + if ($d>$g) { + $g=$d; + } + } + return $g; +} + diff --git a/challenge-106/roger-bell-west/perl/ch-2.pl b/challenge-106/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..2e5e36df15 --- /dev/null +++ b/challenge-106/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,43 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 3; + +is(ds(1,3),"0.(3)",'example 1'); +is(ds(1,2),"0.5",'example 2'); +is(ds(5,66),"0.0(75)",'example 3'); + +sub ds { + my $n=shift; + my $d=shift; + my $quotient=sprintf('%d.',$n/$d); + my $c=10*($n % $d); + while ($c > 0 && $c < $d) { + $c *= 10; + $quotient .= "0"; + } + my @digits; + my %passed; + my $i=0; + while (1) { + if (exists $passed{$c}) { + my @cycle=@digits[$passed{$c}..$#digits]; + my $result=$quotient . join('',@digits[0..$passed{$c}-1]); + if (scalar @cycle > 1 || $cycle[0] != 0) { + $result .= '('.join('',@cycle).')'; + } + if (substr($result,-1,1) eq '.') { + substr($result,-1,1)=''; + } + return $result; + } + my $q=int($c/$d); + my $r=$c % $d; + $passed{$c}=$i; + push @digits,$q; + $i++; + $c=10*$r; + } +} diff --git a/challenge-106/roger-bell-west/python/ch-1.py b/challenge-106/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..c1fbba2c7d --- /dev/null +++ b/challenge-106/roger-bell-west/python/ch-1.py @@ -0,0 +1,26 @@ +#! /usr/bin/python3 + +def mg(*aa): + a=list(aa) + a.sort() + g=0 + for i in range(len(a)-1): + d=abs(a[i]-a[i+1]) + if d>g: + g=d + return g + +import unittest + +class TestMg(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(mg(2,9,3,5),4,'example 1') + + def test_ex2(self): + self.assertEqual(mg(1,3,8,2,0),5,'example 2') + + def test_ex3(self): + self.assertEqual(mg(5),0,'example 3') + +unittest.main() diff --git a/challenge-106/roger-bell-west/python/ch-2.py b/challenge-106/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..e1a0d0566d --- /dev/null +++ b/challenge-106/roger-bell-west/python/ch-2.py @@ -0,0 +1,39 @@ +#! /usr/bin/python3 + +def ds(n,d): + quotient=str(n // d) + '.' + c=10*(n % d) + while (c > 0 and c < d): + c *= 10 + quotient += "0" + digits=list() + passed=dict() + i=0 + while 1: + if c in passed: + cycle=digits[passed[c]:] + result=quotient + ''.join(digits[0:passed[c]]) + if len(cycle)>1 or cycle[0] != '0': + result += "(" + ''.join(cycle) + ")" + return result.rstrip('.') + q=c // d + r=c % d + passed[c]=i + digits.append(str(q)) + i += 1 + c=10*r + +import unittest + +class TestDs(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(ds(1,3),"0.(3)",'example 1') + + def test_ex2(self): + self.assertEqual(ds(1,2),"0.5",'example 2') + + def test_ex3(self): + self.assertEqual(ds(5,66),"0.0(75)",'example 3') + +unittest.main() diff --git a/challenge-106/roger-bell-west/raku/ch-1.p6 b/challenge-106/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..28776c3395 --- /dev/null +++ b/challenge-106/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,21 @@ +#! /usr/bin/perl6 + +use Test; + +plan 3; + +is(mg(2,9,3,5),4,'example 1'); +is(mg(1,3,8,2,0),5,'example 2'); +is(mg(5),0,'example 3'); + +sub mg(**@aa) { + my @a=@aa.sort; + my $g=0; + for (0..@a.elems-2) -> $i { + my $d=abs(@a[$i]-@a[$i+1]); + if ($d>$g) { + $g=$d; + } + } + return $g; +} diff --git a/challenge-106/roger-bell-west/raku/ch-2.p6 b/challenge-106/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..aef2e8f56a --- /dev/null +++ b/challenge-106/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,40 @@ +#! /usr/bin/perl6 + +use Test; + +plan 3; + +is(ds(1,3),"0.(3)",'example 1'); +is(ds(1,2),"0.5",'example 2'); +is(ds(5,66),"0.0(75)",'example 3'); + +sub ds($n,$d) { + my $quotient=sprintf('%d.',$n/$d); + my $c=10*($n % $d); + while ($c > 0 && $c < $d) { + $c *= 10; + $quotient ~= "0"; + } + my @digits; + my %passed; + my $i=0; + while (1) { + if (%passed{$c}:exists) { + my @cycle=@digits[%passed{$c}..@digits.elems-1]; + my $result=$quotient ~ join('', @digits[0..%passed{$c}-1]); + if (@cycle.elems > 1 || @cycle[0] != 0) { + $result ~= '(' ~ join('',@cycle) ~ ')'; + } + if (substr($result,*-1,1) eq '.') { + substr($result,*-1,1)=''; + } + return $result; + } + my $q=floor($c/$d); + my $r=$c % $d; + %passed{$c}=$i; + push @digits,$q; + $i++; + $c=10*$r; + } +} diff --git a/challenge-106/roger-bell-west/ruby/ch-1.rb b/challenge-106/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..ddc295ad78 --- /dev/null +++ b/challenge-106/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,31 @@ +#! /usr/bin/ruby + +def mg(*aa) + a=aa.sort() + g=0 + 0.upto(a.length()-2) do |i| + d=(a[i]-a[i+1]).abs + if d>g then + g=d + end + end + return g +end + +require 'test/unit' + +class TestMg < Test::Unit::TestCase + + def test_ex1 + assert_equal(4,mg(2,9,3,5)) + end + + def test_ex2 + assert_equal(5,mg(1,3,8,2,0)) + end + + def test_ex3 + assert_equal(0,mg(5)) + end + +end diff --git a/challenge-106/roger-bell-west/ruby/ch-2.rb b/challenge-106/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..ade1923c5f --- /dev/null +++ b/challenge-106/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,53 @@ +#! /usr/bin/ruby + +def ds(n,d) + quotient=sprintf('%d.',n/d) + c=10*(n % d) + while c>0 && c < d + c *= 10 + quotient += "0" + end + digits=Array.new + passed=Hash.new + i=0 + while 1 + if passed.has_key?(c) then + cycle=digits[passed[c]..-1] + result=quotient + if passed[c]>0 then + result += digits[0..passed[c]-1].join('') + end + if cycle.length>1 || cycle[0] != 0 then + result += "(" + cycle.join('') + ")" + end + if result[-1] == '.' then + result[-1]='' + end + return result + end + q=(c/d).floor + r=c % d + passed[c]=i + digits.push(q) + i+=1 + c=10*r + end +end + +require 'test/unit' + +class TestDs < Test::Unit::TestCase + + def test_ex1 + assert_equal("0.(3)",ds(1,3)) + end + + def test_ex2 + assert_equal("0.5",ds(1,2)) + end + + def test_ex3 + assert_equal("0.0(75)",ds(5,66)) + end + +end diff --git a/challenge-106/roger-bell-west/rust/ch-1.rs b/challenge-106/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..28069d8289 --- /dev/null +++ b/challenge-106/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,30 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(mg(vec![2,9,3,5]),4); +} + +#[test] +fn test_ex2() { + assert_eq!(mg(vec![1,3,8,2,0]),5); +} + +#[test] +fn test_ex3() { + assert_eq!(mg(vec![5]),0); +} + +fn mg(aa: Vec<i64>) -> i64 { + let mut a=aa; + a.sort(); + let mut g=0; + for i in 0..a.len()-1 { + let d=(a[i]-a[i+1]).abs(); + if d>g { + g=d; + } + } + return g; +} diff --git a/challenge-106/roger-bell-west/rust/ch-2.rs b/challenge-106/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..30be767dc4 --- /dev/null +++ b/challenge-106/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,62 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +use std::collections::HashMap; + +#[test] +fn test_ex1() { + assert_eq!(ds(1,3),"0.(3)"); +} + +#[test] +fn test_ex2() { + assert_eq!(ds(1,2),"0.5"); +} + +#[test] +fn test_ex3() { + assert_eq!(ds(5,66),"0.0(75)"); +} + +fn ds(n: i64, d: i64) -> String { + let mut quotient: String=format!("{}",n/d); + quotient.push_str("."); + let mut c: i64=10*(n % d); + while c > 0 && c < d { + c *= 10; + quotient.push_str("0"); + } + let mut digits: Vec<char>=vec![]; + let mut passed: HashMap<i64,usize>=HashMap::new(); + let mut i: usize=0; + loop { + if passed.contains_key(&c) { + let pc=*(passed.get(&c).unwrap()); + let cycle=&digits[pc..digits.len() as usize].to_vec(); + let mut result: String=quotient; + if pc>0 { + for ix in 0..pc { + result.push(digits[ix]); + } + } + if cycle.len()>1 || cycle[0] != '0' { + result.push('('); + for cc in cycle { + result.push(*cc); + } + result.push(')'); + } + let cc=result.pop().unwrap(); + if cc != '.' { + result.push(cc); + } + return result; + } + let q: i64=c/d; + let r=c % d; + passed.insert(c,i); + digits.push(std::char::from_digit(q as u32,10).unwrap()); + i += 1; + c=10*r; + } +} |
