From 43041c5f15754374a8ed4f5beee5d00248a04fc2 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 22 Nov 2021 15:05:28 +0000 Subject: Add Perl and Python solutions to challenge 140 --- challenge-140/paulo-custodio/perl/ch-1.pl | 46 +++++++++++++++++++++++++ challenge-140/paulo-custodio/perl/ch-2.pl | 53 +++++++++++++++++++++++++++++ challenge-140/paulo-custodio/python/ch-1.py | 38 +++++++++++++++++++++ challenge-140/paulo-custodio/python/ch-2.py | 49 ++++++++++++++++++++++++++ challenge-140/paulo-custodio/t/test-1.yaml | 15 ++++++++ challenge-140/paulo-custodio/t/test-2.yaml | 10 ++++++ 6 files changed, 211 insertions(+) create mode 100644 challenge-140/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-140/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-140/paulo-custodio/python/ch-1.py create mode 100644 challenge-140/paulo-custodio/python/ch-2.py create mode 100644 challenge-140/paulo-custodio/t/test-1.yaml create mode 100644 challenge-140/paulo-custodio/t/test-2.yaml diff --git a/challenge-140/paulo-custodio/perl/ch-1.pl b/challenge-140/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..440c1b4996 --- /dev/null +++ b/challenge-140/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl + +# TASK #1 > Add Binary +# Submitted by: Mohammad S Anwar +# You are given two decimal-coded binary numbers, $a and $b. +# +# Write a script to simulate the addition of the given binary numbers. +# +# The script should simulate something like $a + $b. (operator overloading) +# +# Example 1 +# Input: $a = 11; $b = 1; +# Output: 100 +# Example 2 +# Input: $a = 101; $b = 1; +# Output: 110 +# Example 3 +# Input: $a = 100; $b = 11; +# Output: 111 + +use Modern::Perl; + +{ + package Binary; + + sub new { + my($class, $n) = @_; + $n //= 0; + return bless \$n, $class; + } + + sub add { + my($self, $other) = @_; + my $a = oct("0b".$$self); + my $b = oct("0b".$$other); + return ref($self)->new(sprintf("%b", $a+$b)); + } + + use overload '+' => \&add; + use overload '""' => sub { my($self) = @_; return $$self; } +} + +my $a = Binary->new(shift); +my $b = Binary->new(shift); +my $c = $a+$b; +say $c; diff --git a/challenge-140/paulo-custodio/perl/ch-2.pl b/challenge-140/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..2e2ad24d83 --- /dev/null +++ b/challenge-140/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/usr/bin/perl + +# TASK #2 > Multiplication Table +# Submitted by: Mohammad S Anwar +# You are given 3 positive integers, $i, $j and $k. +# +# Write a script to print the $kth element in the sorted multiplication table of $i and $j. +# +# Example 1 +# Input: $i = 2; $j = 3; $k = 4 +# Output: 3 +# +# Since the multiplication of 2 x 3 is as below: +# +# 1 2 3 +# 2 4 6 +# +# The sorted multiplication table: +# +# 1 2 2 3 4 6 +# +# Now the 4th element in the table is "3". +# Example 2 +# Input: $i = 3; $j = 3; $k = 6 +# Output: 4 +# +# Since the multiplication of 3 x 3 is as below: +# +# 1 2 3 +# 2 4 6 +# 3 6 9 +# +# The sorted multiplication table: +# +# 1 2 2 3 3 4 6 6 9 +# +# Now the 6th element in the table is "4". + +use Modern::Perl; + +sub item { + my($i, $j, $k) = @_; + my @table; + for my $x (1..$j) { + for my $y (1..$i) { + push @table, $x*$y; + } + } + @table = sort @table; + return $table[$k-1]; +} + +say item(@ARGV); diff --git a/challenge-140/paulo-custodio/python/ch-1.py b/challenge-140/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..364d04f5a9 --- /dev/null +++ b/challenge-140/paulo-custodio/python/ch-1.py @@ -0,0 +1,38 @@ +#!/usr/bin/python3 + +# TASK #1 > Add Binary +# Submitted by: Mohammad S Anwar +# You are given two decimal-coded binary numbers, $a and $b. +# +# Write a script to simulate the addition of the given binary numbers. +# +# The script should simulate something like $a + $b. (operator overloading) +# +# Example 1 +# Input: $a = 11; $b = 1; +# Output: 100 +# Example 2 +# Input: $a = 101; $b = 1; +# Output: 110 +# Example 3 +# Input: $a = 100; $b = 11; +# Output: 111 + +import sys + +class Binary(): + def __init__(self, n): + self.n = n + + def __str__(self): + return str(self.n) + + def __add__(self, other): + a = int(str(self.n), 2) + b = int(str(other.n), 2) + return Binary(int("{:b}".format(a+b))) + +a = Binary(int(sys.argv[1])) +b = Binary(int(sys.argv[2])) +c = a+b +print(c) diff --git a/challenge-140/paulo-custodio/python/ch-2.py b/challenge-140/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..54ea2a76ba --- /dev/null +++ b/challenge-140/paulo-custodio/python/ch-2.py @@ -0,0 +1,49 @@ +#!/usr/bin/perl + +# TASK #2 > Multiplication Table +# Submitted by: Mohammad S Anwar +# You are given 3 positive integers, $i, $j and $k. +# +# Write a script to print the $kth element in the sorted multiplication table of $i and $j. +# +# Example 1 +# Input: $i = 2; $j = 3; $k = 4 +# Output: 3 +# +# Since the multiplication of 2 x 3 is as below: +# +# 1 2 3 +# 2 4 6 +# +# The sorted multiplication table: +# +# 1 2 2 3 4 6 +# +# Now the 4th element in the table is "3". +# Example 2 +# Input: $i = 3; $j = 3; $k = 6 +# Output: 4 +# +# Since the multiplication of 3 x 3 is as below: +# +# 1 2 3 +# 2 4 6 +# 3 6 9 +# +# The sorted multiplication table: +# +# 1 2 2 3 3 4 6 6 9 +# +# Now the 6th element in the table is "4". + +import sys + +def item(i, j, k): + table = [] + for x in range(1, j+1): + for y in range(1, i+1): + table.append(x*y) + table.sort() + return table[k-1] + +print(item(*[int(x) for x in sys.argv[1:4]])) diff --git a/challenge-140/paulo-custodio/t/test-1.yaml b/challenge-140/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..1324bfbeee --- /dev/null +++ b/challenge-140/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 11 1 + input: + output: 100 +- setup: + cleanup: + args: 101 1 + input: + output: 110 +- setup: + cleanup: + args: 100 11 + input: + output: 111 diff --git a/challenge-140/paulo-custodio/t/test-2.yaml b/challenge-140/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..835786110e --- /dev/null +++ b/challenge-140/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 2 3 4 + input: + output: 3 +- setup: + cleanup: + args: 3 3 6 + input: + output: 4 -- cgit From aa80c6896292ef6ff58269c6b0e375793905c01f Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 23 Nov 2021 15:14:23 +0000 Subject: Add Python solution to challenge 18 --- challenge-018/paulo-custodio/Makefile | 2 + challenge-018/paulo-custodio/python/ch-1.py | 39 ++++++++++ challenge-018/paulo-custodio/python/ch-2.py | 115 ++++++++++++++++++++++++++++ challenge-018/paulo-custodio/t/test-1.yaml | 10 +++ challenge-018/paulo-custodio/t/test-2.yaml | 24 ++++++ challenge-018/paulo-custodio/test.pl | 23 ------ 6 files changed, 190 insertions(+), 23 deletions(-) create mode 100644 challenge-018/paulo-custodio/Makefile create mode 100644 challenge-018/paulo-custodio/python/ch-1.py create mode 100644 challenge-018/paulo-custodio/python/ch-2.py create mode 100644 challenge-018/paulo-custodio/t/test-1.yaml create mode 100644 challenge-018/paulo-custodio/t/test-2.yaml delete mode 100644 challenge-018/paulo-custodio/test.pl diff --git a/challenge-018/paulo-custodio/Makefile b/challenge-018/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-018/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-018/paulo-custodio/python/ch-1.py b/challenge-018/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..2227f647ee --- /dev/null +++ b/challenge-018/paulo-custodio/python/ch-1.py @@ -0,0 +1,39 @@ +#!/usr/bin/python3 + +# Challenge 018 +# +# Task #1 +# Write a script that takes 2 or more strings as command line parameters and +# print the longest common substring. For example, the longest common substring +# of the strings "ABABC", "BABCA" and "ABCBA" is string "ABC" of length 3. +# Other common substrings are "A", "AB", "B", "BA", "BC" and "C". Please check +# this wiki page for details. + +import sys +import re + +def longest_substr(strs): + def matches_all(substr, strs): + for s in strs: + if not re.search(substr, s): + return False + return True + + longest_len = -1 + longest = set() + + for s in strs: + for start in range(0, len(s)): + for end in range(start+1, len(s)+1): + if end-start >= longest_len: + substr = s[start:end] + if substr not in longest: + if matches_all(substr, strs): + if end-start == longest_len: + longest.add(substr) + else: + longest = set([substr]) + longest_len = end-start + return sorted(list(longest)) + +print("("+", ".join(['"'+x+'"' for x in longest_substr(sys.argv[1:])])+")") diff --git a/challenge-018/paulo-custodio/python/ch-2.py b/challenge-018/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..fa8adf18c9 --- /dev/null +++ b/challenge-018/paulo-custodio/python/ch-2.py @@ -0,0 +1,115 @@ +#!/usr/bin/python3 + +# Challenge 018 +# +# Task #2 +# Write a script to implement Priority Queue. It is like regular queue except +# each element has a priority associated with it. In a priority queue, an +# element with high priority is served before an element with low priority. +# Please check this wiki page for more informations. It should serve the +# following operations: +# +# is_empty: check whether the queue has no elements. +# insert_with_priority: add an element to the queue with an associated priority. +# pull_highest_priority_element: remove the element from the queue that has the +# highest priority, and return it. If two elements have the same priority, +# then return element added first. + +# priority queue +class PQueue(): + def __init__(self): + self.q = [] + + def is_empty(self): + return len(self.q)==0 + + def insert(self, pri, elem): + if self.is_empty(): + self.q.append([pri, [elem]]) + elif pri < self.q[0][0]: + self.q.insert(0, [pri, [elem]]) + elif pri > self.q[-1][0]: + self.q.append([pri, [elem]]) + else: + for i in range(0, len(self.q)): + if self.q[i][0] == pri: + self.q[i][1].append(elem) + return + elif self.q[i][0] > pri: + self.q.insert(i, [pri, [elem]]) + return + + def pull(self): + if self.is_empty(): + return None + else: + elem = self.q[-1][1].pop(0) + if len(self.q[-1][1]) == 0: + self.q.pop(-1) + return elem + +# tests +test_num = 0 + +def ok(f, title): + global test_num + test_num += 1 + if f: + print(f"ok {test_num} - {title}") + else: + print(f"nok {test_num} - {title}") + +def eq(a, b, title): + ok(a==b, title) + if a!=b: + print("#", a, "!=", b) + +def done_testing(): + print(f"1..{test_num}") + + +# run tests +q = PQueue() + +ok(q.is_empty(), "is empty") +ok(q.pull() is None, "pull from empty queue") + +# insert same priority +q.insert(1, 123) +ok(not q.is_empty(), "is not empty") + +q.insert(1, 456) +ok(not q.is_empty(), "is not empty") + +q.insert(1, 789) +ok(not q.is_empty(), "is not empty") + +# pull +eq(q.pull(), 123, "got element") +ok(not q.is_empty(), "is not empty") + +eq(q.pull(), 456, "got element") +ok(not q.is_empty(), "is not empty") + +eq(q.pull(), 789, "got element") +ok(q.is_empty(), "is empty") + +# insert higher priority +q.insert(1, 123) +q.insert(1, 456) +q.insert(2, 23) +q.insert(3, 4) + +# insert lower priority +q.insert(0, 999) +q.insert(0, 998) + +eq(q.pull(), 4, "got element") +eq(q.pull(), 23, "got element") +eq(q.pull(), 123, "got element") +eq(q.pull(), 456, "got element") +eq(q.pull(), 999, "got element") +eq(q.pull(), 998, "got element") +ok(q.is_empty(), "is empty") + +done_testing() diff --git a/challenge-018/paulo-custodio/t/test-1.yaml b/challenge-018/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..041df7a1ad --- /dev/null +++ b/challenge-018/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: ABABC BABCA ABCBA + input: + output: ("ABC") +- setup: + cleanup: + args: ABABCBA BABCACBA ABCBCBA + input: + output: ("ABC", "CBA") diff --git a/challenge-018/paulo-custodio/t/test-2.yaml b/challenge-018/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..e99c9f130a --- /dev/null +++ b/challenge-018/paulo-custodio/t/test-2.yaml @@ -0,0 +1,24 @@ +- setup: + cleanup: + args: + input: + output: | + ok 1 - is empty + ok 2 - pull from empty queue + ok 3 - is not empty + ok 4 - is not empty + ok 5 - is not empty + ok 6 - got element + ok 7 - is not empty + ok 8 - got element + ok 9 - is not empty + ok 10 - got element + ok 11 - is empty + ok 12 - got element + ok 13 - got element + ok 14 - got element + ok 15 - got element + ok 16 - got element + ok 17 - got element + ok 18 - is empty + 1..18 diff --git a/challenge-018/paulo-custodio/test.pl b/challenge-018/paulo-custodio/test.pl deleted file mode 100644 index a60d3559d3..0000000000 --- a/challenge-018/paulo-custodio/test.pl +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -is capture("perl perl/ch-1.pl ABABC BABCA ABCBA"), < Date: Tue, 23 Nov 2021 19:19:16 +0000 Subject: Add Python solution to challenge 19 --- challenge-019/paulo-custodio/Makefile | 2 + challenge-019/paulo-custodio/input.txt | 1 + challenge-019/paulo-custodio/perl/ch-1.pl | 3 +- challenge-019/paulo-custodio/perl/ch-2.pl | 3 +- challenge-019/paulo-custodio/python/ch-1.py | 22 ++++ challenge-019/paulo-custodio/python/ch-2.py | 32 ++++++ challenge-019/paulo-custodio/t/test-1.yaml | 124 ++++++++++++++++++++++ challenge-019/paulo-custodio/t/test-2.yaml | 12 +++ challenge-019/paulo-custodio/test.pl | 156 ---------------------------- 9 files changed, 197 insertions(+), 158 deletions(-) create mode 100644 challenge-019/paulo-custodio/Makefile create mode 100644 challenge-019/paulo-custodio/input.txt create mode 100644 challenge-019/paulo-custodio/python/ch-1.py create mode 100644 challenge-019/paulo-custodio/python/ch-2.py create mode 100644 challenge-019/paulo-custodio/t/test-1.yaml create mode 100644 challenge-019/paulo-custodio/t/test-2.yaml delete mode 100644 challenge-019/paulo-custodio/test.pl 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"), <spew(<