From 6e7630ff2ec64f32f0f17763188f469dc29bcaf1 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 18 Nov 2021 11:41:42 +0000 Subject: Add Python solution to challenge 13 --- challenge-013/paulo-custodio/Makefile | 2 ++ challenge-013/paulo-custodio/python/ch-1.py | 42 ++++++++++++++++++++++ challenge-013/paulo-custodio/python/ch-2.py | 31 ++++++++++++++++ challenge-013/paulo-custodio/t/test-1.yaml | 34 ++++++++++++++++++ challenge-013/paulo-custodio/t/test-2.yaml | 14 ++++++++ challenge-013/paulo-custodio/test.pl | 55 ----------------------------- 6 files changed, 123 insertions(+), 55 deletions(-) create mode 100644 challenge-013/paulo-custodio/Makefile create mode 100644 challenge-013/paulo-custodio/python/ch-1.py create mode 100644 challenge-013/paulo-custodio/python/ch-2.py create mode 100644 challenge-013/paulo-custodio/t/test-1.yaml create mode 100644 challenge-013/paulo-custodio/t/test-2.yaml delete mode 100644 challenge-013/paulo-custodio/test.pl diff --git a/challenge-013/paulo-custodio/Makefile b/challenge-013/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-013/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-013/paulo-custodio/python/ch-1.py b/challenge-013/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..1a9f4ee1d8 --- /dev/null +++ b/challenge-013/paulo-custodio/python/ch-1.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3 + +# Challenge 013 +# +# Challenge #1 +# Write a script to print the date of last Friday of every month of a given year. +# For example, if the given year is 2019 then it should print the following: +# +# 2019/01/25 +# 2019/02/22 +# 2019/03/29 +# 2019/04/26 +# 2019/05/31 +# 2019/06/28 +# 2019/07/26 +# 2019/08/30 +# 2019/09/27 +# 2019/10/25 +# 2019/11/29 +# 2019/12/27 + +import sys +import datetime + +def last_day_of_month(year, month): + dt = datetime.date(year, month, 28) + while dt.month==month: + dt += datetime.timedelta(days=1) + dt -= datetime.timedelta(days=1) + return dt + +def last_friday(dt): + while dt.isoweekday()!=5: + dt -= datetime.timedelta(days=1) + return dt + +def print_last_fridays(year): + for month in range(1, 13): + dt = last_friday(last_day_of_month(year, month)) + print(dt.strftime("%Y/%m/%d")) + +print_last_fridays(int(sys.argv[1])) diff --git a/challenge-013/paulo-custodio/python/ch-2.py b/challenge-013/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..1bea5c0d1c --- /dev/null +++ b/challenge-013/paulo-custodio/python/ch-2.py @@ -0,0 +1,31 @@ +#!/usr/bin/python3 + +# Challenge 013 +# +# Challenge #2 +# Write a script to demonstrate Mutually Recursive methods. Two methods are +# mutually recursive if the first method calls the second and the second calls +# first in turn. Using the mutually recursive methods, generate Hofstadter +# Female and Male sequences. +# +# F ( 0 ) = 1 ; M ( 0 ) = 0 +# F ( n ) = n - M ( F ( n - 1 ) ) , n > 0 +# M ( n ) = n - F ( M ( n - 1 ) ) , n > 0. + +import sys + +def F(n): + if n==0: + return 1 + else: + return n-M(F(n-1)) + +def M(n): + if n==0: + return 0 + else: + return n-F(M(n-1)) + +N = int(sys.argv[1]) +print("F: "+", ".join([str(F(x)) for x in range(N)])+", ...") +print("M: "+", ".join([str(M(x)) for x in range(N)])+", ...") diff --git a/challenge-013/paulo-custodio/t/test-1.yaml b/challenge-013/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..119e80d038 --- /dev/null +++ b/challenge-013/paulo-custodio/t/test-1.yaml @@ -0,0 +1,34 @@ +- setup: + cleanup: + args: 2019 + input: + output: | + 2019/01/25 + 2019/02/22 + 2019/03/29 + 2019/04/26 + 2019/05/31 + 2019/06/28 + 2019/07/26 + 2019/08/30 + 2019/09/27 + 2019/10/25 + 2019/11/29 + 2019/12/27 +- setup: + cleanup: + args: 2021 + input: + output: | + 2021/01/29 + 2021/02/26 + 2021/03/26 + 2021/04/30 + 2021/05/28 + 2021/06/25 + 2021/07/30 + 2021/08/27 + 2021/09/24 + 2021/10/29 + 2021/11/26 + 2021/12/31 diff --git a/challenge-013/paulo-custodio/t/test-2.yaml b/challenge-013/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..743cfc0c4a --- /dev/null +++ b/challenge-013/paulo-custodio/t/test-2.yaml @@ -0,0 +1,14 @@ +- setup: + cleanup: + args: 3 + input: + output: | + F: 1, 1, 2, ... + M: 0, 0, 1, ... +- setup: + cleanup: + args: 21 + input: + output: | + F: 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 11, 12, 13, ... + M: 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12, 12, ... diff --git a/challenge-013/paulo-custodio/test.pl b/challenge-013/paulo-custodio/test.pl deleted file mode 100644 index 91c463d690..0000000000 --- a/challenge-013/paulo-custodio/test.pl +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -is capture("perl perl/ch-1.pl 2019"), <