aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-05 21:21:18 +0000
committerGitHub <noreply@github.com>2021-11-05 21:21:18 +0000
commitea1f2d69a93d7d767bfdd4e2b6be835f3a8e0946 (patch)
treeba7a886a424b8a16d155760d8616e4f83fa7f119
parent41e1a2fb163aaec87e82a923025bb6f06762f152 (diff)
parent22e45b2c865e5dc510215516bf12ea85f9691ae8 (diff)
downloadperlweeklychallenge-club-ea1f2d69a93d7d767bfdd4e2b6be835f3a8e0946.tar.gz
perlweeklychallenge-club-ea1f2d69a93d7d767bfdd4e2b6be835f3a8e0946.tar.bz2
perlweeklychallenge-club-ea1f2d69a93d7d767bfdd4e2b6be835f3a8e0946.zip
Merge pull request #5165 from pauloscustodio/devel
Devel
-rw-r--r--challenge-106/paulo-custodio/Makefile2
-rw-r--r--challenge-106/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-106/paulo-custodio/python/ch-1.py38
-rw-r--r--challenge-106/paulo-custodio/test.pl4
-rw-r--r--challenge-107/paulo-custodio/Makefile2
-rw-r--r--challenge-107/paulo-custodio/python/ch-1.py59
-rw-r--r--challenge-107/paulo-custodio/python/ch-2.py43
-rwxr-xr-xchallenge-107/paulo-custodio/test.pl4
-rw-r--r--challenge-108/paulo-custodio/Makefile2
-rw-r--r--challenge-108/paulo-custodio/python/ch-1.py12
-rw-r--r--challenge-108/paulo-custodio/python/ch-2.py63
-rw-r--r--challenge-108/paulo-custodio/t/test-2.yaml2
-rwxr-xr-xchallenge-108/paulo-custodio/test.pl4
-rw-r--r--challenge-109/paulo-custodio/Makefile2
-rw-r--r--challenge-109/paulo-custodio/python/ch-1.py40
-rw-r--r--challenge-109/paulo-custodio/python/ch-2.py58
-rwxr-xr-xchallenge-109/paulo-custodio/test.pl4
-rw-r--r--challenge-110/paulo-custodio/Makefile2
-rw-r--r--challenge-110/paulo-custodio/python/ch-1.py39
-rw-r--r--challenge-110/paulo-custodio/python/ch-2.py47
-rwxr-xr-xchallenge-110/paulo-custodio/test.pl4
21 files changed, 411 insertions, 22 deletions
diff --git a/challenge-106/paulo-custodio/Makefile b/challenge-106/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-106/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-106/paulo-custodio/perl/ch-1.pl b/challenge-106/paulo-custodio/perl/ch-1.pl
index 90324d33ea..13c5bc7bb6 100644
--- a/challenge-106/paulo-custodio/perl/ch-1.pl
+++ b/challenge-106/paulo-custodio/perl/ch-1.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 106
#
diff --git a/challenge-106/paulo-custodio/python/ch-1.py b/challenge-106/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..f97fce6cf0
--- /dev/null
+++ b/challenge-106/paulo-custodio/python/ch-1.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+
+# Challenge 106
+#
+# TASK #1 > Maximum Gap
+# Submitted by: Mohammad S Anwar
+# You are given an array of integers @N.
+#
+# Write a script to display the maximum difference between two successive
+# elements once the array is sorted.
+#
+# If the array contains only 1 element then display 0.
+#
+# Example
+# Input: @N = (2, 9, 3, 5)
+# Output: 4
+#
+# Input: @N = (1, 3, 8, 2, 0)
+# Output: 5
+#
+# Input: @N = (5)
+# Output: 0
+
+import sys
+
+def max_gap(n):
+ if len(n)<2:
+ return 0
+ n.sort()
+
+ max_gap = 0
+ for i in range(0, len(n)-1):
+ gap = n[i+1]-n[i]
+ max_gap = max(max_gap, gap)
+
+ return max_gap
+
+print(max_gap([int(x) for x in sys.argv[1:]]))
diff --git a/challenge-106/paulo-custodio/test.pl b/challenge-106/paulo-custodio/test.pl
deleted file mode 100644
index ba6c37260b..0000000000
--- a/challenge-106/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-107/paulo-custodio/Makefile b/challenge-107/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-107/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-107/paulo-custodio/python/ch-1.py b/challenge-107/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..db2c65edd5
--- /dev/null
+++ b/challenge-107/paulo-custodio/python/ch-1.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+
+# Challenge 107
+#
+# TASK #1 - Self-descriptive Numbers
+# Submitted by: Mohammad S Anwar
+# Write a script to display the first three self-descriptive numbers. As per
+# wikipedia, the definition of Self-descriptive Number is
+#
+# In mathematics, a self-descriptive number is an integer m that in a given
+# base b is b digits long in which each digit d at position n (the most
+# significant digit being at position 0 and the least significant at position
+# b-1) counts how many instances of digit n are in m.
+#
+# For example:
+#
+# 1210 is a four-digit self-descriptive number:
+#
+# position 0 has value 1 i.e. there is only one 0 in the number
+# position 1 has value 2 i.e. there are two 1 in the number
+# position 2 has value 1 i.e. there is only one 2 in the number
+# position 3 has value 0 i.e. there is no 3 in the number
+# Output
+# 1210, 2020, 21200
+
+def is_self_descriptive(n):
+ for i in range(len(n)):
+ count = len(list(filter(lambda x: x==i, n)))
+ if n[i]!=count:
+ return False
+ return True
+
+def increment(n, base):
+ i = len(n)-1
+ while i >= 0:
+ n[i] += 1
+ if n[i] < base:
+ return
+ else:
+ n[i] = 0
+ i -= 1
+ n.insert(0, 1)
+
+def self_descriptive(num):
+ found = []
+ base = 4
+ while True:
+ n = [0]*base
+ n[0] = 1
+ while len(n)==base:
+ if is_self_descriptive(n):
+ found.append("".join([str(x) for x in n]))
+ if len(found) >= num:
+ return found
+ increment(n, base)
+ base += 1
+
+self_descr = self_descriptive(3)
+print(", ".join(self_descr))
diff --git a/challenge-107/paulo-custodio/python/ch-2.py b/challenge-107/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..5874da0378
--- /dev/null
+++ b/challenge-107/paulo-custodio/python/ch-2.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+# Challenge 107
+#
+# TASK #2 - List Methods
+# Submitted by: Mohammad S Anwar
+# Write a script to list methods of a package/class.
+#
+# Example
+# package Calc;
+#
+# use strict;
+# use warnings;
+#
+# sub new { bless {}, shift; }
+# sub add { }
+# sub mul { }
+# sub div { }
+#
+# 1;
+# Output
+# BEGIN
+# mul
+# div
+# new
+# add
+
+class Calc():
+ def __init__(self):
+ pass
+ def new(self):
+ pass
+ def add(self):
+ pass
+ def mul(self):
+ pass
+ def div(self):
+ pass
+
+method_list = sorted([func for func in dir(Calc) \
+ if callable(getattr(Calc, func)) and not func.startswith("__")])
+for func in method_list:
+ print(func)
diff --git a/challenge-107/paulo-custodio/test.pl b/challenge-107/paulo-custodio/test.pl
deleted file mode 100755
index ba6c37260b..0000000000
--- a/challenge-107/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-108/paulo-custodio/Makefile b/challenge-108/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-108/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-108/paulo-custodio/python/ch-1.py b/challenge-108/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..132951c182
--- /dev/null
+++ b/challenge-108/paulo-custodio/python/ch-1.py
@@ -0,0 +1,12 @@
+#!/usr/bin/env python3
+
+# Challenge 108
+#
+# TASK #1 - Locate Memory
+# Submitted by: Mohammad S Anwar
+#
+# Write a script to declare a variable or constant and print it's location in
+# the memory.
+
+var = "hello world"
+print(hex(id(var)))
diff --git a/challenge-108/paulo-custodio/python/ch-2.py b/challenge-108/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..569e6f47bf
--- /dev/null
+++ b/challenge-108/paulo-custodio/python/ch-2.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+
+# Challenge 108
+#
+# TASK #2 - Bell Numbers
+# Submitted by: Mohammad S Anwar
+#
+# Write a script to display top 10 Bell Numbers. Please refer to wikipedia page
+# for more informations.
+#
+# Example:
+# B0: 1 as you can only have one partition of zero element set
+# B1: 1 as you can only have one partition of one element set {a}.
+# B2: 2
+# {a}{b}
+# {a,b}
+# B3: 5
+# {a}{b}{c}
+# {a,b}{c}
+# {a}{b,c}
+# {a,c}{b}
+# {a,b,c}
+# B4: 15
+# {a}{b}{c}{d}
+# {a,b,c,d}
+# {a,b}{c,d}
+# {a,c}{b,d}
+# {a,d}{b,c}
+# {a,b}{c}{d}
+# {a,c}{b}{d}
+# {a,d}{b}{c}
+# {b,c}{a}{d}
+# {b,d}{a}{c}
+# {c,d}{a}{b}
+# {a}{b,c,d}
+# {b}{a,c,d}
+# {c}{a,b,d}
+# {d}{a,b,c}
+
+import sys
+
+def bell_numbers():
+ n = -1
+ bell = []
+ while True:
+ n += 1
+ if n==0:
+ bell.append([1])
+ yield 1
+ else:
+ bell.append([bell[n-1][n-1]])
+ for i in range(1, n+1):
+ bell[n].append(bell[n-1][i-1] + bell[n][i-1])
+ yield bell[n][0]
+
+count = int(sys.argv[1])
+bell = []
+for n in bell_numbers():
+ if count<=0:
+ break
+ bell.append(n)
+ count -= 1
+print(" ".join([str(x) for x in bell]))
diff --git a/challenge-108/paulo-custodio/t/test-2.yaml b/challenge-108/paulo-custodio/t/test-2.yaml
index db839db250..123634d58d 100644
--- a/challenge-108/paulo-custodio/t/test-2.yaml
+++ b/challenge-108/paulo-custodio/t/test-2.yaml
@@ -1,5 +1,5 @@
- setup:
cleanup:
- args:
+ args: 10
input:
output: 1 1 2 5 15 52 203 877 4140 21147
diff --git a/challenge-108/paulo-custodio/test.pl b/challenge-108/paulo-custodio/test.pl
deleted file mode 100755
index ba6c37260b..0000000000
--- a/challenge-108/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-109/paulo-custodio/Makefile b/challenge-109/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-109/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-109/paulo-custodio/python/ch-1.py b/challenge-109/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..064ce7ab4d
--- /dev/null
+++ b/challenge-109/paulo-custodio/python/ch-1.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+
+# Challenge 109
+#
+# TASK #1 - Chowla Numbers
+# Submitted by: Mohammad S Anwar
+# Write a script to generate first 20 Chowla Numbers, named after,
+# Sarvadaman D. S. Chowla, a London born Indian American mathematician.
+# It is defined as:
+#
+# C(n) = sum of divisors of n except 1 and n
+# NOTE: Updated the above definition as suggested by Abigail [2021/04/19 18:40].
+# Output:
+# 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21
+
+import sys
+import math
+
+def divisors(n):
+ div_low = []
+ div_high = []
+ for i in range(1, int(math.sqrt(n)+1)):
+ if n%i==0:
+ div_low.append(i)
+ if n/i!=i:
+ div_high.append(int(n/i))
+ div_high = div_high[::-1]
+ return [*div_low, *div_high]
+
+def chowla(n):
+ terms = filter(lambda x: x!=1 and x!=n, divisors(n))
+ return sum(terms)
+
+def first_chowla(num):
+ nums = []
+ for i in range(1, num+1):
+ nums.append(chowla(i))
+ return nums
+
+print(", ".join([str(x) for x in first_chowla(int(sys.argv[1]))]))
diff --git a/challenge-109/paulo-custodio/python/ch-2.py b/challenge-109/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..5fdd001d37
--- /dev/null
+++ b/challenge-109/paulo-custodio/python/ch-2.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+
+# Challenge 109
+#
+# TASK #2 - Four Squares Puzzle
+# Submitted by: Mohammad S Anwar
+# You are given four squares as below with numbers named a,b,c,d,e,f,g.
+#
+# (1) (3)
+# +--------------+ +--------------+
+# | | | |
+# | a | | e |
+# | | (2) | | (4)
+# | +---+------+---+ +---+---------+
+# | | | | | | | |
+# | | b | | d | | f | |
+# | | | | | | | |
+# | | | | | | | |
+# +----------+---+ +---+------+---+ |
+# | c | | g |
+# | | | |
+# | | | |
+# +--------------+ +-------------+
+# Write a script to place the given unique numbers in the square box so that sum
+# of numbers in each box is the same.
+#
+# Example
+# Input: 1,2,3,4,5,6,7
+#
+# Output:
+#
+# a = 6
+# b = 4
+# c = 1
+# d = 5
+# e = 2
+# f = 3
+# g = 7
+#
+# Box 1: a + b = 6 + 4 = 10
+# Box 2: b + c + d = 4 + 1 + 5 = 10
+# Box 3: d + e + f = 5 + 2 + 3 = 10
+# Box 4: f + g = 3 + 7 = 10
+
+import sys
+from itertools import permutations
+
+# Note: return first solution found, not necessarily same as example
+def place_numbers(nums):
+ for p in permutations(nums, 7):
+ a,b,c,d,e,f,g = p[0],p[1],p[2],p[3],p[4],p[5],p[6]
+ sum = a+b
+ if b + c + d == sum and d + e + f == sum and f + g == sum:
+ return p
+
+result = place_numbers([int(x) for x in sys.argv[1:]])
+for i in range(7):
+ print(chr(ord('a')+i)+" = "+str(result[i]))
diff --git a/challenge-109/paulo-custodio/test.pl b/challenge-109/paulo-custodio/test.pl
deleted file mode 100755
index ba6c37260b..0000000000
--- a/challenge-109/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-110/paulo-custodio/Makefile b/challenge-110/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-110/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-110/paulo-custodio/python/ch-1.py b/challenge-110/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..d0ae52b786
--- /dev/null
+++ b/challenge-110/paulo-custodio/python/ch-1.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+# Challenge 110
+#
+# TASK #1 - Valid Phone Numbers
+# Submitted by: Mohammad S Anwar
+# You are given a text file.
+#
+# Write a script to display all valid phone numbers in the given text file.
+#
+# Acceptable Phone Number Formats
+# +nn nnnnnnnnnn
+# (nn) nnnnnnnnnn
+# nnnn nnnnnnnnnn
+# Input File
+# 0044 1148820341
+# +44 1148820341
+# 44-11-4882-0341
+# (44) 1148820341
+# 00 1148820341
+# Output
+# 0044 1148820341
+# +44 1148820341
+# (44) 1148820341
+
+import fileinput
+import sys
+import re
+
+def is_valid_phone(phone):
+ return re.match(r"^\s*(?:\+\d{2}|\(\d{2}\)|\d{4})\s+\d{10}\s*$", phone)
+
+def filter_input():
+ for phone in fileinput.input():
+ phone = phone.strip()
+ if is_valid_phone(phone):
+ print(phone)
+
+filter_input()
diff --git a/challenge-110/paulo-custodio/python/ch-2.py b/challenge-110/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..f73f24c8e9
--- /dev/null
+++ b/challenge-110/paulo-custodio/python/ch-2.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+
+# Challenge 110
+#
+# TASK #2 - Transpose File
+# Submitted by: Mohammad S Anwar
+# You are given a text file.
+#
+# Write a script to transpose the contents of the given file.
+#
+# Input File
+# name,age,sex
+# Mohammad,45,m
+# Joe,20,m
+# Julie,35,f
+# Cristina,10,f
+# Output:
+# name,Mohammad,Joe,Julie,Cristina
+# age,45,20,35,10
+# sex,m,m,f,f
+
+import fileinput
+import sys
+
+def read_input():
+ lines = []
+ for line in fileinput.input():
+ lines.append(line)
+ return lines
+
+def read_data(lines):
+ m = []
+ for line in lines:
+ line = line.strip()
+ cols = line.split(',')
+ m.append(cols)
+ return m
+
+def transpose(m):
+ t = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
+ return t
+
+def print_data(m):
+ for row in m:
+ print(",".join(row))
+
+print_data(transpose(read_data(read_input())))
diff --git a/challenge-110/paulo-custodio/test.pl b/challenge-110/paulo-custodio/test.pl
deleted file mode 100755
index ba6c37260b..0000000000
--- a/challenge-110/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';