From de28a61278504b797143899c84b5a4b568409455 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 1 Nov 2021 09:19:35 +0000 Subject: Task 1 & 2 --- challenge-137/perlboy1967/perl/ch-1.pl | 30 +++++++++++++ challenge-137/perlboy1967/perl/ch-2.pl | 82 ++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100755 challenge-137/perlboy1967/perl/ch-1.pl create mode 100755 challenge-137/perlboy1967/perl/ch-2.pl diff --git a/challenge-137/perlboy1967/perl/ch-1.pl b/challenge-137/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..97960c7091 --- /dev/null +++ b/challenge-137/perlboy1967/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/bin/perl + +=pod + +Perl Weekly Challenge - 137 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-137/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +TASK #1 › Long Year +Submitted by: Mohammad S Anwar + +Write a script to find all the years between 1900 and 2100 which is a Long Year. + + || A year is Long if it has 53 weeks. + +=cut + +use v5.16; +use strict; +use warnings; + +use Date::Calc qw(Week_of_Year); + +my @w53years; +foreach my $y (1900 .. 2099) { + push(@w53years,$y) if Week_of_Year($y,12,31) == 53; +} + +printf "Years: %s\n", join(', ', @w53years); diff --git a/challenge-137/perlboy1967/perl/ch-2.pl b/challenge-137/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..acfe110199 --- /dev/null +++ b/challenge-137/perlboy1967/perl/ch-2.pl @@ -0,0 +1,82 @@ +#!/bin/perl + +=pod + +Perl Weekly Challenge - 137 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-137/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +TASK #2 › Lychrel Number +Submitted by: Mohammad S Anwar + +You are given a number, 10 <= $n <= 1000. + +Write a script to find out if the given number is Lychrel number. +To keep the task simple, we impose the following rules: + +a. Stop if the number of iterations reached 500. +b. Stop if you end up with number >= 10_000_000. + +According to wikipedia: + + || A Lychrel number is a natural number that cannot form a palindrome through + || the iterative process of repeatedly reversing its digits and adding the + || resulting numbers. + +=cut + +use v5.16; +use strict; +use warnings; + +use constant MAX_ITER => 500; +use constant MAX_VALUE => 10_000_000; + +use Test::More; + +# Prototype(s) +sub isLychrelNumber($$$;$); + + +my $N = shift // 89; + +isLychrelNumber($N,MAX_ITER,MAX_VALUE,1); + +my %tests = qw(56 0 57 0 59 0 89 -1); +foreach my $n (keys %tests) { + is(isLychrelNumber($n,MAX_ITER,MAX_VALUE),$tests{$n}); +} +done_testing(); + + +sub isLychrelNumber($$$;$) { + my ($n, $maxIter, $maxVal, $print) = @_; + $print //= 0; + + my $return = -1; + + while (1) { + my $nRev = reverse $n; + print "$n + $nRev = " if $print; + $n += $nRev; + print "$n\n" if $print; + + # Max value exceeded? + last if ($n > $maxVal); + + # Palindrome (using a recursive regular expression)? + if ($n =~ /^((.)(?1)\2|.?)$/) { + $return = 0; + last; + } + + # Max iterations exceeded? + $maxIter--; + last if ($maxIter < 0); + } + + printf "%d => %d\n", $n, $return if $print; + + $return; +} -- cgit From 63020343f2a74a8e91040305e955467532af60c8 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 1 Nov 2021 09:53:01 +0000 Subject: Challenge 137 Solutions (Raku) --- challenge-137/mark-anderson/raku/ch-1.raku | 14 ++++++++++++++ challenge-137/mark-anderson/raku/ch-2.raku | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 challenge-137/mark-anderson/raku/ch-1.raku create mode 100644 challenge-137/mark-anderson/raku/ch-2.raku diff --git a/challenge-137/mark-anderson/raku/ch-1.raku b/challenge-137/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..852be66221 --- /dev/null +++ b/challenge-137/mark-anderson/raku/ch-1.raku @@ -0,0 +1,14 @@ +#!/usr/bin/env raku + +my $dt = Date.new(1900, 12, 31); + +my $longs := gather while $dt.year <= 2100 +{ + take $dt.year if $dt.week-number == 53; + $dt .= later(:1years); +} + +say $longs.join(', ') + .comb(6) + .rotor(5, :partial) + .join("\n"); diff --git a/challenge-137/mark-anderson/raku/ch-2.raku b/challenge-137/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..352f4fc9e1 --- /dev/null +++ b/challenge-137/mark-anderson/raku/ch-2.raku @@ -0,0 +1,23 @@ +#!/usr/bin/env raku + +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, 'Smallest Lychrel'; + +sub lychrel($n is copy where 10 <= $n <= 1000) +{ + loop + { + my $sum = $n + $n.flip; + + return 0 if $sum eq $sum.flip; + return 1 if ++$ == 500; + return 1 if $sum >= 10e6; + + $n = $sum; + } +} -- cgit From 19d8d0e1f460dcd52692cb7f53434c7c54443f69 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 1 Nov 2021 10:17:19 +0000 Subject: Challenge 137 Solutions (Raku) --- challenge-137/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-137/mark-anderson/raku/ch-2.raku b/challenge-137/mark-anderson/raku/ch-2.raku index 352f4fc9e1..bd0ef51825 100644 --- a/challenge-137/mark-anderson/raku/ch-2.raku +++ b/challenge-137/mark-anderson/raku/ch-2.raku @@ -14,7 +14,7 @@ sub lychrel($n is copy where 10 <= $n <= 1000) { my $sum = $n + $n.flip; - return 0 if $sum eq $sum.flip; + return 0 if $sum == $sum.flip; return 1 if ++$ == 500; return 1 if $sum >= 10e6; -- cgit From 6108f2cbaf3b6738f7a16a58a64ef1cc7a7899c8 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 1 Nov 2021 11:18:42 +0100 Subject: Task 1 done --- challenge-137/luca-ferrari/raku/ch-1.p6 | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100755 challenge-137/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-137/luca-ferrari/raku/ch-1.p6 b/challenge-137/luca-ferrari/raku/ch-1.p6 new file mode 100755 index 0000000000..1c1d8d9166 --- /dev/null +++ b/challenge-137/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,7 @@ +#!raku + +sub MAIN() { + for 1900 .. 2100 -> $year { + $year.say if Date.new( '%04d-12-31'.sprintf( $year ) ).week-number == 53; + } +} -- cgit From c22ce4b14b260b7acd1f536c6bfdb4ebf3e42dbb Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 1 Nov 2021 11:26:07 +0100 Subject: Task 2 done --- challenge-137/luca-ferrari/raku/ch-2.p6 | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100755 challenge-137/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-137/luca-ferrari/raku/ch-2.p6 b/challenge-137/luca-ferrari/raku/ch-2.p6 new file mode 100755 index 0000000000..3a67328fad --- /dev/null +++ b/challenge-137/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,18 @@ +#!raku + +sub MAIN( Int $n where { 10 <= $n <= 10000 }, Bool :$verbose = False ) { + + my ( $result, $iteration ) = $n,0; + while ( $result < 10_000_000 && $iteration < 500 ) { + $iteration++; + $result += $result.split( '' ).reverse.join; + if $result == $result.split( '' ).reverse.join { + '0'.say; + "Found $result after $iteration iterations".say if $verbose; + exit; + } + } + + '1'.say; + "Cannot find Lychrel number for $n".say if $verbose; +} -- cgit From 9033acb1a25d205c9fe4b3c0b0efede574a86416 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 1 Nov 2021 11:38:21 +0100 Subject: Solutions in PostgreSQL --- challenge-137/luca-ferrari/postgresql/ch-1.sql | 61 ++++++++++++++++++++++++++ challenge-137/luca-ferrari/postgresql/ch-2.sql | 36 +++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 challenge-137/luca-ferrari/postgresql/ch-1.sql create mode 100644 challenge-137/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-137/luca-ferrari/postgresql/ch-1.sql b/challenge-137/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..798e9070fb --- /dev/null +++ b/challenge-137/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,61 @@ +/* +testdb=> select * from f_long_year(); +f_long_year +------------- +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 +(36 rows) + + +*/ +CREATE OR REPLACE FUNCTION +f_long_year() +RETURNS SETOF int +AS $CODE$ +DECLARE + current_year int; +BEGIN + FOR current_year IN 1900 .. 2100 LOOP + IF date_part( 'week', make_Date( current_year, 12, 31 ) ) = 53 THEN + RETURN NEXT current_year; + END IF; + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-137/luca-ferrari/postgresql/ch-2.sql b/challenge-137/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..ed96d27b8f --- /dev/null +++ b/challenge-137/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,36 @@ +/* +testdb=> select * from f_lychrel( 59, true ); +INFO: Found 1111 after 3 iterations +f_lychrel +----------- +0 +*/ +CREATE OR REPLACE FUNCTION +f_lychrel( n int, verb boolean default false ) +RETURNS smallint +AS $CODE$ +DECLARE + result bigint := n; + iteration int := 0; +BEGIN + IF n < 10 OR n > 10000 THEN + RAISE 'n is out of bounds!'; + END IF; + + WHILE result < 10000000 AND iteration < 500 LOOP + iteration = iteration + 1; + result = result + reverse( result::text )::int; + IF result = reverse( result::text )::int THEN + IF verb THEN + RAISE INFO 'Found % after % iterations', result, iteration; + END IF; + + RETURN 0; + END IF; + + END LOOP; + + RETURN 1; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From c667b534cb80c1431f4f46d6dcb6e0ae17e12fd8 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 1 Nov 2021 10:40:38 +0000 Subject: Solutions for challenge #137 --- challenge-137/roger-bell-west/perl/ch-1.pl | 23 ++++++++++++++ challenge-137/roger-bell-west/perl/ch-2.pl | 25 +++++++++++++++ challenge-137/roger-bell-west/postscript/ch-1.ps | 30 ++++++++++++++++++ challenge-137/roger-bell-west/postscript/ch-2.ps | 40 ++++++++++++++++++++++++ challenge-137/roger-bell-west/python/ch-1.py | 23 ++++++++++++++ challenge-137/roger-bell-west/python/ch-2.py | 30 ++++++++++++++++++ challenge-137/roger-bell-west/raku/ch-1.p6 | 22 +++++++++++++ challenge-137/roger-bell-west/raku/ch-2.p6 | 24 ++++++++++++++ challenge-137/roger-bell-west/ruby/ch-1.rb | 26 +++++++++++++++ challenge-137/roger-bell-west/ruby/ch-2.rb | 38 ++++++++++++++++++++++ challenge-137/roger-bell-west/rust/ch-1.rs | 22 +++++++++++++ challenge-137/roger-bell-west/rust/ch-2.rs | 37 ++++++++++++++++++++++ 12 files changed, 340 insertions(+) create mode 100755 challenge-137/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-137/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-137/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-137/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-137/roger-bell-west/python/ch-1.py create mode 100755 challenge-137/roger-bell-west/python/ch-2.py create mode 100755 challenge-137/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-137/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-137/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-137/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-137/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-137/roger-bell-west/rust/ch-2.rs 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 { + (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::().parse::().unwrap(); + if m==n { + return 0; + } + n+=m; + if n > 10000000 { + break; + } + } + -1 +} -- cgit From 33326a1deec549ef63cce109ae6e5f4cde404ea2 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 1 Nov 2021 12:09:32 +0100 Subject: Blog references --- challenge-137/luca-ferrari/blog-1.txt | 1 + challenge-137/luca-ferrari/blog-2.txt | 1 + challenge-137/luca-ferrari/blog-3.txt | 1 + challenge-137/luca-ferrari/blog-4.txt | 1 + 4 files changed, 4 insertions(+) create mode 100644 challenge-137/luca-ferrari/blog-1.txt create mode 100644 challenge-137/luca-ferrari/blog-2.txt create mode 100644 challenge-137/luca-ferrari/blog-3.txt create mode 100644 challenge-137/luca-ferrari/blog-4.txt diff --git a/challenge-137/luca-ferrari/blog-1.txt b/challenge-137/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..063e6c16bd --- /dev/null +++ b/challenge-137/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/11/01/PerlWeeklyChallenge137.html#task1 diff --git a/challenge-137/luca-ferrari/blog-2.txt b/challenge-137/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..e036e1bd2d --- /dev/null +++ b/challenge-137/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/11/01/PerlWeeklyChallenge137.html#task2 diff --git a/challenge-137/luca-ferrari/blog-3.txt b/challenge-137/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..4385ac2151 --- /dev/null +++ b/challenge-137/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/11/01/PerlWeeklyChallenge137.html#task1pg diff --git a/challenge-137/luca-ferrari/blog-4.txt b/challenge-137/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..1a5ea48a41 --- /dev/null +++ b/challenge-137/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/11/01/PerlWeeklyChallenge137.html#task2pg -- cgit From 0d2a281d3c2d8b1ad4b86788ca17d1c40cfae316 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Tue, 2 Nov 2021 00:19:19 +1100 Subject: sgreen solutions to challenge 137 --- challenge-137/sgreen/README.md | 4 ++-- challenge-137/sgreen/blog.txt | 1 + challenge-137/sgreen/perl/ch-1.pl | 39 +++++++++++++++++++++++++++++++++++++++ challenge-137/sgreen/perl/ch-2.pl | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 challenge-137/sgreen/blog.txt create mode 100755 challenge-137/sgreen/perl/ch-1.pl create mode 100755 challenge-137/sgreen/perl/ch-2.pl diff --git a/challenge-137/sgreen/README.md b/challenge-137/sgreen/README.md index a369799dbf..413edf3cdd 100644 --- a/challenge-137/sgreen/README.md +++ b/challenge-137/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 136 +# The Weekly Challenge 137 -Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-136-12m7) +Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-137-3jon) diff --git a/challenge-137/sgreen/blog.txt b/challenge-137/sgreen/blog.txt new file mode 100644 index 0000000000..b8784a4f98 --- /dev/null +++ b/challenge-137/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-137-3jon diff --git a/challenge-137/sgreen/perl/ch-1.pl b/challenge-137/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..b834a9da94 --- /dev/null +++ b/challenge-137/sgreen/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub _is_leap_year { + my $year = shift; + return 1 if $year % 400 == 0; + return 0 if $year % 100 == 0; + return 1 if $year % 4 == 0; + return 0; +} + +sub main { + my @long_years = (); + + # January 1st 1990 was a Monday + my $day_of_week = 1; + my $year = 1900; + + while ( $year <= 2100 ) { + my $leap = _is_leap_year($year); + + # It's a long year if January 1st was a Thursday or Wednesday if + # a leap year + if ( $day_of_week == 4 or ( $leap and $day_of_week == 3 ) ) { + push @long_years, $year; + } + + # See you next year + $year++; + $day_of_week = ( $day_of_week + 1 + $leap ) % 7; + } + + say join ', ', @long_years; +} + +main(); diff --git a/challenge-137/sgreen/perl/ch-2.pl b/challenge-137/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..9d0bd40b78 --- /dev/null +++ b/challenge-137/sgreen/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +sub main { + my $n = shift; + + # Sanity check + die "You must specify a number" unless defined $n; + die "The value does not look like a number\n" unless $n =~ /^[0-9]+$/; + die "The value must be between 10 and 1,000" unless $n >= 10 and $n <= 1000; + + my $lychrel = 1; + my $count = 0; + my $i = $n; + + while ( ++$count <= 500 and $i <= 10_000_000 ) { + my $r = reverse($i); + if ( $i == $r ) { + # The number is not lychrel-al (is that a word?) + $lychrel = 0; + last; + } + + # Add the reverse to the original number, and repeat the test + $i += $r; + } + + say $lychrel; +} + +main(@ARGV); -- cgit From 685e4a02f5116bbb45eaf9697a00bb9e0226e85d Mon Sep 17 00:00:00 2001 From: boblied Date: Mon, 1 Nov 2021 10:57:26 -0500 Subject: Solve Task #1 Long Year --- challenge-137/bob-lied/README | 4 ++-- challenge-137/bob-lied/perl/ch-1.pl | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 challenge-137/bob-lied/perl/ch-1.pl diff --git a/challenge-137/bob-lied/README b/challenge-137/bob-lied/README index bbaa2cbbcb..1383134251 100644 --- a/challenge-137/bob-lied/README +++ b/challenge-137/bob-lied/README @@ -1,3 +1,3 @@ -Solutions to weekly challenge 116 by Bob Lied. +Solutions to weekly challenge 137 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-116/ +https://perlweeklychallenge.org/blog/perl-weekly-challenge-137/ diff --git a/challenge-137/bob-lied/perl/ch-1.pl b/challenge-137/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..b49897d3ef --- /dev/null +++ b/challenge-137/bob-lied/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl +#============================================================================= +# Copyright (c) 2021, Bob Lied +#============================================================================= +# Perl Weekly Challenge 137, Task 1: Long Year +# +# Write a script to find all the years between 1900 and 2100 which is a Long +# Year. A year is Long if it has 53 weeks. +# +#============================================================================= + +use strict; +use warnings; +use v5.32; + +use experimental qw/ signatures /; +no warnings "experimental::signatures"; + +use DateTime; + +my @longYearList; + +for my $year ( 1900 .. 2100 ) +{ + my $dt = DateTime->new(year => $year, month => 12, day => 31); + my ($year, $weeknum) = $dt->week(); + + push @longYearList, $year if $weeknum == 53; +} + +# Output in comma-separated groups of five. +my @row; +while ( @longYearList ) +{ + push @row, join(", ", splice(@longYearList, 0, 5) ); +} +say join(",\n", @row); -- cgit From b94859f1f2498a4c39ec9794898ccab73111ba28 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 1 Nov 2021 17:20:49 +0100 Subject: Fix reference --- challenge-133/abigail/perl/ch-2.pl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/challenge-133/abigail/perl/ch-2.pl b/challenge-133/abigail/perl/ch-2.pl index 506e4ca1e6..28e85343f5 100644 --- a/challenge-133/abigail/perl/ch-2.pl +++ b/challenge-133/abigail/perl/ch-2.pl @@ -9,6 +9,10 @@ no warnings 'syntax'; use experimental 'signatures'; use experimental 'lexical_subs'; +# +# Run as: perl ch-2.pl < input-file +# + # # We've done factorization in previous challenges, so now I'll just # use a module to give me the prime factors of a number. -- cgit From c97941ff8e30ea3fdb098a9cfdd6050a002478b6 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 1 Nov 2021 17:34:04 +0100 Subject: Tests for week 133 --- challenge-133/abigail/t/ctest.ini | 13 +++++++++++++ challenge-133/abigail/t/input-1-1 | 4 ++++ challenge-133/abigail/t/input-2-1 | 0 challenge-133/abigail/t/output-1-1.exp | 4 ++++ challenge-133/abigail/t/output-2-1 | 10 ++++++++++ 5 files changed, 31 insertions(+) create mode 100644 challenge-133/abigail/t/ctest.ini create mode 100644 challenge-133/abigail/t/input-1-1 create mode 100644 challenge-133/abigail/t/input-2-1 create mode 100644 challenge-133/abigail/t/output-1-1.exp create mode 100644 challenge-133/abigail/t/output-2-1 diff --git a/challenge-133/abigail/t/ctest.ini b/challenge-133/abigail/t/ctest.ini new file mode 100644 index 0000000000..70cc45fe36 --- /dev/null +++ b/challenge-133/abigail/t/ctest.ini @@ -0,0 +1,13 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Given Examples +2-1 = Fixed output + + +[2-1] +no_input = 1 + diff --git a/challenge-133/abigail/t/input-1-1 b/challenge-133/abigail/t/input-1-1 new file mode 100644 index 0000000000..58649d3b17 --- /dev/null +++ b/challenge-133/abigail/t/input-1-1 @@ -0,0 +1,4 @@ +10 +27 +85 +101 diff --git a/challenge-133/abigail/t/input-2-1 b/challenge-133/abigail/t/input-2-1 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-133/abigail/t/output-1-1.exp b/challenge-133/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..17d29e8c97 --- /dev/null +++ b/challenge-133/abigail/t/output-1-1.exp @@ -0,0 +1,4 @@ +3 +5 +9 +10 diff --git a/challenge-133/abigail/t/output-2-1 b/challenge-133/abigail/t/output-2-1 new file mode 100644 index 0000000000..59bee5f2c3 --- /dev/null +++ b/challenge-133/abigail/t/output-2-1 @@ -0,0 +1,10 @@ +4 +22 +27 +58 +85 +94 +121 +166 +202 +265 -- cgit From 4f367cc84f76448eb21a5bda0bb4771f1118d09f Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 1 Nov 2021 17:34:39 +0100 Subject: Alternative Perl solution for week 133, part 1 --- challenge-133/abigail/README.md | 6 ++++++ challenge-133/abigail/perl/ch-1a.pl | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 challenge-133/abigail/perl/ch-1a.pl diff --git a/challenge-133/abigail/README.md b/challenge-133/abigail/README.md index d51d3d73e2..bbf706a1f5 100644 --- a/challenge-133/abigail/README.md +++ b/challenge-133/abigail/README.md @@ -2,8 +2,14 @@ ## Part 1 +### "No buildin function at all" + * [Perl](perl/ch-1.pl) +### "No buildin sqrt function" + +* [Perl][perl/ch-1a.pl) + ## Part 2 * [C](c/ch-2.c) diff --git a/challenge-133/abigail/perl/ch-1a.pl b/challenge-133/abigail/perl/ch-1a.pl new file mode 100644 index 0000000000..dfe877ae0f --- /dev/null +++ b/challenge-133/abigail/perl/ch-1a.pl @@ -0,0 +1,36 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1a.pl < input-file +# + +# +# Unlike ch-1.pl, this is a solution which only avoids sqrt (and int for +# that matter) +# +# Given that: +# +# * sqrt (a) == a^(1/2) +# * a^b == exp (log (a^b)), a > 0 +# * log (a^b) == b * log (a), a > 0 +# +# We can write sqrt (N) as exp (log (N) / 2) +# +# We avoid using int() by just stripping the decimal point and +# anything following it using a regular expression. +# + +say (exp (log ($_) / 2) =~ s/\..*//r) while <>; -- cgit From 7110482dec6231637d1cc92b636d4e409729db3b Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 1 Nov 2021 17:56:43 +0100 Subject: Perl solution for week 137, part 1 --- challenge-137/abigail/perl/ch-1.pl | 67 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 challenge-137/abigail/perl/ch-1.pl diff --git a/challenge-137/abigail/perl/ch-1.pl b/challenge-137/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..8c788fe38a --- /dev/null +++ b/challenge-137/abigail/perl/ch-1.pl @@ -0,0 +1,67 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# + +# +# The Gregorian calender cycles every 400 years. That is, every calender +# event, or sequence of calender events, repeats after 400 years. +# (This is because there are 97 leap days in a 400 year period, which +# means there are 146097 days in such a period, and 146097 days are +# exactly 20871 weeks). +# + +# +# In such a 400 year period, there are 71 long years. A long year +# starts or ends with a Thursday (a non-leap long year starts and +# ends with with a Thursday, a leap long year either starts with +# a Wednesday and ends with a Thursday, or starts with a Thursday, +# and end with a Friday). +# + +# +# Now, we could calculate the day of the week of Jan 1 and Dec 31, +# either using a module or some handrolled calculations, checking +# whether at least of those days is a Thursday. +# +# Or we could just list the 71 years in a 400 year period, and so +# some trivial arithmetic. +# +# Guess which method we're using. +# + +my @long_year_offsets = qw [ + 004 009 015 020 026 + 032 037 043 048 054 + 060 065 071 076 082 + 088 093 099 + 105 111 116 122 + 128 133 139 144 150 + 156 161 167 172 178 + 184 189 195 + 201 207 212 218 + 224 229 235 240 246 + 252 257 263 268 274 + 280 285 291 296 + 303 308 314 + 320 325 331 336 342 + 348 353 359 364 370 + 376 381 387 392 398 +]; + +say for grep {1900 <= $_ <= 2100} + map {my $fy = $_; map {$_ + $fy} @long_year_offsets} 1600, 2000; -- cgit From d1b5c227152dc307c2f07bed723e12acfd9fe71c Mon Sep 17 00:00:00 2001 From: boblied Date: Mon, 1 Nov 2021 12:23:05 -0500 Subject: Solve W137 Task 2. --- challenge-137/bob-lied/perl/ch-2.pl | 89 +++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 challenge-137/bob-lied/perl/ch-2.pl diff --git a/challenge-137/bob-lied/perl/ch-2.pl b/challenge-137/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..448f54df01 --- /dev/null +++ b/challenge-137/bob-lied/perl/ch-2.pl @@ -0,0 +1,89 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl +#============================================================================= +# Copyright (c) 2021, Bob Lied +#============================================================================= +# Perl Weekly Challenge week 137, Task #2, Lychrel number +# +# You are given a number, 10 <= $n <= 1000. +# Write a script to find out if the given number is Lychrel number. To keep +# the task simple, we impose the following rules: +# a. Stop if the number of iterations reached 500. +# b. Stop if you end up with number >= 10_000_000. +# +# According to wikipedia: +# A Lychrel number is a natural number that cannot form a palindrome through +# the iterative process of repeatedly reversing its digits and adding the +# resulting numbers. +# +# Example 1: Input $n = 56, Output = 0 +# 56+65 = 121, found palindrome after 1 iteration +# +# Example 2: Input $n = 57, Output = 0 +# 57+75 = 132 +# 132+231 = 363, found palindrome after 2 iterations +#============================================================================= + +use strict; +use warnings; +use v5.32; + +use experimental qw/ signatures /; +no warnings "experimental::signatures"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +my $MaxIter = 500; +my $MaxSum = 10_000_000; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub isPalindrome($n) +{ + return 1 if $n < 10; + my @dig = split("", $n); + while ( @dig && $dig[0] == $dig[-1] ) + { + @dig = @dig[ 1 .. $#dig-1 ]; + } + return @dig == 0; +} + +sub isLychrel($n) +{ + my $iter = 0; + my $sum = $n; + while ( $iter < $MaxIter && $sum < $MaxSum ) + { + my $revn = join("", reverse split(//, $n)); + $sum = $n + $revn; + say "$iter: $n + $revn = $sum" if $Verbose; + return 0 if isPalindrome($sum); + $n = $sum; + } + return 1; +} + +sub runTest +{ + use Test::More; + + is( isPalindrome( 1), 1, "P=1"); + is( isPalindrome( 22), 1, "P=22"); + is( isPalindrome( 27), '', "P=27"); + is( isPalindrome( 343), 1, "P=343"); + is( isPalindrome( 346), '', "P=346"); + + is( isLychrel( 56), 0, "N=56"); + is( isLychrel( 57), 0, "N=57"); + is( isLychrel( 59), 0, "N=59"); + is( isLychrel(196), 1, "N=196"); + + done_testing; +} + -- cgit From 2d8675b32c102417490b9e3aacdda9ff342da10d Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 1 Nov 2021 17:27:25 +0000 Subject: added solutions to 137 --- challenge-137/james-smith/perl/ch-1.pl | 22 ++++++++++++++ challenge-137/james-smith/perl/ch-2.pl | 54 ++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 challenge-137/james-smith/perl/ch-1.pl create mode 100644 challenge-137/james-smith/perl/ch-2.pl diff --git a/challenge-137/james-smith/perl/ch-1.pl b/challenge-137/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..4ab92a0091 --- /dev/null +++ b/challenge-137/james-smith/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my $start_day = 1; ## Jan 1st 1900 was a Monday. + +foreach my $year ( 1900 .. 2100 ) { + my $is_leap_year = ! $year % 4 && ( $year % 100 || ! $year % 400 ); + say $year if $start_day == 4 || $start_day == 3 && $is_leap_year; + $start_day = ( $start_day + 1 + $is_leap_year ) % 7; +} + +## Compute if leap year? +## Long year if starts on Thursday (day 4) or Wednesday (day 3) in a leap year +## Next year starts on the next day of the week Mon->Tues .... +## except in a leap year when it moves 2 Mon->Wed diff --git a/challenge-137/james-smith/perl/ch-2.pl b/challenge-137/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..8951d7c972 --- /dev/null +++ b/challenge-137/james-smith/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my $MAX = 1e9; +my $MAX_LARGE = 40; +my $COUNT = 500; +my @TESTS = ( + [ 56, 0 ], + [ 57, 0 ], + [ 59, 0 ], + [196, 1 ], +); + +is( lychrel( $_->[0]), $_->[1] ) foreach @TESTS; +is( lychrel_large($_->[0]), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub lychrel { + my($n,$c) = (shift,$COUNT); + ($n eq reverse $n) ? (return 0) : ($n+= reverse $n) while $c-- && $n <= $MAX; + 1; +} + +sub lychrel_large { + my($n,$c) = (shift,$COUNT); + my @n = split //, $n; + while( $c-- && @n <= $MAX_LARGE ) { + my @r = reverse @n; + return 0 if (join '', @r) eq (join '', @n); + foreach ( reverse 0 .. @n-1 ) { + $n[$_] += $r[$_]; + next if $n[$_] < 10; + $n[$_]-=10; + $_ ? ($n[$_-1]++) : (unshift @n,1); + } + } + 1; +} + + +print "Simple:"; +print " $_" foreach grep { lychrel $_ } 10..1000; +print "\nLarge: "; +print " $_" foreach grep { lychrel_large $_ } 10..1000; +print "\n\n"; + -- cgit From 5435ed7a89c63f6ff797acd9fd3e19a3d0a1bb9e Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Mon, 1 Nov 2021 17:50:18 +0000 Subject: add solution week 137 task 1 in perl --- challenge-137/steven-wilson/perl/ch-1.pl | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 challenge-137/steven-wilson/perl/ch-1.pl diff --git a/challenge-137/steven-wilson/perl/ch-1.pl b/challenge-137/steven-wilson/perl/ch-1.pl new file mode 100644 index 0000000000..3932c0e9dd --- /dev/null +++ b/challenge-137/steven-wilson/perl/ch-1.pl @@ -0,0 +1,12 @@ +#!/usr/bin/env perl +# Week 137 Task 1 +# Long Year + +use strict; +use warnings; +use feature qw/ say /; +use DateTime; + +say join ",", grep { + DateTime->new( year => $_, month => 12, day => 31 )->week_number() == 53 +} ( 1900 .. 2100 ); -- cgit From 673f136d28443eef61239432a0ccd2d5dec70c69 Mon Sep 17 00:00:00 2001 From: Flavio Poletti Date: Mon, 1 Nov 2021 18:54:29 +0100 Subject: Add polettix's solution to challenge-137 --- challenge-137/polettix/blog.txt | 1 + challenge-137/polettix/blog1.txt | 1 + challenge-137/polettix/perl/ch-1.pl | 18 ++++++++++++++++++ challenge-137/polettix/perl/ch-2.pl | 16 ++++++++++++++++ challenge-137/polettix/raku/ch-1.raku | 15 +++++++++++++++ challenge-137/polettix/raku/ch-2.raku | 13 +++++++++++++ 6 files changed, 64 insertions(+) create mode 100644 challenge-137/polettix/blog.txt create mode 100644 challenge-137/polettix/blog1.txt create mode 100644 challenge-137/polettix/perl/ch-1.pl create mode 100644 challenge-137/polettix/perl/ch-2.pl create mode 100644 challenge-137/polettix/raku/ch-1.raku create mode 100644 challenge-137/polettix/raku/ch-2.raku diff --git a/challenge-137/polettix/blog.txt b/challenge-137/polettix/blog.txt new file mode 100644 index 0000000000..c257c6d02b --- /dev/null +++ b/challenge-137/polettix/blog.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2021/11/03/pwc137-long-year/ diff --git a/challenge-137/polettix/blog1.txt b/challenge-137/polettix/blog1.txt new file mode 100644 index 0000000000..7d8f2ccb0a --- /dev/null +++ b/challenge-137/polettix/blog1.txt @@ -0,0 +1 @@ +https://github.polettix.it/ETOOBUSY/2021/11/04/pwc137-lychrel-number/ diff --git a/challenge-137/polettix/perl/ch-1.pl b/challenge-137/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..30f78bec0b --- /dev/null +++ b/challenge-137/polettix/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; +use Time::Local 'timegm'; + +sub dow ($y, $m, $d) { (gmtime(timegm(1, 1, 1, $d, --$m, $y)))[6] } +sub is_long_year ($y) { + my $dow = dow($y, 1, 1); + return $dow == 4 || $dow == 3 && dow($y, 12, 31) == 4; +} ## end sub is_long_year ($y) + +my @longs = grep { is_long_year($_) } (1900 .. 2100); +while (@longs > 0) { + my @slice = splice @longs, 0, 5; + say join ', ', @slice, (@slice == 5 ? '' : ()); +} diff --git a/challenge-137/polettix/perl/ch-2.pl b/challenge-137/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..d987ef8d91 --- /dev/null +++ b/challenge-137/polettix/perl/ch-2.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; + +sub maybe_lychrel ($n) { + while ($n < 10_000_000) { + my $r = reverse $n; + return 0 if $n eq $r; + $n += $r; + } + return 1; +} + +say maybe_lychrel(shift || 196); diff --git a/challenge-137/polettix/raku/ch-1.raku b/challenge-137/polettix/raku/ch-1.raku new file mode 100644 index 0000000000..b89593d70d --- /dev/null +++ b/challenge-137/polettix/raku/ch-1.raku @@ -0,0 +1,15 @@ +#!/usr/bin/env raku +use v6; + +subset FullyGregorianYear of Int where * > 1582; +sub is-long-year (FullyGregorianYear:D $y) { + my $dow = Date.new($y, 1, 1).day-of-week; + return $dow == 4 || $dow == 3 && Date.new($y, 12, 31).day-of-week == 4; +} + +my @longs = (1900 .. 2100).grep({is-long-year($_)}); +while @longs > 0 { + my @slice = @longs.splice(0, 5); + @slice.push: '' if @slice == 5; + @slice.join(', ').put; +} diff --git a/challenge-137/polettix/raku/ch-2.raku b/challenge-137/polettix/raku/ch-2.raku new file mode 100644 index 0000000000..3eedb840bd --- /dev/null +++ b/challenge-137/polettix/raku/ch-2.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku +use v6; + +sub MAIN (Int:D $x where 10 <= * <= 1000 = 196) { maybe-lychrel($x).put } + +sub maybe-lychrel (Int:D $n is copy where * > 0) { + while $n < 10000000 { + my $r = $n.flip; + return 0 if $n eq $r; + $n += $r; + } + return 1; +} -- cgit From 4cb0f7b8bc788572a1ff715a58e88ad2635d3a9d Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 1 Nov 2021 18:33:10 +0100 Subject: Test all numbers between 10 and 1000 --- challenge-137/abigail/t/ctest.ini | 13 + challenge-137/abigail/t/input-1-1 | 0 challenge-137/abigail/t/input-2-1 | 3 + challenge-137/abigail/t/input-2-2 | 991 +++++++++++++++++++++++++++++++++ challenge-137/abigail/t/output-1-1.exp | 36 ++ challenge-137/abigail/t/output-2-1.exp | 3 + challenge-137/abigail/t/output-2-2.exp | 991 +++++++++++++++++++++++++++++++++ 7 files changed, 2037 insertions(+) create mode 100644 challenge-137/abigail/t/ctest.ini create mode 100644 challenge-137/abigail/t/input-1-1 create mode 100644 challenge-137/abigail/t/input-2-1 create mode 100644 challenge-137/abigail/t/input-2-2 create mode 100644 challenge-137/abigail/t/output-1-1.exp create mode 100644 challenge-137/abigail/t/output-2-1.exp create mode 100644 challenge-137/abigail/t/output-2-2.exp diff --git a/challenge-137/abigail/t/ctest.ini b/challenge-137/abigail/t/ctest.ini new file mode 100644 index 0000000000..4a58fc7705 --- /dev/null +++ b/challenge-137/abigail/t/ctest.ini @@ -0,0 +1,13 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Fixed output +2-1 = Given examples +2-2 = Full input range + + +[1-1] +no_input = 1 diff --git a/challenge-137/abigail/t/input-1-1 b/challenge-137/abigail/t/input-1-1 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-137/abigail/t/input-2-1 b/challenge-137/abigail/t/input-2-1 new file mode 100644 index 0000000000..b44d4f58a0 --- /dev/null +++ b/challenge-137/abigail/t/input-2-1 @@ -0,0 +1,3 @@ +56 +57 +59 diff --git a/challenge-137/abigail/t/input-2-2 b/challenge-137/abigail/t/input-2-2 new file mode 100644 index 0000000000..0b5e4ce92b --- /dev/null +++ b/challenge-137/abigail/t/input-2-2 @@ -0,0 +1,991 @@ +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 +49 +50 +51 +52 +53 +54 +55 +56 +57 +58 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68 +69 +70 +71 +72 +73 +74 +75 +76 +77 +78 +79 +80 +81 +82 +83 +84 +85 +86 +87 +88 +89 +90 +91 +92 +93 +94 +95 +96 +97 +98 +99 +100 +101 +102 +103 +104 +105 +106 +107 +108 +109 +110 +111 +112 +113 +114 +115 +116 +117 +118 +119 +120 +121 +122 +123 +124 +125 +126 +127 +128 +129 +130 +131 +132 +133 +134 +135 +136 +137 +138 +139 +140 +141 +142 +143 +144 +145 +146 +147 +148 +149 +150 +151 +152 +153 +154 +155 +156 +157 +158 +159 +160 +161 +162 +163 +164 +165 +166 +167 +168 +169 +170 +171 +172 +173 +174 +175 +176 +177 +178 +179 +180 +181 +182 +183 +184 +185 +186 +187 +188 +189 +190 +191 +192 +193 +194 +195 +196 +197 +198 +199 +200 +201 +202 +203 +204 +205 +206 +207 +208 +209 +210 +211 +212 +213 +214 +215 +216 +217 +218 +219 +220 +221 +222 +223 +224 +225 +226 +227 +228 +229 +230 +231 +232 +233 +234 +235 +236 +237 +238 +239 +240 +241 +242 +243 +244 +245 +246 +247 +248 +249 +250 +251 +252 +253 +254 +255 +256 +257 +258 +259 +260 +261 +262 +263 +264 +265 +266 +267 +268 +269 +270 +271 +272 +273 +274 +275 +276 +277 +278 +279 +280 +281 +282 +283 +284 +285 +286 +287 +288 +289 +290 +291 +292 +293 +294 +295 +296 +297 +298 +299 +300 +301 +302 +303 +304 +305 +306 +307 +308 +309 +310 +311 +312 +313 +314 +315 +316 +317 +318 +319 +320 +321 +322 +323 +324 +325 +326 +327 +328 +329 +330 +331 +332 +333 +334 +335 +336 +337 +338 +339 +340 +341 +342 +343 +344 +345 +346 +347 +348 +349 +350 +351 +352 +353 +354 +355 +356 +357 +358 +359 +360 +361 +362 +363 +364 +365 +366 +367 +368 +369 +370 +371 +372 +373 +374 +375 +376 +377 +378 +379 +380 +381 +382 +383 +384 +385 +386 +387 +388 +389 +390 +391 +392 +393 +394 +395 +396 +397 +398 +399 +400 +401 +402 +403 +404 +405 +406 +407 +408 +409 +410 +411 +412 +413 +414 +415 +416 +417 +418 +419 +420 +421 +422 +423 +424 +425 +426 +427 +428 +429 +430 +431 +432 +433 +434 +435 +436 +437 +438 +439 +440 +441 +442 +443 +444 +445 +446 +447 +448 +449 +450 +451 +452 +453 +454 +455 +456 +457 +458 +459 +460 +461 +462 +463 +464 +465 +466 +467 +468 +469 +470 +471 +472 +473 +474 +475 +476 +477 +478 +479 +480 +481 +482 +483 +484 +485 +486 +487 +488 +489 +490 +491 +492 +493 +494 +495 +496 +497 +498 +499 +500 +501 +502 +503 +504 +505 +506 +507 +508 +509 +510 +511 +512 +513 +514 +515 +516 +517 +518 +519 +520 +521 +522 +523 +524 +525 +526 +527 +528 +529 +530 +531 +532 +533 +534 +535 +536 +537 +538 +539 +540 +541 +542 +543 +544 +545 +546 +547 +548 +549 +550 +551 +552 +553 +554 +555 +556 +557 +558 +559 +560 +561 +562 +563 +564 +565 +566 +567 +568 +569 +570 +571 +572 +573 +574 +575 +576 +577 +578 +579 +580 +581 +582 +583 +584 +585 +586 +587 +588 +589 +590 +591 +592 +593 +594 +595 +596 +597 +598 +599 +600 +601 +602 +603 +604 +605 +606 +607 +608 +609 +610 +611 +612 +613 +614 +615 +616 +617 +618 +619 +620 +621 +622 +623 +624 +625 +626 +627 +628 +629 +630 +631 +632 +633 +634 +635 +636 +637 +638 +639 +640 +641 +642 +643 +644 +645 +646 +647 +648 +649 +650 +651 +652 +653 +654 +655 +656 +657 +658 +659 +660 +661 +662 +663 +664 +665 +666 +667 +668 +669 +670 +671 +672 +673 +674 +675 +676 +677 +678 +679 +680 +681 +682 +683 +684 +685 +686 +687 +688 +689 +690 +691 +692 +693 +694 +695 +696 +697 +698 +699 +700 +701 +702 +703 +704 +705 +706 +707 +708 +709 +710 +711 +712 +713 +714 +715 +716 +717 +718 +719 +720 +721 +722 +723 +724 +725 +726 +727 +728 +729 +730 +731 +732 +733 +734 +735 +736 +737 +738 +739 +740 +741 +742 +743 +744 +745 +746 +747 +748 +749 +750 +751 +752 +753 +754 +755 +756 +757 +758 +759 +760 +761 +762 +763 +764 +765 +766 +767 +768 +769 +770 +771 +772 +773 +774 +775 +776 +777 +778 +779 +780 +781 +782 +783 +784 +785 +786 +787 +788 +789 +790 +791 +792 +793 +794 +795 +796 +797 +798 +799 +800 +801 +802 +803 +804 +805 +806 +807 +808 +809 +810 +811 +812 +813 +814 +815 +816 +817 +818 +819 +820 +821 +822 +823 +824 +825 +826 +827 +828 +829 +830 +831 +832 +833 +834 +835 +836 +837 +838 +839 +840 +841 +842 +843 +844 +845 +846 +847 +848 +849 +850 +851 +852 +853 +854 +855 +856 +857 +858 +859 +860 +861 +862 +863 +864 +865 +866 +867 +868 +869 +870 +871 +872 +873 +874 +875 +876 +877 +878 +879 +880 +881 +882 +883 +884 +885 +886 +887 +888 +889 +890 +891 +892 +893 +894 +895 +896 +897 +898 +899 +900 +901 +902 +903 +904 +905 +906 +907 +908 +909 +910 +911 +912 +913 +914 +915 +916 +917 +918 +919 +920 +921 +922 +923 +924 +925 +926 +927 +928 +929 +930 +931 +932 +933 +934 +935 +936 +937 +938 +939 +940 +941 +942 +943 +944 +945 +946 +947 +948 +949 +950 +951 +952 +953 +954 +955 +956 +957 +958 +959 +960 +961 +962 +963 +964 +965 +966 +967 +968 +969 +970 +971 +972 +973 +974 +975 +976 +977 +978 +979 +980 +981 +982 +983 +984 +985 +986 +987 +988 +989 +990 +991 +992 +993 +994 +995 +996 +997 +998 +999 +1000 diff --git a/challenge-137/abigail/t/output-1-1.exp b/challenge-137/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..69c7436f46 --- /dev/null +++ b/challenge-137/abigail/t/output-1-1.exp @@ -0,0 +1,36 @@ +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 diff --git a/challenge-137/abigail/t/output-2-1.exp b/challenge-137/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..bb0b1cf658 --- /dev/null +++ b/challenge-137/abigail/t/output-2-1.exp @@ -0,0 +1,3 @@ +0 +0 +0 diff --git a/challenge-137/abigail/t/output-2-2.exp b/challenge-137/abigail/t/output-2-2.exp new file mode 100644 index 0000000000..e290a9d2b0 --- /dev/null +++ b/challenge-137/abigail/t/output-2-2.exp @@ -0,0 +1,991 @@ +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +1 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +1 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +1 +1 +0 +0 +0 +0 +0 +0 +0 +0 +1 +1 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +0 +0 -- cgit From f1ea467485df825b48e023ae7efb9cc031e94e8a Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 1 Nov 2021 18:34:09 +0100 Subject: Perl solution for week 137. part 2 --- challenge-137/abigail/perl/ch-2.pl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 challenge-137/abigail/perl/ch-2.pl diff --git a/challenge-137/abigail/perl/ch-2.pl b/challenge-137/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..294272ec68 --- /dev/null +++ b/challenge-137/abigail/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl < input-file +# + +# +# Note that the challenge says we can terminate the process of iterating +# if we have had 500 iterations. We will never hit that: for all numbers +# between 10 and 1000, we either reach a palindrome or exceed 10_000_000 +# long before hitting the 500th iteration (11 iterations is the maximum +# amount of iterations we need). So, we just won't bother tracking the +# number of iterations. +# + +sub l ($n) {$n >= 10_000_000 ? 1 : $n eq reverse ($n) ? 0 : l ($n + reverse $n)} +say l s/\n//r while <>; -- cgit From 289bed506e57873ce957f89ab7c4093bb032e482 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 1 Nov 2021 19:00:06 +0100 Subject: AWK solutions for week 137 --- challenge-137/abigail/awk/ch-1.awk | 38 ++++++++++++++++++++++++++++++++++++++ challenge-137/abigail/awk/ch-2.awk | 23 +++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 challenge-137/abigail/awk/ch-1.awk create mode 100644 challenge-137/abigail/awk/ch-2.awk diff --git a/challenge-137/abigail/awk/ch-1.awk b/challenge-137/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..f0d29a5887 --- /dev/null +++ b/challenge-137/abigail/awk/ch-1.awk @@ -0,0 +1,38 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk +# + +BEGIN { + m = split ("1600 2000", start_years) + n = split ("004 009 015 020 026 " \ + "032 037 043 048 054 " \ + "060 065 071 076 082 " \ + "088 093 099 " \ + " 105 111 116 122 " \ + "128 133 139 144 150 " \ + "156 161 167 172 178 " \ + "184 189 195 " \ + " 201 207 212 218 " \ + "224 229 235 240 246 " \ + "252 257 263 268 274 " \ + "280 285 291 296 " \ + " 303 308 314 " \ + "320 325 331 336 342 " \ + "348 353 359 364 370 " \ + "376 381 387 392 398", long_year_offsets) + + for (j = 1; j <= m; j ++) { + for (i = 1; i <= n; i ++) { + year = start_years [j] + long_year_offsets [i] + if (1900 <= year && year <= 2100) { + print year + } + } + } +} diff --git a/challenge-137/abigail/awk/ch-2.awk b/challenge-137/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..281b28dc55 --- /dev/null +++ b/challenge-137/abigail/awk/ch-2.awk @@ -0,0 +1,23 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +function reverse (str) { + x = "" + for (i = length (str); i != 0; i --) { + x = x substr (str, i, 1) + } + return x +} + +function ly (n) { + return n >= 10000000 ? 1 : n == reverse(n) ? 0 : ly(n + reverse(n)) +} + +{print ly($1)} -- cgit From df1b35e96d20b0fbbe887974cafb077b0b1209d9 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 1 Nov 2021 19:36:57 +0100 Subject: Bash solutions for week 137 --- challenge-137/abigail/bash/ch-1.sh | 35 ++++++++++++++++++++++++++++++++++ challenge-137/abigail/bash/ch-2.sh | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 challenge-137/abigail/bash/ch-1.sh create mode 100644 challenge-137/abigail/bash/ch-2.sh diff --git a/challenge-137/abigail/bash/ch-1.sh b/challenge-137/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..a3b12c9266 --- /dev/null +++ b/challenge-137/abigail/bash/ch-1.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +start_years=(1600 2000) +m=${#start_years[@]} + +long_year_offsets=( 4 9 15 20 26 32 37 43 48 54 \ + 60 65 71 76 82 88 93 99 \ + 105 111 116 122 128 133 139 144 150 \ + 156 161 167 172 178 184 189 195 \ + 201 207 212 218 224 229 235 240 246 \ + 252 257 263 268 274 280 285 291 296 \ + 303 308 314 320 325 331 336 342 \ + 348 353 359 364 370 376 381 387 392 398) +n=${#long_year_offsets[@]} + + +for ((j = 0; j < m; j ++)) +do for ((i = 0; i < n; i ++)) + do year=$((${start_years[$j]} + ${long_year_offsets[$i]})) + if ((1900 <= year && year <= 2100)) + then echo $year + fi + done +done + diff --git a/challenge-137/abigail/bash/ch-2.sh b/challenge-137/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..6776acf025 --- /dev/null +++ b/challenge-137/abigail/bash/ch-2.sh @@ -0,0 +1,39 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f +shopt -s extglob + +function reverse () { + local str=$1 + local len=${#str} + local rev="" + for ((i = len - 1; i >= 0; i --)) + do rev=$rev${str:$i:1} + done + reverse=$rev +} + +function ly () { + local n=$1 + reverse $n + local rev=${reverse/#+(0)/} + if ((n >= 10000000)) + then ly=1 + elif [[ $n = $rev ]] + then ly=0 + else ly $((n + rev)) + fi +} + +while read number +do ly $number + echo $ly +done -- cgit From dd6a1bfa4793a4da537a79ccbd70acf43141509a Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 1 Nov 2021 20:16:15 +0100 Subject: C solutions for week 137 --- challenge-137/abigail/c/ch-1.c | 42 ++++++++++++++++++++++++++++++++++++++++++ challenge-137/abigail/c/ch-2.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 challenge-137/abigail/c/ch-1.c create mode 100644 challenge-137/abigail/c/ch-2.c diff --git a/challenge-137/abigail/c/ch-1.c b/challenge-137/abigail/c/ch-1.c new file mode 100644 index 0000000000..de5f7df145 --- /dev/null +++ b/challenge-137/abigail/c/ch-1.c @@ -0,0 +1,42 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +short start_years [] = {1600, 2000}; +short long_year_offsets [] = { + 4, 9, 15, 20, 26, + 32, 37, 43, 48, 54, + 60, 65, 71, 76, 82, + 88, 93, 99, + 105, 111, 116, 122, + 128, 133, 139, 144, 150, + 156, 161, 167, 172, 178, + 184, 189, 195, + 201, 207, 212, 218, + 224, 229, 235, 240, 2