From 6997cbd3aff33c517ec03a8a074f9702845dd567 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 2 Nov 2021 15:17:39 +0000 Subject: Add Perl solution to challenge 137 --- challenge-137/paulo-custodio/Makefile | 2 + challenge-137/paulo-custodio/perl/ch-1.pl | 45 +++++++++++++++++++++ challenge-137/paulo-custodio/perl/ch-2.pl | 64 ++++++++++++++++++++++++++++++ challenge-137/paulo-custodio/t/test-1.yaml | 13 ++++++ challenge-137/paulo-custodio/t/test-2.yaml | 20 ++++++++++ 5 files changed, 144 insertions(+) create mode 100644 challenge-137/paulo-custodio/Makefile create mode 100644 challenge-137/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-137/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-137/paulo-custodio/t/test-1.yaml create mode 100644 challenge-137/paulo-custodio/t/test-2.yaml diff --git a/challenge-137/paulo-custodio/Makefile b/challenge-137/paulo-custodio/Makefile new file mode 100644 index 0000000000..6316089eb8 --- /dev/null +++ b/challenge-137/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-137/paulo-custodio/perl/ch-1.pl b/challenge-137/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..f4aa2a0fa3 --- /dev/null +++ b/challenge-137/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +# 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. +# +# +# [UPDATED][2021-11-01 16:20:00]: For more information about Long Year, please +# refer to wikipedia. +# +# 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 + +# https://en.wikipedia.org/wiki/ISO_week_date#Weeks_per_year +# years in which 1 January or 31 December are Thursdays + +use Modern::Perl; +use DateTime; + +sub is_long_year { + my($year) = @_; + my $dt = DateTime->new(year => $year, month => 1, day => 1); + return 1 if $dt->dow == 4; + $dt = DateTime->new(year => $year, month => 12, day => 31); + return 1 if $dt->dow == 4; + return 0; +} + +my @years = grep {is_long_year($_)} 1900..2100; + +# output in 5 columns +while (@years) { + my @col = splice(@years, 0, 5); + say join(", ", @col), @years ? "," : ""; +} diff --git a/challenge-137/paulo-custodio/perl/ch-2.pl b/challenge-137/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..3d32a9d628 --- /dev/null +++ b/challenge-137/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/usr/bin/env perl + +# 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. +# +# [UPDATED][2021-11-01 16:20:00]: If you stop because of any of the above two +# rules then we expect 1 as an output. +# +# 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 + +use Modern::Perl; + +use constant { + MAX_ITER => 500, + MAX_NUM => 10_000_000, +}; + +sub is_lychrel { + my($n) = @_; + for (1 .. MAX_ITER) { + last if $n > MAX_NUM; + my $rev_n = 0+reverse($n); + return 0 if $n == $rev_n; + $n += $rev_n; + } + return 1; +} + +say is_lychrel(shift||0); + + diff --git a/challenge-137/paulo-custodio/t/test-1.yaml b/challenge-137/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..3a1860050b --- /dev/null +++ b/challenge-137/paulo-custodio/t/test-1.yaml @@ -0,0 +1,13 @@ +- setup: + cleanup: + args: + input: + 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 diff --git a/challenge-137/paulo-custodio/t/test-2.yaml b/challenge-137/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..08ebb50bdd --- /dev/null +++ b/challenge-137/paulo-custodio/t/test-2.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 56 + input: + output: 0 +- setup: + cleanup: + args: 57 + input: + output: 0 +- setup: + cleanup: + args: 59 + input: + output: 0 +- setup: + cleanup: + args: 196 + input: + output: 1 -- cgit