aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-02 08:06:10 +0000
committerGitHub <noreply@github.com>2021-11-02 08:06:10 +0000
commit65bffdfa12ba9d4c3c1fe113191aec6024d99671 (patch)
tree5c333bec5d4e6f6c10b8f6976e363b39d794be07
parent13a800e5bd399032f808cfd7934074716288c747 (diff)
parent0d2a281d3c2d8b1ad4b86788ca17d1c40cfae316 (diff)
downloadperlweeklychallenge-club-65bffdfa12ba9d4c3c1fe113191aec6024d99671.tar.gz
perlweeklychallenge-club-65bffdfa12ba9d4c3c1fe113191aec6024d99671.tar.bz2
perlweeklychallenge-club-65bffdfa12ba9d4c3c1fe113191aec6024d99671.zip
Merge pull request #5145 from simongreen-net/swg-137
sgreen solutions to challenge 137
-rw-r--r--challenge-137/sgreen/README.md4
-rw-r--r--challenge-137/sgreen/blog.txt1
-rwxr-xr-xchallenge-137/sgreen/perl/ch-1.pl39
-rwxr-xr-xchallenge-137/sgreen/perl/ch-2.pl34
4 files changed, 76 insertions, 2 deletions
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);