diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-23 19:19:16 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-23 19:19:16 +0000 |
| commit | ec625b645f0ab4e866858980c63d938d0403af03 (patch) | |
| tree | 5bebd963c7bb7e41b6f10d09bc1b42121baf163e | |
| parent | aa80c6896292ef6ff58269c6b0e375793905c01f (diff) | |
| download | perlweeklychallenge-club-ec625b645f0ab4e866858980c63d938d0403af03.tar.gz perlweeklychallenge-club-ec625b645f0ab4e866858980c63d938d0403af03.tar.bz2 perlweeklychallenge-club-ec625b645f0ab4e866858980c63d938d0403af03.zip | |
Add Python solution to challenge 19
| -rw-r--r-- | challenge-019/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-019/paulo-custodio/input.txt | 1 | ||||
| -rw-r--r-- | challenge-019/paulo-custodio/perl/ch-1.pl | 3 | ||||
| -rw-r--r-- | challenge-019/paulo-custodio/perl/ch-2.pl | 3 | ||||
| -rw-r--r-- | challenge-019/paulo-custodio/python/ch-1.py | 22 | ||||
| -rw-r--r-- | challenge-019/paulo-custodio/python/ch-2.py | 32 | ||||
| -rw-r--r-- | challenge-019/paulo-custodio/t/test-1.yaml | 124 | ||||
| -rw-r--r-- | challenge-019/paulo-custodio/t/test-2.yaml | 12 | ||||
| -rw-r--r-- | challenge-019/paulo-custodio/test.pl | 156 |
9 files changed, 197 insertions, 158 deletions
diff --git a/challenge-019/paulo-custodio/Makefile b/challenge-019/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-019/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-019/paulo-custodio/input.txt b/challenge-019/paulo-custodio/input.txt new file mode 100644 index 0000000000..e3b0cd82f1 --- /dev/null +++ b/challenge-019/paulo-custodio/input.txt @@ -0,0 +1 @@ +In the beginning God created the heavens and the earth. Now the earth was formless and empty, darkness was over the surface of the deep, and the Spirit of God was hovering over the waters. And God said, "Let there be light," and there was light. God saw that the light was good, and he separated the light from the darkness. God called the light "day," and the darkness he called "night." And there was evening, and there was morning-the first day. diff --git a/challenge-019/paulo-custodio/perl/ch-1.pl b/challenge-019/paulo-custodio/perl/ch-1.pl index ef4be52b5b..5704e83b4a 100644 --- a/challenge-019/paulo-custodio/perl/ch-1.pl +++ b/challenge-019/paulo-custodio/perl/ch-1.pl @@ -6,7 +6,8 @@ # Write a script to display months from the year 1900 to 2019 where you find # 5 weekends i.e. 5 Friday, 5 Saturday and 5 Sunday. # -# Solution: 4 weeks are 28 days, to have 5 week-ends we need additional 3 days (29,30,31), +# Solution: 4 weeks are 28 days, to have 5 week-ends we need additional 3 days +# (29,30,31), # therefore 31st must be a Sunday use Modern::Perl; diff --git a/challenge-019/paulo-custodio/perl/ch-2.pl b/challenge-019/paulo-custodio/perl/ch-2.pl index dfe7ddaf5f..6b59d781ad 100644 --- a/challenge-019/paulo-custodio/perl/ch-2.pl +++ b/challenge-019/paulo-custodio/perl/ch-2.pl @@ -3,7 +3,8 @@ # Challenge 019 # # Task #2 -# Write a script that can wrap the given paragraph at a specified column using the greedy algorithm. +# Write a script that can wrap the given paragraph at a specified column using +# the greedy algorithm. use Modern::Perl; diff --git a/challenge-019/paulo-custodio/python/ch-1.py b/challenge-019/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..9a1105859c --- /dev/null +++ b/challenge-019/paulo-custodio/python/ch-1.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 + +# Challenge 019 +# +# Task #1 +# Write a script to display months from the year 1900 to 2019 where you find +# 5 weekends i.e. 5 Friday, 5 Saturday and 5 Sunday. +# +# Solution: 4 weeks are 28 days, to have 5 week-ends we need additional 3 days +# (29,30,31), +# therefore 31st must be a Sunday + +import datetime + +def five_weekends(year, month): + return month in [1,3,5,7,8,10,12] and \ + datetime.date(year, month, 31).isoweekday() == 7 + +for year in range(1900, 2020): + for month in range(1, 13): + if five_weekends(year, month): + print("{:04d}-{:02d}".format(year, month)) diff --git a/challenge-019/paulo-custodio/python/ch-2.py b/challenge-019/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..e36e65f379 --- /dev/null +++ b/challenge-019/paulo-custodio/python/ch-2.py @@ -0,0 +1,32 @@ +#!/usr/bin/python3 + +# Challenge 019 +# +# Task #2 +# Write a script that can wrap the given paragraph at a specified column using +# the greedy algorithm. + +import fileinput +import sys + +def read_input(): + lines = [] + for line in fileinput.input(): + lines.append(line) + return lines + +def wrap(text, column): + output = "" + pos = 0 + sep = "" + for word in (text.split()): + if pos+len(sep)+len(word) >= column: + output += "\n" + sep = "" + pos = 0 + output += sep+word + pos += len(sep)+len(word) + sep = " " + return output + +print(wrap(" ".join(read_input()), 72)) diff --git a/challenge-019/paulo-custodio/t/test-1.yaml b/challenge-019/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..30c4179163 --- /dev/null +++ b/challenge-019/paulo-custodio/t/test-1.yaml @@ -0,0 +1,124 @@ +- setup: + cleanup: + args: + input: + output: | + 1901-03 + 1902-08 + 1903-05 + 1904-01 + 1904-07 + 1905-12 + 1907-03 + 1908-05 + 1909-01 + 1909-10 + 1910-07 + 1911-12 + 1912-03 + 1913-08 + 1914-05 + 1915-01 + 1915-10 + 1916-12 + 1918-03 + 1919-08 + 1920-10 + 1921-07 + 1922-12 + 1924-08 + 1925-05 + 1926-01 + 1926-10 + 1927-07 + 1929-03 + 1930-08 + 1931-05 + 1932-01 + 1932-07 + 1933-12 + 1935-03 + 1936-05 + 1937-01 + 1937-10 + 1938-07 + 1939-12 + 1940-03 + 1941-08 + 1942-05 + 1943-01 + 1943-10 + 1944-12 + 1946-03 + 1947-08 + 1948-10 + 1949-07 + 1950-12 + 1952-08 + 1953-05 + 1954-01 + 1954-10 + 1955-07 + 1957-03 + 1958-08 + 1959-05 + 1960-01 + 1960-07 + 1961-12 + 1963-03 + 1964-05 + 1965-01 + 1965-10 + 1966-07 + 1967-12 + 1968-03 + 1969-08 + 1970-05 + 1971-01 + 1971-10 + 1972-12 + 1974-03 + 1975-08 + 1976-10 + 1977-07 + 1978-12 + 1980-08 + 1981-05 + 1982-01 + 1982-10 + 1983-07 + 1985-03 + 1986-08 + 1987-05 + 1988-01 + 1988-07 + 1989-12 + 1991-03 + 1992-05 + 1993-01 + 1993-10 + 1994-07 + 1995-12 + 1996-03 + 1997-08 + 1998-05 + 1999-01 + 1999-10 + 2000-12 + 2002-03 + 2003-08 + 2004-10 + 2005-07 + 2006-12 + 2008-08 + 2009-05 + 2010-01 + 2010-10 + 2011-07 + 2013-03 + 2014-08 + 2015-05 + 2016-01 + 2016-07 + 2017-12 + 2019-03 diff --git a/challenge-019/paulo-custodio/t/test-2.yaml b/challenge-019/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..0c927ffc38 --- /dev/null +++ b/challenge-019/paulo-custodio/t/test-2.yaml @@ -0,0 +1,12 @@ +- setup: + cleanup: + args: input.txt + input: + output: | + In the beginning God created the heavens and the earth. Now the earth + was formless and empty, darkness was over the surface of the deep, and + the Spirit of God was hovering over the waters. And God said, "Let + there be light," and there was light. God saw that the light was good, + and he separated the light from the darkness. God called the light + "day," and the darkness he called "night." And there was evening, and + there was morning-the first day. diff --git a/challenge-019/paulo-custodio/test.pl b/challenge-019/paulo-custodio/test.pl deleted file mode 100644 index 1ead292820..0000000000 --- a/challenge-019/paulo-custodio/test.pl +++ /dev/null @@ -1,156 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; -use Path::Tiny; - -is capture("perl perl/ch-1.pl"), <<END; -1901-03 -1902-08 -1903-05 -1904-01 -1904-07 -1905-12 -1907-03 -1908-05 -1909-01 -1909-10 -1910-07 -1911-12 -1912-03 -1913-08 -1914-05 -1915-01 -1915-10 -1916-12 -1918-03 -1919-08 -1920-10 -1921-07 -1922-12 -1924-08 -1925-05 -1926-01 -1926-10 -1927-07 -1929-03 -1930-08 -1931-05 -1932-01 -1932-07 -1933-12 -1935-03 -1936-05 -1937-01 -1937-10 -1938-07 -1939-12 -1940-03 -1941-08 -1942-05 -1943-01 -1943-10 -1944-12 -1946-03 -1947-08 -1948-10 -1949-07 -1950-12 -1952-08 -1953-05 -1954-01 -1954-10 -1955-07 -1957-03 -1958-08 -1959-05 -1960-01 -1960-07 -1961-12 -1963-03 -1964-05 -1965-01 -1965-10 -1966-07 -1967-12 -1968-03 -1969-08 -1970-05 -1971-01 -1971-10 -1972-12 -1974-03 -1975-08 -1976-10 -1977-07 -1978-12 -1980-08 -1981-05 -1982-01 -1982-10 -1983-07 -1985-03 -1986-08 -1987-05 -1988-01 -1988-07 -1989-12 -1991-03 -1992-05 -1993-01 -1993-10 -1994-07 -1995-12 -1996-03 -1997-08 -1998-05 -1999-01 -1999-10 -2000-12 -2002-03 -2003-08 -2004-10 -2005-07 -2006-12 -2008-08 -2009-05 -2010-01 -2010-10 -2011-07 -2013-03 -2014-08 -2015-05 -2016-01 -2016-07 -2017-12 -2019-03 -END - -path("input.txt")->spew(<<END); -In the beginning God created the heavens and the earth. Now the earth was formless and empty, -darkness was over the surface of the deep, and the Spirit of God was hovering over the waters. -And God said, "Let there be light," and there was light. God saw that the light was good, and -he separated the light from the darkness. God called the light "day," and the darkness he called "night." -And there was evening, and there was morning-the first day. -END - - -is capture("perl perl/ch-2.pl < input.txt"), <<END; -In the beginning God created the heavens and the earth. Now the earth -was formless and empty, darkness was over the surface of the deep, and -the Spirit of God was hovering over the waters. And God said, "Let -there be light," and there was light. God saw that the light was good, -and he separated the light from the darkness. God called the light -"day," and the darkness he called "night." And there was evening, and -there was morning-the first day. -END - -unlink "input.txt"; -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} |
