aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-12-06 20:28:59 +0000
committerGitHub <noreply@github.com>2021-12-06 20:28:59 +0000
commit4972aecab28d6e78bd949512632c290e8acddbf9 (patch)
tree775d2044c469505efd30ddca51f02a058cc66fb5
parenta620de2dcf2f0510f4bac760cd0a650be2e8b2df (diff)
parent519d44c0a7356bc03840549e421446656f7f2813 (diff)
downloadperlweeklychallenge-club-4972aecab28d6e78bd949512632c290e8acddbf9.tar.gz
perlweeklychallenge-club-4972aecab28d6e78bd949512632c290e8acddbf9.tar.bz2
perlweeklychallenge-club-4972aecab28d6e78bd949512632c290e8acddbf9.zip
Merge pull request #5341 from pauloscustodio/devel
Devel
-rw-r--r--challenge-001/paulo-custodio/check_challenge_title.pl10
-rw-r--r--challenge-023/paulo-custodio/Makefile2
-rw-r--r--challenge-023/paulo-custodio/perl/ch-1.pl4
-rw-r--r--challenge-023/paulo-custodio/perl/ch-2.pl7
-rw-r--r--challenge-023/paulo-custodio/python/ch-1.py32
-rw-r--r--challenge-023/paulo-custodio/python/ch-2.py38
-rw-r--r--challenge-023/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-023/paulo-custodio/t/test-2.yaml55
-rw-r--r--challenge-023/paulo-custodio/test.pl30
-rw-r--r--challenge-024/paulo-custodio/perl/ch-1.pl0
-rw-r--r--challenge-142/paulo-custodio/perl/ch-1.pl41
-rw-r--r--challenge-142/paulo-custodio/perl/ch-2.pl24
-rw-r--r--challenge-142/paulo-custodio/python/ch-1.py42
-rw-r--r--challenge-142/paulo-custodio/python/ch-2.py26
-rw-r--r--challenge-142/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-142/paulo-custodio/t/test-2.yaml14
16 files changed, 310 insertions, 40 deletions
diff --git a/challenge-001/paulo-custodio/check_challenge_title.pl b/challenge-001/paulo-custodio/check_challenge_title.pl
index cd36c62e87..02a8d1babd 100644
--- a/challenge-001/paulo-custodio/check_challenge_title.pl
+++ b/challenge-001/paulo-custodio/check_challenge_title.pl
@@ -33,10 +33,10 @@ for my $chall_dir (path(".")->children(qr/challenge-\d+/)) {
next unless $dir->is_dir;
for my $sol ($dir->children(qr/^ch[-_]\d\.$LANG{$lang}$/)) {
- my $text = $sol->slurp;
- if ($text !~ /Challenge 0*$chall\b/) {
- say $sol;
- }
- }
+ my $text = $sol->slurp;
+ if ($text !~ /Challenge 0*$chall\b/) {
+ say $sol;
+ }
+ }
}
}
diff --git a/challenge-023/paulo-custodio/Makefile b/challenge-023/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-023/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-023/paulo-custodio/perl/ch-1.pl b/challenge-023/paulo-custodio/perl/ch-1.pl
index ee500f38dc..581caa45f9 100644
--- a/challenge-023/paulo-custodio/perl/ch-1.pl
+++ b/challenge-023/paulo-custodio/perl/ch-1.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/env perl
+#!/usr/bin/perl
# Challenge 023
#
@@ -19,7 +19,7 @@
use Modern::Perl;
my($n, @seq) = @ARGV;
-say join(", ", nth_forward_diff($n, @seq)), ".";
+say join(", ", nth_forward_diff($n, @seq));
sub forward_diff {
diff --git a/challenge-023/paulo-custodio/perl/ch-2.pl b/challenge-023/paulo-custodio/perl/ch-2.pl
index 62f450ba8d..695e54128d 100644
--- a/challenge-023/paulo-custodio/perl/ch-2.pl
+++ b/challenge-023/paulo-custodio/perl/ch-2.pl
@@ -1,16 +1,17 @@
-#!/usr/bin/env perl
+#!/usr/bin/perl
# Challenge 023
#
# Task #2
# Create a script that prints Prime Decomposition of a given number. The prime
# decomposition of a number is defined as a list of prime numbers which when
-# all multiplied together, are equal to that number. For example, the Prime decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19.
+# all multiplied together, are equal to that number. For example, the Prime
+# decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19.
use Modern::Perl;
my($n) = @ARGV;
-say join(", ", prime_decomposition($n)), ".";
+say join(", ", prime_decomposition($n));
# check if number is prime
diff --git a/challenge-023/paulo-custodio/python/ch-1.py b/challenge-023/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..5baa57d74a
--- /dev/null
+++ b/challenge-023/paulo-custodio/python/ch-1.py
@@ -0,0 +1,32 @@
+#!/usr/bin/python3
+
+# Challenge 023
+#
+# Task #1
+# Create a script that prints nth order forward difference series. You should
+# be a able to pass the list of numbers and order number as command line
+# parameters. Let me show you with an example.
+#
+# Suppose we have list (X) of numbers: 5, 9, 2, 8, 1, 6 and we would like to
+# create 1st order forward difference series (Y). So using the formula
+# Y(i) = X(i+1) - X(i), we get the following numbers:
+# (9-5), (2-9), (8-2), (1-8), (6-1).
+# In short, the final series would be: 4, -7, 6, -7, 5.
+# If you noticed, it has one less number than the original series.
+# Similarly you can carry on 2nd order forward difference series like:
+# (-7-4), (6+7), (-7-6), (5+7) => -11, 13, -13, 12.
+
+import sys
+
+def forward_diff(seq):
+ return [seq[i+1]-seq[i] for i in range(len(seq)-1)]
+
+def nth_forward_diff(n ,seq):
+ for i in range(n):
+ seq = forward_diff(seq)
+ return seq
+
+n = int(sys.argv[1])
+seq = [int(x) for x in sys.argv[2:]]
+seq = nth_forward_diff(n ,seq)
+print(", ".join([str(x) for x in seq]))
diff --git a/challenge-023/paulo-custodio/python/ch-2.py b/challenge-023/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..4438d1af8a
--- /dev/null
+++ b/challenge-023/paulo-custodio/python/ch-2.py
@@ -0,0 +1,38 @@
+#!/usr/bin/python3
+
+# Challenge 023
+#
+# Task #2
+# Create a script that prints Prime Decomposition of a given number. The prime
+# decomposition of a number is defined as a list of prime numbers which when
+# all multiplied together, are equal to that number. For example, the Prime
+# decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19.
+
+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 prime_decomposition(n):
+ if n<2:
+ return [n]
+
+ f = []
+ p = 2
+ while n>1:
+ if n%p == 0:
+ f.append(p)
+ n //= p
+ else:
+ p = next_prime(p)
+ return f
+
+f = prime_decomposition(int(sys.argv[1]))
+print(", ".join([str(x) for x in f]))
diff --git a/challenge-023/paulo-custodio/t/test-1.yaml b/challenge-023/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..d93bef9607
--- /dev/null
+++ b/challenge-023/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 1 5 9 2 8 1 6
+ input:
+ output: 4, -7, 6, -7, 5
+- setup:
+ cleanup:
+ args: 2 5 9 2 8 1 6
+ input:
+ output: -11, 13, -13, 12
+- setup:
+ cleanup:
+ args: 1 4 -7 6 -7 5
+ input:
+ output: -11, 13, -13, 12
diff --git a/challenge-023/paulo-custodio/t/test-2.yaml b/challenge-023/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..f94852e4f0
--- /dev/null
+++ b/challenge-023/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,55 @@
+- setup:
+ cleanup:
+ args: 1
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 2
+ input:
+ output: 2
+- setup:
+ cleanup:
+ args: 3
+ input:
+ output: 3
+- setup:
+ cleanup:
+ args: 4
+ input:
+ output: 2, 2
+- setup:
+ cleanup:
+ args: 5
+ input:
+ output: 5
+- setup:
+ cleanup:
+ args: 6
+ input:
+ output: 2, 3
+- setup:
+ cleanup:
+ args: 7
+ input:
+ output: 7
+- setup:
+ cleanup:
+ args: 8
+ input:
+ output: 2, 2, 2
+- setup:
+ cleanup:
+ args: 9
+ input:
+ output: 3, 3
+- setup:
+ cleanup:
+ args: 10
+ input:
+ output: 2, 5
+- setup:
+ cleanup:
+ args: 228
+ input:
+ output: 2, 2, 3, 19
diff --git a/challenge-023/paulo-custodio/test.pl b/challenge-023/paulo-custodio/test.pl
deleted file mode 100644
index bc90aec76b..0000000000
--- a/challenge-023/paulo-custodio/test.pl
+++ /dev/null
@@ -1,30 +0,0 @@
-#!/usr/bin/perl
-
-use Modern::Perl;
-use Test::More;
-use Path::Tiny;
-
-is capture("perl perl/ch-1.pl 1 5 9 2 8 1 6"), "4, -7, 6, -7, 5.\n";
-is capture("perl perl/ch-1.pl 2 5 9 2 8 1 6"), "-11, 13, -13, 12.\n";
-is capture("perl perl/ch-1.pl 1 4 -7 6 -7 5"), "-11, 13, -13, 12.\n";
-
-is capture("perl perl/ch-2.pl 1"), "1.\n";
-is capture("perl perl/ch-2.pl 2"), "2.\n";
-is capture("perl perl/ch-2.pl 3"), "3.\n";
-is capture("perl perl/ch-2.pl 4"), "2, 2.\n";
-is capture("perl perl/ch-2.pl 5"), "5.\n";
-is capture("perl perl/ch-2.pl 6"), "2, 3.\n";
-is capture("perl perl/ch-2.pl 7"), "7.\n";
-is capture("perl perl/ch-2.pl 8"), "2, 2, 2.\n";
-is capture("perl perl/ch-2.pl 9"), "3, 3.\n";
-is capture("perl perl/ch-2.pl 10"), "2, 5.\n";
-is capture("perl perl/ch-2.pl 228"), "2, 2, 3, 19.\n";
-
-done_testing;
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \t\v\f\r]*\n/\n/g;
- return $out;
-}
diff --git a/challenge-024/paulo-custodio/perl/ch-1.pl b/challenge-024/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-024/paulo-custodio/perl/ch-1.pl
diff --git a/challenge-142/paulo-custodio/perl/ch-1.pl b/challenge-142/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..76a8e19638
--- /dev/null
+++ b/challenge-142/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+
+# TASK #1 > Divisor Last Digit
+# Submitted by: Mohammad S Anwar
+# You are given positive integers, $m and $n.
+#
+# Write a script to find total count of divisors of $m having last digit $n.
+#
+#
+# Example 1:
+# Input: $m = 24, $n = 2
+# Output: 2
+#
+# The divisors of 24 are 1, 2, 3, 4, 6, 8 and 12.
+# There are only 2 divisors having last digit 2 are 2 and 12.
+#
+# Example 2:
+# Input: $m = 30, $n = 5
+# Output: 2
+#
+# The divisors of 30 are 1, 2, 3, 5, 6, 10 and 15.
+# There are only 2 divisors having last digit 5 are 5 and 15.
+
+use Modern::Perl;
+
+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);
+}
+
+my($m, $n) = @ARGV;
+my @divisors = divisors($m);
+my $count = scalar grep {/$n$/} @divisors;
+say $count;
diff --git a/challenge-142/paulo-custodio/perl/ch-2.pl b/challenge-142/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..83160b0a28
--- /dev/null
+++ b/challenge-142/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+
+# TASK #2 > Sleep Sort
+# Submitted by: Adam Russell
+# Another joke sort similar to JortSort suggested by champion Adam Russell.
+#
+# You are given a list of numbers.
+#
+# Write a script to implement Sleep Sort. For more information, please checkout
+# this post.
+
+use Modern::Perl;
+use Config;
+use threads;
+
+sub sleeper {
+ my($n) = @_;
+ sleep $n;
+ say $n;
+}
+
+my @thrs;
+push @thrs, threads->create(\&sleeper, $_) for @ARGV;
+$_->join() for @thrs;
diff --git a/challenge-142/paulo-custodio/python/ch-1.py b/challenge-142/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..d142b7d24f
--- /dev/null
+++ b/challenge-142/paulo-custodio/python/ch-1.py
@@ -0,0 +1,42 @@
+#!/usr/bin/python3
+
+# TASK #1 > Divisor Last Digit
+# Submitted by: Mohammad S Anwar
+# You are given positive integers, $m and $n.
+#
+# Write a script to find total count of divisors of $m having last digit $n.
+#
+#
+# Example 1:
+# Input: $m = 24, $n = 2
+# Output: 2
+#
+# The divisors of 24 are 1, 2, 3, 4, 6, 8 and 12.
+# There are only 2 divisors having last digit 2 are 2 and 12.
+#
+# Example 2:
+# Input: $m = 30, $n = 5
+# Output: 2
+#
+# The divisors of 30 are 1, 2, 3, 5, 6, 10 and 15.
+# There are only 2 divisors having last digit 5 are 5 and 15.
+
+import sys
+import math
+import re
+
+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]
+
+m = int(sys.argv[1])
+n = int(sys.argv[2])
+count = len(list(filter(lambda x: re.search(str(n)+"$", str(x)), divisors(m))))
+print(count)
diff --git a/challenge-142/paulo-custodio/python/ch-2.py b/challenge-142/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..31f6f67abe
--- /dev/null
+++ b/challenge-142/paulo-custodio/python/ch-2.py
@@ -0,0 +1,26 @@
+#!/usr/bin/python3
+
+# TASK #2 > Sleep Sort
+# Submitted by: Adam Russell
+# Another joke sort similar to JortSort suggested by champion Adam Russell.
+#
+# You are given a list of numbers.
+#
+# Write a script to implement Sleep Sort. For more information, please checkout
+# this post.
+
+import sys
+import threading
+from time import sleep
+
+def sleeper(n):
+ sleep(n)
+ print(n)
+
+thrs = []
+for n in [int(x) for x in sys.argv[1:]]:
+ thr = threading.Thread(target=sleeper, args=[n])
+ thrs.append(thr)
+ thr.start()
+for thr in thrs:
+ thr.join()
diff --git a/challenge-142/paulo-custodio/t/test-1.yaml b/challenge-142/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..73afb6c3bd
--- /dev/null
+++ b/challenge-142/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 24 2
+ input:
+ output: 2
+- setup:
+ cleanup:
+ args: 30 5
+ input:
+ output: 2
diff --git a/challenge-142/paulo-custodio/t/test-2.yaml b/challenge-142/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..5880ec1526
--- /dev/null
+++ b/challenge-142/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,14 @@
+- setup:
+ cleanup:
+ args: 1 9 3 2 8 6 7 5 4
+ input:
+ output: |
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9