aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-12-01 19:38:33 +0000
committerGitHub <noreply@github.com>2021-12-01 19:38:33 +0000
commitc3b51d8c2fa9efb9663bf81a0e80d81801b5d72b (patch)
treee4a42388a3984b37fbe5791c1927d489257586b0
parenta33d718bbcef606c60833c466881f1db8159b00a (diff)
parent9b5f294fd7feb46c546f0fa2b51ff4d12eb8ffe5 (diff)
downloadperlweeklychallenge-club-c3b51d8c2fa9efb9663bf81a0e80d81801b5d72b.tar.gz
perlweeklychallenge-club-c3b51d8c2fa9efb9663bf81a0e80d81801b5d72b.tar.bz2
perlweeklychallenge-club-c3b51d8c2fa9efb9663bf81a0e80d81801b5d72b.zip
Merge pull request #5312 from pauloscustodio/devel
Devel
-rw-r--r--challenge-022/paulo-custodio/Makefile2
-rw-r--r--challenge-022/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-022/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-022/paulo-custodio/python/ch-1.py39
-rw-r--r--challenge-022/paulo-custodio/python/ch-2.py100
-rw-r--r--challenge-022/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-022/paulo-custodio/t/test-2.yaml10
-rw-r--r--challenge-022/paulo-custodio/test.pl50
-rw-r--r--challenge-141/paulo-custodio/perl/ch-1.pl44
-rw-r--r--challenge-141/paulo-custodio/perl/ch-2.pl68
-rw-r--r--challenge-141/paulo-custodio/python/ch-1.py40
-rw-r--r--challenge-141/paulo-custodio/python/ch-2.py64
-rw-r--r--challenge-141/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-141/paulo-custodio/t/test-2.yaml10
14 files changed, 409 insertions, 52 deletions
diff --git a/challenge-022/paulo-custodio/Makefile b/challenge-022/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-022/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-022/paulo-custodio/perl/ch-1.pl b/challenge-022/paulo-custodio/perl/ch-1.pl
index 68e05cd91b..34225c0078 100644
--- a/challenge-022/paulo-custodio/perl/ch-1.pl
+++ b/challenge-022/paulo-custodio/perl/ch-1.pl
@@ -5,7 +5,7 @@
# Task #1
# Write a script to print first 10 Sexy Prime Pairs. Sexy primes are prime
# numbers that differ from each other by 6. For example, the numbers 5 and 11
-# are both sexy primes, because 11 - 5 = 6. The term “sexy prime” is a pun
+# are both sexy primes, because 11 - 5 = 6. The term "sexy prime" is a pun
# stemming from the Latin word for six: sex. For more information, please
# checkout wiki page.
diff --git a/challenge-022/paulo-custodio/perl/ch-2.pl b/challenge-022/paulo-custodio/perl/ch-2.pl
index 878d0162b9..27fd92c867 100644
--- a/challenge-022/paulo-custodio/perl/ch-2.pl
+++ b/challenge-022/paulo-custodio/perl/ch-2.pl
@@ -3,7 +3,7 @@
# Challenge 022
#
# Task #2
-# Write a script to implement Lempel–Ziv–Welch (LZW) compression algorithm.
+# Write a script to implement Lempel-Ziv-Welch (LZW) compression algorithm.
# The script should have method to encode/decode algorithm. The wiki page
# explains the compression algorithm very nicely.
diff --git a/challenge-022/paulo-custodio/python/ch-1.py b/challenge-022/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..346c160fb4
--- /dev/null
+++ b/challenge-022/paulo-custodio/python/ch-1.py
@@ -0,0 +1,39 @@
+#!/usr/bin/python3
+
+# Challenge 022
+#
+# Task #1
+# Write a script to print first 10 Sexy Prime Pairs. Sexy primes are prime
+# numbers that differ from each other by 6. For example, the numbers 5 and 11
+# are both sexy primes, because 11 - 5 = 6. The term "sexy prime" is a pun
+# stemming from the Latin word for six: sex. For more information, please
+# checkout wiki page.
+
+import sys
+from primePy import primes
+
+def next_prime(n):
+ if n <= 1:
+ return 2
+ else:
+ n += 1
+ while not primes.check(n):
+ n += 1
+ return n
+
+def sexy_primes_iter():
+ a = 1
+ while True:
+ a = next_prime(a)
+ b = a
+ while b < a+6:
+ b = next_prime(b)
+ if b == a+6:
+ yield (a, b)
+
+count = 0
+for a, b in sexy_primes_iter():
+ print(f"({a}, {b})")
+ count += 1
+ if count >= 10:
+ break
diff --git a/challenge-022/paulo-custodio/python/ch-2.py b/challenge-022/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..8900cf37cd
--- /dev/null
+++ b/challenge-022/paulo-custodio/python/ch-2.py
@@ -0,0 +1,100 @@
+#!/usr/bin/python3
+
+# Challenge 022
+#
+# Task #2
+# Write a script to implement Lempel-Ziv-Welch (LZW) compression algorithm.
+# The script should have method to encode/decode algorithm. The wiki page
+# explains the compression algorithm very nicely.
+
+import sys
+
+class Dict():
+ EOM = '#'
+ SYMBOLS = ['A','B','C','D','E','F','G','H','I','J','K','L','M',
+ 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
+
+ def __init__(self):
+ self.dict = {}
+ self.symbols = []
+ for sym in [Dict.EOM, *Dict.SYMBOLS]:
+ self.add(sym)
+
+ def __str__(self):
+ return "dict="+str(self.dict)+",symbols="+str(self.symbols)
+
+ def last(self):
+ return len(self.symbols)-1
+
+ def width(self):
+ return len("{:b}".format(self.last()))
+
+ def next_width(self):
+ return len("{:b}".format(self.last()+1))
+
+ def add(self, seq):
+ seq = seq.upper()
+
+ id = self.last()+1
+ self.dict[seq] = id
+ self.symbols.append(seq)
+
+ def longest_match(self, text):
+ text = text.upper()
+
+ # find longest match
+ match_len = 0
+ while match_len < len(text) and \
+ text[:match_len+1] in self.dict:
+ match_len += 1
+ w = text[:match_len]
+ text = text[match_len:]
+ code = self.dict[w]
+ old_width = self.width()
+
+ # store new prefix in the dictionary
+ if text != "":
+ next_prefix = w+text[0]
+ self.add(next_prefix)
+
+ # return code and new text
+ return ("{:0"+str(old_width)+"b}").format(code), text
+
+def encode(text):
+ text = text.upper()+Dict.EOM
+ encoded = ""
+ dict = Dict()
+
+ while text != "":
+ code, text = dict.longest_match(text)
+ encoded += code
+
+ return encoded
+
+def decode(encoded):
+ text = ""
+ dict = Dict()
+
+ while encoded != "":
+ width = dict.width()
+ code = int(encoded[:width], 2)
+ encoded = encoded[width:]
+ seq = dict.symbols[code]
+ text += seq
+
+ if encoded != "":
+ next_width = dict.next_width()
+ next_code = int(encoded[:next_width], 2)
+ next_seq = dict.symbols[next_code]
+ dict.add(seq + next_seq[0])
+
+ # remove end terminator
+ text = text[:-1]
+ return text
+
+if sys.argv[1] == "encode":
+ print(encode(sys.argv[2]))
+elif sys.argv[1] == "decode":
+ print(decode(sys.argv[2]))
+else:
+ print("Usage: ch-2.py encode|decode text")
diff --git a/challenge-022/paulo-custodio/t/test-1.yaml b/challenge-022/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..f5b7fc6388
--- /dev/null
+++ b/challenge-022/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: |
+ (5, 11)
+ (7, 13)
+ (11, 17)
+ (13, 19)
+ (17, 23)
+ (23, 29)
+ (31, 37)
+ (37, 43)
+ (41, 47)
+ (47, 53)
diff --git a/challenge-022/paulo-custodio/t/test-2.yaml b/challenge-022/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..06a3467c73
--- /dev/null
+++ b/challenge-022/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: encode TOBEORNOTTOBEORTOBEORNOT
+ input:
+ output: 101000111100010001010111110010001110001111010100011011011101011111100100011110100000100010000000
+- setup:
+ cleanup:
+ args: decode 101000111100010001010111110010001110001111010100011011011101011111100100011110100000100010000000
+ input:
+ output: TOBEORNOTTOBEORTOBEORNOT
diff --git a/challenge-022/paulo-custodio/test.pl b/challenge-022/paulo-custodio/test.pl
deleted file mode 100644
index 4fb163a066..0000000000
--- a/challenge-022/paulo-custodio/test.pl
+++ /dev/null
@@ -1,50 +0,0 @@
-#!/usr/bin/perl
-
-use Modern::Perl;
-use Test::More;
-
-is capture("perl perl/ch-1.pl"), <<END;
-(5, 11)
-(7, 13)
-(11, 17)
-(13, 19)
-(17, 23)
-(23, 29)
-(31, 37)
-(37, 43)
-(41, 47)
-(47, 53)
-END
-
-my($text, $encoded) = ("TOBEORNOTTOBEORTOBEORNOT", join("", qw(
- 10100
- 01111
- 00010
- 00101
- 01111
- 10010
- 001110
- 001111
- 010100
- 011011
- 011101
- 011111
- 100100
- 011110
- 100000
- 100010
- 000000
- )));
-
-is capture("perl perl/ch-2.pl encode $text"), "$encoded\n", "$text -> $encoded";
-is capture("perl perl/ch-2.pl decode $encoded"), "$text\n", "$encoded -> $text";
-
-done_testing;
-
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \t\v\f\r]*\n/\n/g;
- return $out;
-}
diff --git a/challenge-141/paulo-custodio/perl/ch-1.pl b/challenge-141/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..e012a7e143
--- /dev/null
+++ b/challenge-141/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+# Challenge 141
+#
+# TASK #1 > Number Divisors
+# Submitted by: Mohammad S Anwar
+# Write a script to find lowest 10 positive integers having exactly 8 divisors.
+#
+# Example
+# 24 is the first such number having exactly 8 divisors.
+# 1, 2, 3, 4, 6, 8, 12 and 24.
+
+use Modern::Perl;
+
+use constant NUM_DIVISORS => 8;
+
+sub divisors {
+ my($n) = @_;
+ my(@div_low, @div_high);
+ for (my $i = 1; $i <= sqrt($n); $i++) {
+ if ($n%$i == 0) {
+ push @div_low, $i;
+ unshift @div_high, $n/$i if $n/$i != $i;
+ }
+ }
+ return (@div_low, @div_high);
+}
+
+sub next_number {
+ my($n) = @_;
+
+ for (;;) {
+ $n++;
+ my @divisors = divisors($n);
+ return $n if @divisors == NUM_DIVISORS;
+ }
+}
+
+my $num = shift||10;
+my $n = 0;
+for (1..$num) {
+ $n = next_number($n);
+ say $n;
+}
diff --git a/challenge-141/paulo-custodio/perl/ch-2.pl b/challenge-141/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..3350785871
--- /dev/null
+++ b/challenge-141/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+
+# Challenge 141
+#
+# TASK #2 > Like Numbers
+# Submitted by: Mohammad S Anwar
+# You are given positive integers, $m and $n.
+#
+# Write a script to find total count of integers created using the digits
+# of $m which is also divisible by $n.
+#
+# Repeating of digits are not allowed. Order/Sequence of digits can't be
+# altered. You are only allowed to use (n-1) digits at the most. For example,
+# 432 is not acceptable integer created using the digits of 1234. Also for
+# 1234, you can only have integers having no more than three digits.
+#
+# Example 1:
+# Input: $m = 1234, $n = 2
+# Output: 9
+#
+# Possible integers created using the digits of 1234 are:
+# 1, 2, 3, 4, 12, 13, 14, 23, 24, 34, 123, 124, 134 and 234.
+#
+# There are 9 integers divisible by 2 such as:
+# 2, 4, 12, 14, 24, 34, 124, 134 and 234.
+# Example 2:
+# Input: $m = 768, $n = 4
+# Output: 3
+#
+# Possible integers created using the digits of 768 are:
+# 7, 6, 8, 76, 78 and 68.
+#
+# There are 3 integers divisible by 4 such as:
+# 8, 76 and 68.
+
+use Modern::Perl;
+
+sub numbers {
+ my($num) = @_;
+ my @ret;
+
+ my $mask_n = 0;
+ for (;; $mask_n++) {
+ my $mask = sprintf("%0".length($num)."b", $mask_n);
+ last if length($mask) > length($num);
+
+ # combine $num with $mask
+ my $res = 0;
+ for my $i (0 .. length($num)-1) {
+ if (substr($mask, $i, 1) eq "1") {
+ $res = 10*$res + substr($num, $i, 1);
+ }
+ }
+ push @ret, $res;
+ }
+ return @ret;
+}
+
+my($m, $n) = @ARGV;
+my $count = 0;
+for my $num (numbers($m)) {
+ if ($num != 0 && $num != $m) {
+ if ($num % $n == 0) {
+ $count++;
+ }
+ }
+}
+say $count;
diff --git a/challenge-141/paulo-custodio/python/ch-1.py b/challenge-141/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..fe912ccca5
--- /dev/null
+++ b/challenge-141/paulo-custodio/python/ch-1.py
@@ -0,0 +1,40 @@
+#!/usr/bin/python3
+
+# Challenge 141
+#
+# TASK #1 > Number Divisors
+# Submitted by: Mohammad S Anwar
+# Write a script to find lowest 10 positive integers having exactly 8 divisors.
+#
+# Example
+# 24 is the first such number having exactly 8 divisors.
+# 1, 2, 3, 4, 6, 8, 12 and 24.
+
+import sys
+import math
+
+NUM_DIVISORS = 8
+
+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 next_number(n):
+ while True:
+ n += 1
+ divs = divisors(n)
+ if len(divs)==NUM_DIVISORS:
+ return n
+
+num = int(sys.argv[1])
+n = 0
+for i in range(num):
+ n = next_number(n)
+ print(n)
diff --git a/challenge-141/paulo-custodio/python/ch-2.py b/challenge-141/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..f1eb159af7
--- /dev/null
+++ b/challenge-141/paulo-custodio/python/ch-2.py
@@ -0,0 +1,64 @@
+#!/usr/bin/python3
+
+# Challenge 141
+#
+# TASK #2 > Like Numbers
+# Submitted by: Mohammad S Anwar
+# You are given positive integers, $m and $n.
+#
+# Write a script to find total count of integers created using the digits
+# of $m which is also divisible by $n.
+#
+# Repeating of digits are not allowed. Order/Sequence of digits can't be
+# altered. You are only allowed to use (n-1) digits at the most. For example,
+# 432 is not acceptable integer created using the digits of 1234. Also for
+# 1234, you can only have integers having no more than three digits.
+#
+# Example 1:
+# Input: $m = 1234, $n = 2
+# Output: 9
+#
+# Possible integers created using the digits of 1234 are:
+# 1, 2, 3, 4, 12, 13, 14, 23, 24, 34, 123, 124, 134 and 234.
+#
+# There are 9 integers divisible by 2 such as:
+# 2, 4, 12, 14, 24, 34, 124, 134 and 234.
+# Example 2:
+# Input: $m = 768, $n = 4
+# Output: 3
+#
+# Possible integers created using the digits of 768 are:
+# 7, 6, 8, 76, 78 and 68.
+#
+# There are 3 integers divisible by 4 such as:
+# 8, 76 and 68.
+
+import sys
+
+def numbers(num):
+ ret = []
+ mask_n = 0
+ while True:
+ mask = ("{:0"+str(len(str(num)))+"b}").format(mask_n)
+ if len(mask) > len(str(num)):
+ break
+
+ # combine num with mask
+ res = 0
+ for i in range(len(str(num))):
+ if mask[i] == "1":
+ res = 10*res + int(str(num)[i])
+
+ ret.append(res)
+ mask_n += 1
+
+ return ret
+
+m = int(sys.argv[1])
+n = int(sys.argv[2])
+count = 0
+for num in numbers(m):
+ if num != 0 and num != m:
+ if num % n == 0:
+ count += 1
+print(count)
diff --git a/challenge-141/paulo-custodio/t/test-1.yaml b/challenge-141/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..1a802e4e2c
--- /dev/null
+++ b/challenge-141/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 10
+ input:
+ output: |
+ 24
+ 30
+ 40
+ 42
+ 54
+ 56
+ 66
+ 70
+ 78
+ 88
diff --git a/challenge-141/paulo-custodio/t/test-2.yaml b/challenge-141/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..ce5c683394
--- /dev/null
+++ b/challenge-141/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 1234 2
+ input:
+ output: 9
+- setup:
+ cleanup:
+ args: 768 4
+ input:
+ output: 3