aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-03 07:02:29 +0000
committerGitHub <noreply@github.com>2021-11-03 07:02:29 +0000
commit32ecf22a49491cb28e95a79fe3976943a6b41524 (patch)
tree35a228f5bb3b81b73a98e27212e7c987c9a71bf3
parentf2e30e883ce4831af8e13a5f53f8a69602bda8b6 (diff)
parent87883da68b695f54a3bd4f108fc5c31784879dac (diff)
downloadperlweeklychallenge-club-32ecf22a49491cb28e95a79fe3976943a6b41524.tar.gz
perlweeklychallenge-club-32ecf22a49491cb28e95a79fe3976943a6b41524.tar.bz2
perlweeklychallenge-club-32ecf22a49491cb28e95a79fe3976943a6b41524.zip
Merge pull request #5152 from pauloscustodio/devel
Devel
-rw-r--r--challenge-117/paulo-custodio/Makefile2
-rw-r--r--challenge-117/paulo-custodio/python/ch-1.py33
-rw-r--r--challenge-117/paulo-custodio/python/ch-2.py57
-rwxr-xr-xchallenge-117/paulo-custodio/test.pl4
-rw-r--r--challenge-118/paulo-custodio/python/ch-2.py138
-rw-r--r--challenge-136/paulo-custodio/python/ch-2.py27
-rw-r--r--challenge-137/paulo-custodio/Makefile2
-rw-r--r--challenge-137/paulo-custodio/perl/ch-1.pl45
-rw-r--r--challenge-137/paulo-custodio/perl/ch-2.pl62
-rw-r--r--challenge-137/paulo-custodio/python/ch-1.py43
-rw-r--r--challenge-137/paulo-custodio/python/ch-2.py62
-rw-r--r--challenge-137/paulo-custodio/t/test-1.yaml13
-rw-r--r--challenge-137/paulo-custodio/t/test-2.yaml20
13 files changed, 477 insertions, 31 deletions
diff --git a/challenge-117/paulo-custodio/Makefile b/challenge-117/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-117/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-117/paulo-custodio/python/ch-1.py b/challenge-117/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..9788415993
--- /dev/null
+++ b/challenge-117/paulo-custodio/python/ch-1.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python3
+
+# Challenge 117
+#
+# TASK #1 - Missing Row
+# Submitted by: Mohammad S Anwar
+# You are given text file with rows numbered 1-15 in random order but there
+# is a catch one row in missing in the file.
+#
+# 11, Line Eleven
+# 1, Line one
+# 9, Line Nine
+# 13, Line Thirteen
+# 2, Line two
+# 6, Line Six
+# 8, Line Eight
+# 10, Line Ten
+# 7, Line Seven
+# 4, Line Four
+# 14, Line Fourteen
+# 3, Line three
+# 15, Line Fifteen
+# 5, Line Five
+# Write a script to find the missing row number.
+
+import sys
+
+rows = set(range(1, 16))
+for line in sys.stdin:
+ row = int(line.split(',')[0])
+ if row in rows:
+ rows.remove(row)
+print(",".join([str(x) for x in list(rows)]))
diff --git a/challenge-117/paulo-custodio/python/ch-2.py b/challenge-117/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..5e979b888d
--- /dev/null
+++ b/challenge-117/paulo-custodio/python/ch-2.py
@@ -0,0 +1,57 @@
+#!/usr/bin/env python3
+
+# Challenge 117
+#
+# TASK #2 - Find Possible Paths
+# Submitted by: E. Choroba
+# You are given size of a triangle.
+#
+# Write a script to find all possible paths from top to the bottom right
+# corner.
+#
+# In each step, we can either move horizontally to the right (H), or move
+# downwards to the left (L) or right (R).
+#
+# BONUS: Try if it can handle triangle of size 10 or 20.
+#
+# Example 1:
+# Input: $N = 2
+#
+# S
+# / \
+# / _ \
+# /\ /\
+# /__\ /__\ E
+#
+# Output: RR, LHR, LHLH, LLHH, RLH, LRH
+# Example 2:
+# Input: $N = 1
+#
+# S
+# / \
+# / _ \ E
+#
+# Output: R, LH
+
+import sys
+
+def get_paths(size):
+ paths = []
+
+ def find_paths(path, row, col):
+ nonlocal paths, size
+
+ if row==size and col==size: # reached end
+ paths.append(path)
+ else:
+ if row < size:
+ find_paths(path+'L', row+1, col)
+ find_paths(path+'R', row+1, col+1)
+ if col < row:
+ find_paths(path+'H', row, col+1)
+
+ find_paths('', 0, 0)
+ return paths
+
+paths = get_paths(int(sys.argv[1]))
+print(", ".join(paths))
diff --git a/challenge-117/paulo-custodio/test.pl b/challenge-117/paulo-custodio/test.pl
deleted file mode 100755
index ba6c37260b..0000000000
--- a/challenge-117/paulo-custodio/test.pl
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/usr/bin/env perl
-use Modern::Perl;
-use Test::More;
-require '../../challenge-001/paulo-custodio/test.pl';
diff --git a/challenge-118/paulo-custodio/python/ch-2.py b/challenge-118/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..1eef593754
--- /dev/null
+++ b/challenge-118/paulo-custodio/python/ch-2.py
@@ -0,0 +1,138 @@
+#!/usr/bin/env python3
+
+# Challenge 118
+#
+# TASK #2 - Adventure of Knight
+# Submitted by: Cheok-Yin Fung
+# A knight is restricted to move on an 8x8 chessboard. The knight is denoted by
+# N and its way of movement is the same as what it is defined in Chess.
+# * represents an empty square. x represents a square with treasure.
+#
+# The Knight's movement is unique. It may move two squares vertically and one
+# square horizontally, or two squares horizontally and one square vertically
+# (with both forming the shape of an L).
+#
+# There are 6 squares with treasures.
+#
+# Write a script to find the path such that Knight can capture all treasures.
+# The Knight can start from the top-left square.
+#
+# a b c d e f g h
+# 8 N * * * * * * * 8
+# 7 * * * * * * * * 7
+# 6 * * * * x * * * 6
+# 5 * * * * * * * * 5
+# 4 * * x * * * * * 4
+# 3 * x * * * * * * 3
+# 2 x x * * * * * * 2
+# 1 * x * * * * * * 1
+# a b c d e f g h
+#
+
+# https://en.m.wikipedia.org/wiki/Knight%27s_tour
+
+import copy
+import re
+import sys
+
+def check_header(line):
+ if not re.search(r"a b c d e f g h", line):
+ print("expected header")
+ sys.exit(1)
+
+solution = None
+
+class Board():
+ def __init__(self):
+ self.board = [[0 for i in range(8)] for j in range(8)]
+ self.treasures = set()
+ self.path = []
+
+ def parse(self):
+ self.board = [[0 for i in range(8)] for j in range(8)]
+ self.treasures = set()
+ self.path = []
+
+ line = input()
+ check_header(line)
+
+ for row in range(8):
+ line = input()
+ cells = line.split()
+ if cells[0] != str(8-row):
+ print("expected row "+str(y))
+ sys.exit(1)
+ cells.pop(0)
+
+ for col in range(8):
+ if cells[col] == 'N':
+ self.path.append((row,col))
+ self.board[row][col] = 1
+ elif cells[col] == 'x':
+ self.treasures.add((row,col))
+
+ line = input()
+ check_header(line)
+
+ def next_moves(self, row, col):
+ moves = []
+ for drow, dcol in [(-2, -1), (-2, +1), (+2, -1), (+2, +1), \
+ (+1, -2), (-1, -2), (+1, +2), (-1, +2)]:
+ nrow = row+drow
+ ncol = col+dcol
+ if 0 <= nrow < 8 and 0 <= ncol < 8:
+ if self.board[nrow][ncol]==0:
+ moves.append((nrow, ncol))
+ return moves
+
+ def next_possible_moves(self):
+ # get moves from last position in path
+ row, col = self.path[-1]
+ moves = self.next_moves(row, col)
+ moves_count = []
+
+ # count possible moves from each destination
+ min_count = 1000
+ for nrow, ncol in moves:
+ moves2 = self.next_moves(nrow, ncol)
+ count = len(moves2)
+ moves_count.append(count)
+ min_count = min(count, min_count)
+
+ # select moves with minimum count
+ sel_moves = []
+ for i in range(0, len(moves)):
+ if moves_count[i]==min_count:
+ sel_moves.append(moves[i])
+
+ return sel_moves
+
+ def path_str(self):
+ moves = []
+ for row, col in self.path:
+ x = chr(ord('a')+col)
+ y = str(8-row)
+ moves.append(x+y)
+ return " ".join(moves)
+
+ def solve(self):
+ global solution
+
+ if len(self.treasures)==0: # all treasures found
+ if solution is None or \
+ len(solution.path) > len(self.path):
+ solution = copy.deepcopy(self)
+ else:
+ moves = self.next_possible_moves()
+ for row, col in moves:
+ new_board = copy.deepcopy(self)
+ new_board.board[row][col] = 1
+ new_board.path.append((row, col))
+ if (row, col) in new_board.treasures:
+ new_board.treasures.remove((row, col))
+ new_board.solve()
+
+board = Board()
+board.parse()
+board.solve()
+print(solution.path_str())
diff --git a/challenge-136/paulo-custodio/python/ch-2.py b/challenge-136/paulo-custodio/python/ch-2.py
index 91506afbac..be09093b3c 100644
--- a/challenge-136/paulo-custodio/python/ch-2.py
+++ b/challenge-136/paulo-custodio/python/ch-2.py
@@ -83,30 +83,3 @@ def count_combin_sum(n, terms):
n = int(sys.argv[1])
fibs = fibonacci_upto(n)
print(count_combin_sum(n, fibs))
-
-# use Modern::Perl;
-# use Math::Fibonacci 'term';
-# use Math::Combinatorics;
-# use List::Util 'sum';
-#
-# @ARGV or die "Usage: ch-2.pl n\n";
-# my $n = shift;
-# my @fibs = fibs_upto($n);
-# say count_combin_sum($n, @fibs);
-#
-#
-# sub count_combin_sum {
-# my($n, @fibs) = @_;
-# my $count = 0;
-# for my $k (1..@fibs) {
-# my @combin = combine($k, @fibs);
-# for (@combin) {
-# my @combo = @$_;
-# if (sum(@combo) == $n) {
-# $count++;
-# }
-# }
-# }
-# return $count;
-# }
-#
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..c35929554b
--- /dev/null
+++ b/challenge-137/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/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/python/ch-1.py b/challenge-137/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..8decf87d5d
--- /dev/null
+++ b/challenge-137/paulo-custodio/python/ch-1.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+# 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
+
+import datetime
+
+def is_long_year(year):
+ dt = datetime.date(year, 1, 1)
+ if dt.isoweekday()==4:
+ return True
+ dt = datetime.date(year, 12, 31)
+ if dt.isoweekday()==4:
+ return True
+ return False
+
+years = list(filter(lambda x:is_long_year(x), range(1900, 2100+1)))
+for i in range(0, len(years), 5):
+ out = ", ".join([str(x) for x in years[i:i+5]])
+ if i+5 < len(years):
+ out += ","
+ print(out)
diff --git a/challenge-137/paulo-custodio/python/ch-2.py b/challenge-137/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..cbdf04d268
--- /dev/null
+++ b/challenge-137/paulo-custodio/python/ch-2.py
@@ -0,0 +1,62 @@
+#!/usr/bin/env python3
+
+# 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
+
+import sys
+
+MAX_ITER = 500
+MAX_NUM = 10000000
+
+def is_lychrel(n):
+ for i in range(0, MAX_ITER):
+ if n > MAX_NUM:
+ break
+ rev_n = int(str(n)[::-1])
+ if n == rev_n:
+ return False
+ n += rev_n
+ return True
+
+if is_lychrel(int(sys.argv[1])):
+ print(1)
+else:
+ print(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