aboutsummaryrefslogtreecommitdiff
path: root/challenge-137
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-11-08 01:01:44 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-11-08 01:01:44 +0000
commit4003e5e2bba985403f840de1e0e22362e5642c5e (patch)
treef434626ea566a5a11b970e5097fe2a819ee148a8 /challenge-137
parent5d5a9babfd03083a0c2d03816253f125c4e89e3d (diff)
downloadperlweeklychallenge-club-4003e5e2bba985403f840de1e0e22362e5642c5e.tar.gz
perlweeklychallenge-club-4003e5e2bba985403f840de1e0e22362e5642c5e.tar.bz2
perlweeklychallenge-club-4003e5e2bba985403f840de1e0e22362e5642c5e.zip
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-137')
-rwxr-xr-xchallenge-137/colin-crain/perl/ch-1.pl108
-rwxr-xr-xchallenge-137/colin-crain/perl/ch-2.pl83
-rwxr-xr-xchallenge-137/colin-crain/raku/ch-1.raku24
-rwxr-xr-xchallenge-137/colin-crain/raku/ch-2.raku44
4 files changed, 259 insertions, 0 deletions
diff --git a/challenge-137/colin-crain/perl/ch-1.pl b/challenge-137/colin-crain/perl/ch-1.pl
new file mode 100755
index 0000000000..586bda94c0
--- /dev/null
+++ b/challenge-137/colin-crain/perl/ch-1.pl
@@ -0,0 +1,108 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# .pl
+#
+# 137-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.
+#
+# Expected Output
+# 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
+#
+# a year, 365 days, divided into 7-day weeks yield 52 and 1/7 or
+# one extra day. IF it is leap year, with 366 days, is will have
+# two. So by my reckoning, every year has 53 weeks worth of days:
+# 52 plus a part or a week. So this challenge hinges on which day
+# of the week starts the year, and further, which day of the week
+# starts a given week. Two commonly accepted schemes are for a new
+# week to either start on Monday, running through Sunday, or for
+# the week to start on Sunday, running through the following
+# Saturday. That, I can see, is going to cause some
+# internationalization problems with our global community. So if we
+# start our weeks say on Sunday, then in a non-leap year where the
+# years starts on a Saturday we will roll into Sunday on December
+# 31, and the year will have 53 weeks. For any other day the year
+# will only have 52 weeks. In the case of a leap year, however,
+# then the rollover will occur with two start days, either Friday
+# or Saturday.
+#
+# In light of this sketchiness, the ISO has set up a standard that
+# the first week of the year is to be considered the week that
+# contains January 4, which is to say the first week containing at
+# least 4 days, or put another way the first week where the
+# majority of days falls within the new year. In this scheme
+# everything is shifted a few days, but the same precessional logic
+# applies.
+#
+# We could solve this mathematically, by determining the start day
+# of January 1, 1900, and precessing the day one or two days
+# depending on the arcane rules for placing leap years, noting when
+# the start dates properly align.
+#
+# But then again, that's why we have computers, to do this dreary,
+# repetitive work for us. Enter Dave Rolsky, and his DataTime module.
+#
+# What we can do, then, is choose the 28th of December in a given
+# year, and check its week number. If that value is 53, then the week has
+# 53 ISO weeks. Easy peasy.
+#
+# As this result matches the expected output, we will suppose that
+# interpretation of weeks in a year was what was being requested.
+#
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+use DateTime;
+use DateTime::Duration;
+
+my @years;
+
+my $dt = DateTime->new (
+ year => 1900 ,
+ month => 12 ,
+ day => 28 ) ;
+
+my $dur = DateTime::Duration->new (
+ years => 1 );
+
+for (1..200) {
+ if ($dt->week_number == 53) {
+ push @years, $dt->year;
+ }
+ $dt->add( $dur );
+}
+
+## output phase
+local $" = ', ';
+my @five;
+for (@years) {
+ push @five, $_;
+ if (scalar @five == 5) {
+ say "@five";
+ @five = ();
+ }
+}
+say "@five";
+
diff --git a/challenge-137/colin-crain/perl/ch-2.pl b/challenge-137/colin-crain/perl/ch-2.pl
new file mode 100755
index 0000000000..9ab0470a08
--- /dev/null
+++ b/challenge-137/colin-crain/perl/ch-2.pl
@@ -0,0 +1,83 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# lychrel.pl
+#
+# 137-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.
+#
+# Example 1
+#
+# Input: $n = 56
+# Output: 0
+#
+# After 1 iteration, we found palindrome number.
+# 56 + 65 = 121
+#
+# Example 2
+#
+# Input: $n = 57
+# Output: 0
+#
+# After 2 iterations, we found palindrome number.
+# 57 + 75 = 132
+# 132 + 231 = 363
+#
+# Example 3
+#
+# Input: $n = 59
+# Output: 0
+#
+# After 3 iterations, we found palindrome number.
+# 59 + 95 = 154
+# 154 + 451 = 605
+# 605 + 506 = 1111
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+use integer;
+
+
+for my $num ( 10..1000 ) {
+ say "input: $num";
+ my @ret = lychrel( $num );
+ say "output: ", $ret[0];
+ ref($ret[1]) eq 'ARRAY'
+ ? do {say "steps: (", scalar $ret[1]->@*, ")"; say "\t $_" for $ret[1]->@*}
+ : say $ret[1];
+ say '';
+}
+
+sub lychrel ( $num ) {
+ my @chain = ($num);
+ return (0, @chain) if $num == reverse $num;
+ for (1..500) {
+ my $revsum = $num + reverse $num;
+ push @chain, $revsum;
+ return (0, \@chain) if $revsum == reverse $revsum;
+ $num = $revsum;
+ return (1, "number too large: $num") if $num > 10_000_000;
+ }
+ say (1, "too many iterations, more than 500");
+}
+
diff --git a/challenge-137/colin-crain/raku/ch-1.raku b/challenge-137/colin-crain/raku/ch-1.raku
new file mode 100755
index 0000000000..a76126e80f
--- /dev/null
+++ b/challenge-137/colin-crain/raku/ch-1.raku
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl6
+#
+#
+# 137-1-long-cat-years.raku
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN () ;
+
+my @out = gather {
+ for 1900..2100 {
+ my $d = Date.new( $_, 12, 28 );
+ take $d.year if $d.week-number == 53;
+ }
+}
+
+.join(', ')
+.say
+ for @out.rotor(5, :partial);
diff --git a/challenge-137/colin-crain/raku/ch-2.raku b/challenge-137/colin-crain/raku/ch-2.raku
new file mode 100755
index 0000000000..b4241aa572
--- /dev/null
+++ b/challenge-137/colin-crain/raku/ch-2.raku
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl6
+#
+#
+# lychrel.raku
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+unit sub MAIN () ;
+
+constant MAX-ITER = 5000;
+constant MAX-VALUE = 10_000_000_000;
+constant ERR-LARGE = 'number too large - larger than ' ~ MAX-VALUE;
+constant ERR-MANY = 'too many iterations - more than ' ~ MAX-ITER;
+
+
+for 10..1000 -> $num {
+ my ($out, $res) = lychrel( $num );
+ say qq:to/END/;
+ Input: $num
+ Output: $out
+ $res
+ END
+}
+
+
+sub lychrel ( $num is copy ) {
+ my @chain = $num;
+ return 1, @chain if $num == $num.flip;
+ for 1..MAX-ITER {
+ my $revsum = $num + $num.flip;
+ @chain.push: $revsum;
+ return 1, @chain if $revsum == $revsum.flip;
+ $num = $revsum;
+ return 0, ERR-LARGE if $num > MAX-VALUE;
+ }
+ return 0, ERR-MANY;
+}
+
+