aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-16 20:21:48 +0000
committerGitHub <noreply@github.com>2021-11-16 20:21:48 +0000
commit7d03517a91f29f6ed6f3d8c3cade7973524b4ba4 (patch)
treeb3ad3a4be8d1d666fa2bfae5e1005df6c6d2b940
parenta3ba958fc0f3bdbf0729c28be9de0e4f373fbe02 (diff)
parent429dde959e2bbde5fa2d4b9dd98fe6c2c035e597 (diff)
downloadperlweeklychallenge-club-7d03517a91f29f6ed6f3d8c3cade7973524b4ba4.tar.gz
perlweeklychallenge-club-7d03517a91f29f6ed6f3d8c3cade7973524b4ba4.tar.bz2
perlweeklychallenge-club-7d03517a91f29f6ed6f3d8c3cade7973524b4ba4.zip
Merge pull request #5234 from pauloscustodio/devel
Devel
-rw-r--r--challenge-008/paulo-custodio/Makefile2
-rw-r--r--challenge-008/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-008/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-008/paulo-custodio/python/ch-1.py51
-rw-r--r--challenge-008/paulo-custodio/python/ch-2.py21
-rw-r--r--challenge-008/paulo-custodio/test.pl4
-rw-r--r--challenge-009/paulo-custodio/Makefile2
-rw-r--r--challenge-009/paulo-custodio/python/ch-1.py18
-rw-r--r--challenge-009/paulo-custodio/python/ch-2.py97
-rw-r--r--challenge-009/paulo-custodio/test.pl4
-rw-r--r--challenge-010/paulo-custodio/Makefile2
-rw-r--r--challenge-010/paulo-custodio/python/ch-1.py28
-rw-r--r--challenge-010/paulo-custodio/python/ch-2.py12
-rw-r--r--challenge-010/paulo-custodio/test.pl4
-rw-r--r--challenge-011/paulo-custodio/Makefile2
-rw-r--r--challenge-011/paulo-custodio/python/ch-1.py2
-rw-r--r--challenge-011/paulo-custodio/python/ch-2.py29
-rw-r--r--challenge-011/paulo-custodio/test.pl4
-rw-r--r--challenge-139/paulo-custodio/perl/ch-1.pl32
-rw-r--r--challenge-139/paulo-custodio/perl/ch-2.pl58
-rw-r--r--challenge-139/paulo-custodio/python/ch-1.py30
-rw-r--r--challenge-139/paulo-custodio/python/ch-2.py70
-rw-r--r--challenge-139/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-139/paulo-custodio/t/test-2.yaml10
24 files changed, 477 insertions, 19 deletions
diff --git a/challenge-008/paulo-custodio/Makefile b/challenge-008/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-008/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-008/paulo-custodio/perl/ch-1.pl b/challenge-008/paulo-custodio/perl/ch-1.pl
index 41a8010327..a0c9a72945 100644
--- a/challenge-008/paulo-custodio/perl/ch-1.pl
+++ b/challenge-008/paulo-custodio/perl/ch-1.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 008
#
diff --git a/challenge-008/paulo-custodio/perl/ch-2.pl b/challenge-008/paulo-custodio/perl/ch-2.pl
index 09e628d446..8edbc236fa 100644
--- a/challenge-008/paulo-custodio/perl/ch-2.pl
+++ b/challenge-008/paulo-custodio/perl/ch-2.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 008
#
diff --git a/challenge-008/paulo-custodio/python/ch-1.py b/challenge-008/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..9e11164357
--- /dev/null
+++ b/challenge-008/paulo-custodio/python/ch-1.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+
+# Challenge 008
+#
+# Challenge #1
+# Write a script that computes the first five perfect numbers. A perfect number
+# is an integer that is the sum of its positive proper divisors (all divisors
+# except itself). Please check Wiki for more information. This challenge was
+# proposed by Laurent Rosenfeld.
+
+import sys
+
+def is_prime(n):
+ if n <= 1:
+ return 0
+ elif n <= 3:
+ return 1
+ elif n % 2 == 0 or n % 3 == 0:
+ return 0
+ else:
+ for i in range(5, n+1, 6):
+ if i*i>n:
+ break
+ if n % i == 0 or n % (i+2) == 0:
+ return 0
+ return 1
+
+def next_prime(n):
+ if n<=1:
+ return 2
+ n += 1
+ while not is_prime(n):
+ n += 1
+ return n
+
+# Euclid proved that 2^(p-1)*(2^p - 1) is an even perfect number
+# whenever 2p - 1 is prime
+def perfect_iter():
+ p = 1
+ while True:
+ p = next_prime(p)
+ f = (2**p)-1
+ if is_prime(f):
+ yield (2**(p-1))*f
+
+count = int(sys.argv[1])
+for n in perfect_iter():
+ print(n)
+ count -= 1
+ if count<=0:
+ break
diff --git a/challenge-008/paulo-custodio/python/ch-2.py b/challenge-008/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..fc6bb3acd4
--- /dev/null
+++ b/challenge-008/paulo-custodio/python/ch-2.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+
+# Challenge 008
+#
+# Challenge #2
+# Write a function, 'center', whose argument is a list of strings, which will
+# be lines of text. The function should insert spaces at the beginning of the
+# lines of text so that if they were printed, the text would be centered, and
+# return the modified lines.
+
+import sys
+
+def center(lines):
+ max_len = max([len(x) for x in lines])
+ for i in range(len(lines)):
+ lines[i] = " "*int((max_len-len(lines[i]))/2) + lines[i]
+ return lines
+
+lines = center(sys.argv[1:])
+for line in lines:
+ print(line)
diff --git a/challenge-008/paulo-custodio/test.pl b/challenge-008/paulo-custodio/test.pl
deleted file mode 100644
index ba6c37260b..0000000000
--- a/challenge-008/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-009/paulo-custodio/Makefile b/challenge-009/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-009/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-009/paulo-custodio/python/ch-1.py b/challenge-009/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..2482dae8c6
--- /dev/null
+++ b/challenge-009/paulo-custodio/python/ch-1.py
@@ -0,0 +1,18 @@
+#!/usr/bin/python3
+
+# Challenge 009
+#
+# Challenge #1
+# Write a script that finds the first square number that has at least 5 distinct
+# digits. This was proposed by Laurent Rosenfeld.
+
+import sys
+
+def num_diff_digits(n):
+ return len(set([x for x in str(n)]))
+
+min_diff_digits = int(sys.argv[1])
+n = 1
+while num_diff_digits(n*n) < min_diff_digits:
+ n += 1
+print(n*n)
diff --git a/challenge-009/paulo-custodio/python/ch-2.py b/challenge-009/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..4c7f8135d3
--- /dev/null
+++ b/challenge-009/paulo-custodio/python/ch-2.py
@@ -0,0 +1,97 @@
+#!/usr/bin/python3
+
+# Challenge 009
+#
+# Challenge #2
+# Write a script to perform different types of ranking as described below:
+#
+# 1. Standard Ranking (1224): Items that compare equal receive the same ranking
+# number, and then a gap is left in the ranking numbers.
+# 2. Modified Ranking (1334): It is done by leaving the gaps in the ranking
+# numbers before the sets of equal-ranking items.
+# 3. Dense Ranking (1223): Items that compare equally receive the same
+# ranking number, and the next item(s) receive the immediately following
+# ranking number.
+
+import sys
+import operator
+
+class Item():
+ def __init__(self, index, value):
+ self.index = index
+ self.value = value
+ self.rank = 0
+
+ def __str__(self):
+ return f"Item(index={self.index},value={self.value},rank={self.rank})"
+
+class Items():
+ def __init__(self, values):
+ self.items = []
+ for index, value in enumerate(values):
+ self.items.append(Item(index, value))
+
+ def __str__(self):
+ return "["+ ", ".join([str(x) for x in self.items]) +"]"
+
+ def elems(self):
+ return self.items
+
+ def sort_by_value(self):
+ self.items = sorted(self.items, key=operator.attrgetter('value'))[::-1]
+
+ def sort_by_index(self):
+ self.items = sorted(self.items, key=operator.attrgetter('index'))
+
+ def count_same(self, i):
+ value = self.items[i].value
+ count = 0
+ while i < len(self.items) and self.items[i].value == value:
+ count += 1
+ i += 1
+ return count
+
+ def standard_ranking(self):
+ self.sort_by_value()
+ rank = 1
+ i = 0
+ while i < len(self.items):
+ same = self.count_same(i)
+ for j in range(i, i+same):
+ self.items[j].rank = rank
+ rank += same
+ i += same
+ self.sort_by_index()
+
+ def modified_ranking(self):
+ self.sort_by_value()
+ rank = 1
+ i = 0
+ while i < len(self.items):
+ same = self.count_same(i)
+ for j in range(i, i+same):
+ self.items[j].rank = rank+same-1
+ rank += same
+ i += same
+ self.sort_by_index()
+
+ def dense_ranking(self):
+ self.sort_by_value()
+ rank = 1
+ i = 0
+ while i < len(self.items):
+ same = self.count_same(i)
+ for j in range(i, i+same):
+ self.items[j].rank = rank
+ rank += 1
+ i += same
+ self.sort_by_index()
+
+items = Items([int(x) for x in sys.argv[1:]])
+print("Data: "+", ".join([str(x.value) for x in items.elems()]))
+items.standard_ranking()
+print("Standard ranking: "+", ".join([str(x.rank) for x in items.elems()]))
+items.modified_ranking()
+print("Modified ranking: "+", ".join([str(x.rank) for x in items.elems()]))
+items.dense_ranking()
+print("Dense ranking: "+", ".join([str(x.rank) for x in items.elems()]))
diff --git a/challenge-009/paulo-custodio/test.pl b/challenge-009/paulo-custodio/test.pl
deleted file mode 100644
index ba6c37260b..0000000000
--- a/challenge-009/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-010/paulo-custodio/Makefile b/challenge-010/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-010/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-010/paulo-custodio/python/ch-1.py b/challenge-010/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..bbbee60dde
--- /dev/null
+++ b/challenge-010/paulo-custodio/python/ch-1.py
@@ -0,0 +1,28 @@
+#!/usr/bin/python3
+
+# Challenge 010
+#
+# Challenge #1
+# Write a script to encode/decode Roman numerals. For example, given Roman
+# numeral CCXLVI, it should return 246. Similarly, for decimal number 39, it
+# should return XXXIX. Checkout wikipedia page for more information.
+
+import sys
+import roman
+
+if len(sys.argv)==2:
+ if sys.argv[1]=="-test":
+ for i in range(1, 3001):
+ s = roman.toRoman(i)
+ n = roman.fromRoman(s)
+ if i!=n:
+ print(f"{i} => {s} => {n}")
+ sys.exit(1)
+ elif sys.argv[1].isdigit():
+ n = int(sys.argv[1])
+ s = roman.toRoman(n)
+ print(f"{n} => {s}")
+ else:
+ s = sys.argv[1]
+ n = roman.fromRoman(s)
+ print(f"{s} => {n}")
diff --git a/challenge-010/paulo-custodio/python/ch-2.py b/challenge-010/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..6dd3ebe04a
--- /dev/null
+++ b/challenge-010/paulo-custodio/python/ch-2.py
@@ -0,0 +1,12 @@
+#!/usr/bin/python3
+
+# Challenge 010
+#
+# Challenge #2
+# Write a script to find Jaro-Winkler distance between two strings. For more
+# information check wikipedia page.
+
+import sys
+from pyjarowinkler import distance
+
+print("{:.2f}".format(1-distance.get_jaro_distance(*sys.argv[1:3])))
diff --git a/challenge-010/paulo-custodio/test.pl b/challenge-010/paulo-custodio/test.pl
deleted file mode 100644
index ba6c37260b..0000000000
--- a/challenge-010/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-011/paulo-custodio/Makefile b/challenge-011/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-011/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-011/paulo-custodio/python/ch-1.py b/challenge-011/paulo-custodio/python/ch-1.py
index d8f93b7668..766c48fbba 100644
--- a/challenge-011/paulo-custodio/python/ch-1.py
+++ b/challenge-011/paulo-custodio/python/ch-1.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python3
# Challenge 011
#
diff --git a/challenge-011/paulo-custodio/python/ch-2.py b/challenge-011/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..7402779a99
--- /dev/null
+++ b/challenge-011/paulo-custodio/python/ch-2.py
@@ -0,0 +1,29 @@
+#!/usr/bin/python3
+
+# Challenge 011
+#
+# Challenge #2
+# Write a script to create an Indentity Matrix for the given size. For example,
+# if the size is 4, then create Identity Matrix 4x4. For more information about
+# Indentity Matrix, please read the wiki page.
+
+import sys
+
+def id_matrix(size):
+ id = [[0 for y in range(size)] for x in range(size)]
+ for i in range(size):
+ id[i][i] = 1
+ return id
+
+def print_matrix(m):
+ output = "["
+ sep = ""
+ for row in m:
+ output += sep+"["+", ".join([str(x) for x in row])+"]"
+ sep = ",\n "
+ output += "]"
+ print(output)
+
+size = int(sys.argv[1])
+id = id_matrix(size)
+print_matrix(id)
diff --git a/challenge-011/paulo-custodio/test.pl b/challenge-011/paulo-custodio/test.pl
deleted file mode 100644
index ba6c37260b..0000000000
--- a/challenge-011/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-139/paulo-custodio/perl/ch-1.pl b/challenge-139/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..b182d12cb4
--- /dev/null
+++ b/challenge-139/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+
+# TASK #1 > JortSort
+# Submitted by: Mohammad S Anwar
+# You are given a list of numbers.
+#
+# Write a script to implement JortSort. It should return true/false depending
+# if the given list of numbers are already sorted.
+#
+# Example 1:
+# Input: @n = (1,2,3,4,5)
+# Output: 1
+#
+# Since the array is sorted, it prints 1.
+# Example 2:
+# Input: @n = (1,3,2,4,5)
+# Output: 0
+#
+# Since the array is NOT sorted, it prints 0.
+
+use Modern::Perl;
+
+sub jortsort {
+ my(@nums) = @_;
+ my @sorted = sort {$a<=>$b} @nums;
+ for (0..$#nums) {
+ return 0 if $nums[$_]!=$sorted[$_];
+ }
+ return 1;
+}
+
+say jortsort(@ARGV);
diff --git a/challenge-139/paulo-custodio/perl/ch-2.pl b/challenge-139/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..4404e1f67d
--- /dev/null
+++ b/challenge-139/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/env perl
+
+# TASK #2 > Long Primes
+# Submitted by: Mohammad S Anwar
+# Write a script to generate first 5 Long Primes.
+#
+# A prime number (p) is called Long Prime if (1/p) has an infinite decimal
+# expansion repeating every (p-1) digits.
+#
+# Example
+# 7 is a long prime since 1/7 = 0.142857142857...
+# The repeating part (142857) size is 6 i.e. one less than the prime number 7.
+#
+# Also 17 is a long prime since 1/17 = 0.05882352941176470588235294117647...
+# The repeating part (0588235294117647) size is 16 i.e. one less than the
+# prime number 17.
+#
+# Another example, 2 is not a long prime as 1/2 = 0.5.
+# There is no repeating part in this case.
+
+use Modern::Perl;
+use Math::BigFloat;
+use ntheory 'next_prime';
+
+Math::BigFloat->accuracy(1000); # very long list of digits
+
+my $N = shift||5;
+my $prime = 2;
+for (1..$N) {
+ while (!is_long_prime($prime)) {
+ $prime = next_prime($prime);
+ }
+ say $prime;
+ $prime = next_prime($prime);
+}
+
+sub is_long_prime {
+ my($p) = @_;
+ my $inv = Math::BigFloat->new(1) / Math::BigFloat->new($p);
+ if (rept_sequence($inv, $p-1)==$p-1) {
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+
+sub rept_sequence {
+ my($n, $max) = @_;
+ for my $rept (1..$max) {
+ my $rept1 = $rept-1;
+ my $code = "\$n =~ /\\.(\\d{$rept})\\1+\\d{0,$rept1}\$/;";
+ if (eval $code) {
+ return $rept;
+ }
+ }
+ return -1;
+}
diff --git a/challenge-139/paulo-custodio/python/ch-1.py b/challenge-139/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..a5404eede9
--- /dev/null
+++ b/challenge-139/paulo-custodio/python/ch-1.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+# TASK #1 > JortSort
+# Submitted by: Mohammad S Anwar
+# You are given a list of numbers.
+#
+# Write a script to implement JortSort. It should return true/false depending
+# if the given list of numbers are already sorted.
+#
+# Example 1:
+# Input: @n = (1,2,3,4,5)
+# Output: 1
+#
+# Since the array is sorted, it prints 1.
+# Example 2:
+# Input: @n = (1,3,2,4,5)
+# Output: 0
+#
+# Since the array is NOT sorted, it prints 0.
+
+import sys
+
+def jortsort(a):
+ sa = sorted(a)
+ for i in range(len(a)):
+ if a[i]!=sa[i]:
+ return False
+ return True
+
+print(1 if jortsort([int(x) for x in sys.argv[1:]]) else 0)
diff --git a/challenge-139/paulo-custodio/python/ch-2.py b/challenge-139/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..37fa045f25
--- /dev/null
+++ b/challenge-139/paulo-custodio/python/ch-2.py
@@ -0,0 +1,70 @@
+#!/usr/bin/env python3
+
+# TASK #2 > Long Primes
+# Submitted by: Mohammad S Anwar
+# Write a script to generate first 5 Long Primes.
+#
+# A prime number (p) is called Long Prime if (1/p) has an infinite decimal
+# expansion repeating every (p-1) digits.
+#
+# Example
+# 7 is a long prime since 1/7 = 0.142857142857...
+# The repeating part (142857) size is 6 i.e. one less than the prime number 7.
+#
+# Also 17 is a long prime since 1/17 = 0.05882352941176470588235294117647...
+# The repeating part (0588235294117647) size is 16 i.e. one less than the
+# prime number 17.
+#
+# Another example, 2 is not a long prime as 1/2 = 0.5.
+# There is no repeating part in this case.
+
+import sys
+import re
+from decimal import *
+
+getcontext().prec = 1000
+getcontext().rounding = ROUND_DOWN
+
+def is_prime(n):
+ if n <= 1:
+ return 0
+ elif n <= 3:
+ return 1
+ elif n % 2 == 0 or n % 3 == 0:
+ return 0
+ else:
+ for i in range(5, n+1, 6):
+ if i*i>n:
+ break
+ if n % i == 0 or n % (i+2) == 0:
+ return 0
+ return 1
+
+def rept_sequence(n, max):
+ #print("rept_sequence", n, max)
+ for rept in range(1, max+1):
+ if re.search(r"\.(\d{"+str(rept)+r"})\1+", str(n)):
+ #print(rept)
+ return rept
+ #print(-1)
+ return -1
+
+def is_long_prime(p):
+ if not is_prime(p):
+ return False
+ inv = Decimal(1) / Decimal(p)
+ #print("check", inv, p, rept_sequence(inv, p-1))
+ if rept_sequence(inv, p-1)==p-1:
+ return True
+ else:
+ return False
+
+def print_long_primes(n):
+ p = 2
+ for i in range(n):
+ while not is_long_prime(p):
+ p += 1
+ print(p)
+ p += 1
+
+print_long_primes(int(sys.argv[1]))
diff --git a/challenge-139/paulo-custodio/t/test-1.yaml b/challenge-139/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..ff33b5b648
--- /dev/null
+++ b/challenge-139/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 1 2 3 4 5
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 1 3 2 4 5
+ input:
+ output: 0
diff --git a/challenge-139/paulo-custodio/t/test-2.yaml b/challenge-139/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..cc94e5612a
--- /dev/null
+++ b/challenge-139/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 5
+ input:
+ output: |
+ 7
+ 17
+ 19
+ 23
+ 29