diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-02 10:38:20 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-02 10:38:20 +0000 |
| commit | 31a3db901f91cf9f1e575687a44da64ea1657b6a (patch) | |
| tree | a648ea44b262c29b47a6b32c200f85c2d6d99e61 | |
| parent | 8a9395def85b52dc3c537a4c216305625cd11c20 (diff) | |
| parent | c667b534cb80c1431f4f46d6dcb6e0ae17e12fd8 (diff) | |
| download | perlweeklychallenge-club-31a3db901f91cf9f1e575687a44da64ea1657b6a.tar.gz perlweeklychallenge-club-31a3db901f91cf9f1e575687a44da64ea1657b6a.tar.bz2 perlweeklychallenge-club-31a3db901f91cf9f1e575687a44da64ea1657b6a.zip | |
Merge pull request #5143 from Firedrake/rogerbw-challenge-137
Solutions for challenge #137
| -rwxr-xr-x | challenge-137/roger-bell-west/perl/ch-1.pl | 23 | ||||
| -rwxr-xr-x | challenge-137/roger-bell-west/perl/ch-2.pl | 25 | ||||
| -rw-r--r-- | challenge-137/roger-bell-west/postscript/ch-1.ps | 30 | ||||
| -rw-r--r-- | challenge-137/roger-bell-west/postscript/ch-2.ps | 40 | ||||
| -rwxr-xr-x | challenge-137/roger-bell-west/python/ch-1.py | 23 | ||||
| -rwxr-xr-x | challenge-137/roger-bell-west/python/ch-2.py | 30 | ||||
| -rwxr-xr-x | challenge-137/roger-bell-west/raku/ch-1.p6 | 22 | ||||
| -rwxr-xr-x | challenge-137/roger-bell-west/raku/ch-2.p6 | 24 | ||||
| -rwxr-xr-x | challenge-137/roger-bell-west/ruby/ch-1.rb | 26 | ||||
| -rwxr-xr-x | challenge-137/roger-bell-west/ruby/ch-2.rb | 38 | ||||
| -rwxr-xr-x | challenge-137/roger-bell-west/rust/ch-1.rs | 22 | ||||
| -rwxr-xr-x | challenge-137/roger-bell-west/rust/ch-2.rs | 37 |
12 files changed, 340 insertions, 0 deletions
diff --git a/challenge-137/roger-bell-west/perl/ch-1.pl b/challenge-137/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..53c04b283c --- /dev/null +++ b/challenge-137/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,23 @@ +#! /usr/bin/perl + +use strict; + +use Test::More tests => 1; + +is_deeply(longyear(),[1903, 1908, 1914, 1920, 1925, + 1931, 1936, 1942, 1948, 1953, + 1959, 1964, 1970, 1976, 1981, + 1987, 1992, 1998, 2004, 2009, + 2015, 2020, 2026, 2032, 2037, + 2043, 2048, 2054, 2060, 2065, + 2071, 2076, 2082, 2088, 2093, + 2099],'example 1'); + +sub p { + my $y=shift; + return ($y+int($y/4)-int($y/100)+int($y/400))%7; +} + +sub longyear { + return [grep {p($_-1)==3 || p($_)==4} (1900..2100)]; +} diff --git a/challenge-137/roger-bell-west/perl/ch-2.pl b/challenge-137/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..0072d9e7f4 --- /dev/null +++ b/challenge-137/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,25 @@ +#! /usr/bin/perl + +use strict; + +use Test::More tests => 4; + +is(lychrel(56),0,'example 1'); +is(lychrel(57),0,'example 2'); +is(lychrel(59),0,'example 3'); +is(lychrel(196),-1,'example 4'); + +sub lychrel { + my $n=shift; + foreach (1..100) { + my $m=join('',reverse split '',$n); + if ($m==$n) { + return 0; + } + $n+=$m; + if ($n>=10000000) { + last; + } + } + return -1; +} diff --git a/challenge-137/roger-bell-west/postscript/ch-1.ps b/challenge-137/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..56ae27ad72 --- /dev/null +++ b/challenge-137/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,30 @@ +%!PS + +/apush { % [a b] c -> [a b c] + /t exch def + [ exch aload pop t ] +} bind def + +/p { + dup dup dup + 4 idiv 4 1 roll + 100 idiv neg 4 1 roll + 400 idiv + add add add + 7 mod +} bind def + +/out 0 array def +1900 1 2100 { + dup + dup p 4 eq + exch + 1 sub p 3 eq + or + { + out exch apush /out exch def + } { + pop + } ifelse +} for +out == diff --git a/challenge-137/roger-bell-west/postscript/ch-2.ps b/challenge-137/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..2388059129 --- /dev/null +++ b/challenge-137/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,40 @@ +%!PS + +/i2s { + dup log cvi 1 add string cvs +} bind def + +/reverse { + 2 dict begin + dup length dup /l exch def string /out exch def + { + /l l 1 sub def + out exch l exch put + } forall + out + end +} bind def + +/lychrel { + /ret -1 def + 500 { + dup i2s reverse cvi + dup 2 index eq { + /ret 0 def + pop + exit + } if + add + dup 1e7 ge { + exit + } if + } repeat + pop + ret +} bind def + +56 lychrel 0 eq { (Pass) } { (FAIL) } ifelse print ( ) print +57 lychrel 0 eq { (Pass) } { (FAIL) } ifelse print ( ) print +59 lychrel 0 eq { (Pass) } { (FAIL) } ifelse print ( ) print +196 lychrel -1 eq { (Pass) } { (FAIL) } ifelse = + diff --git a/challenge-137/roger-bell-west/python/ch-1.py b/challenge-137/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..4ac0381e4e --- /dev/null +++ b/challenge-137/roger-bell-west/python/ch-1.py @@ -0,0 +1,23 @@ +#! /usr/bin/python3 + +import unittest + +def p(y): + return (y+int(y/4)-int(y/100)+int(y/400)) % 7 + +def longyear(): + return [y for y in range(1900,2100+1) if p(y-1)==3 or p(y)==4] + +class TestLongyear(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(longyear(),[1903, 1908, 1914, 1920, 1925, + 1931, 1936, 1942, 1948, 1953, + 1959, 1964, 1970, 1976, 1981, + 1987, 1992, 1998, 2004, 2009, + 2015, 2020, 2026, 2032, 2037, + 2043, 2048, 2054, 2060, 2065, + 2071, 2076, 2082, 2088, 2093, + 2099],'example 1') + +unittest.main() diff --git a/challenge-137/roger-bell-west/python/ch-2.py b/challenge-137/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..ae9c2c921f --- /dev/null +++ b/challenge-137/roger-bell-west/python/ch-2.py @@ -0,0 +1,30 @@ +#! /usr/bin/python3 + +import unittest + +def lychrel(nn): + n=nn + for _ in range(100): + m=int(str(n)[::-1]) + if m==n: + return 0 + n+=m + if n>1e7: + break + return -1 + +class TestLychrel(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(lychrel(56),0,'example 1') + + def test_ex2(self): + self.assertEqual(lychrel(57),0,'example 2') + + def test_ex3(self): + self.assertEqual(lychrel(58),0,'example 3') + + def test_ex4(self): + self.assertEqual(lychrel(196),-1,'example 4') + +unittest.main() diff --git a/challenge-137/roger-bell-west/raku/ch-1.p6 b/challenge-137/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..a6c2b659ea --- /dev/null +++ b/challenge-137/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,22 @@ +#! /usr/bin/perl6 + +use Test; + +plan 1; + +is-deeply(longyear(),[1903, 1908, 1914, 1920, 1925, + 1931, 1936, 1942, 1948, 1953, + 1959, 1964, 1970, 1976, 1981, + 1987, 1992, 1998, 2004, 2009, + 2015, 2020, 2026, 2032, 2037, + 2043, 2048, 2054, 2060, 2065, + 2071, 2076, 2082, 2088, 2093, + 2099],'example 1'); + +sub p($y) { + return ($y+floor($y/4)-floor($y/100)+floor($y/400))%7; +} + +sub longyear { + return [grep {p($_-1)==3 || p($_)==4}, (1900..2100)]; +} diff --git a/challenge-137/roger-bell-west/raku/ch-2.p6 b/challenge-137/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..a0a425ce7e --- /dev/null +++ b/challenge-137/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,24 @@ +#! /usr/bin/perl6 + +use Test; +plan 4; + +is(lychrel(56),0,'example 1'); +is(lychrel(57),0,'example 2'); +is(lychrel(59),0,'example 3'); +is(lychrel(196),-1,'example 4'); + +sub lychrel($nn) { + my $n=$nn; + for (1..100) { + my $m=$n.comb.reverse.join(''); + if ($m==$n) { + return 0; + } + $n+=$m; + if ($n>=10000000) { + last; + } + } + return -1; +} diff --git a/challenge-137/roger-bell-west/ruby/ch-1.rb b/challenge-137/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..c095554e85 --- /dev/null +++ b/challenge-137/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,26 @@ +#! /usr/bin/ruby + +def p(y) + return (y+y.div(4)-y.div(100)+y.div(400)) % 7 +end + +def longyear() + return 1900.upto(2100).find_all {|y| p(y-1)==3 || p(y)==4} +end + +require 'test/unit' + +class TestLongyear < Test::Unit::TestCase + + def test_ex1 + assert_equal([1903, 1908, 1914, 1920, 1925, + 1931, 1936, 1942, 1948, 1953, + 1959, 1964, 1970, 1976, 1981, + 1987, 1992, 1998, 2004, 2009, + 2015, 2020, 2026, 2032, 2037, + 2043, 2048, 2054, 2060, 2065, + 2071, 2076, 2082, 2088, 2093, + 2099],longyear()) + end + +end diff --git a/challenge-137/roger-bell-west/ruby/ch-2.rb b/challenge-137/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..a9a359455b --- /dev/null +++ b/challenge-137/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,38 @@ +#! /usr/bin/ruby + +def lychrel(nn) + n=nn + 1.upto(100) do + m=n.to_s.reverse.to_i + if m==n then + return 0 + end + n+=m + if n>1e7 then + break + end + end + return -1 +end + +require 'test/unit' + +class TestLychrel < Test::Unit::TestCase + + def test_ex1 + assert_equal(0,lychrel(56)) + end + + def test_ex2 + assert_equal(0,lychrel(57)) + end + + def test_ex3 + assert_equal(0,lychrel(59)) + end + + def test_ex4 + assert_equal(-1,lychrel(196)) + end + +end diff --git a/challenge-137/roger-bell-west/rust/ch-1.rs b/challenge-137/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..d2ee1d76cf --- /dev/null +++ b/challenge-137/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,22 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(longyear(),vec![1903, 1908, 1914, 1920, 1925, + 1931, 1936, 1942, 1948, 1953, + 1959, 1964, 1970, 1976, 1981, + 1987, 1992, 1998, 2004, 2009, + 2015, 2020, 2026, 2032, 2037, + 2043, 2048, 2054, 2060, 2065, + 2071, 2076, 2082, 2088, 2093, + 2099]); +} + +fn p(y: i32) -> i32 { + (y+y/4-y/100+y/400) % 7 +} + +fn longyear() -> Vec<i32> { + (1900..=2100).into_iter().filter(|y| p(*y-1)==3 || p(*y)==4).collect() +} diff --git a/challenge-137/roger-bell-west/rust/ch-2.rs b/challenge-137/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..8df1cd15bb --- /dev/null +++ b/challenge-137/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,37 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(lychrel(56),0); +} + +#[test] +fn test_ex2() { + assert_eq!(lychrel(57),0); +} + +#[test] +fn test_ex3() { + assert_eq!(lychrel(59),0); +} + +#[test] +fn test_ex4() { + assert_eq!(lychrel(196),-1); +} + +fn lychrel(nn: i32) -> i32 { + let mut n=nn; + for _ in 1..100 { + let m=n.to_string().chars().rev().collect::<String>().parse::<i32>().unwrap(); + if m==n { + return 0; + } + n+=m; + if n > 10000000 { + break; + } + } + -1 +} |
