aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-03-19 18:15:01 +0100
committerLubos Kolouch <lubos@kolouch.net>2023-03-19 18:15:01 +0100
commit45fbadb9777f592fa7836287a3f62dcab4d47e97 (patch)
tree621f17371d0d7b45320d8799119039653d47d851
parenta51682fbcc4462b56b273c4d4755be4981e40791 (diff)
downloadperlweeklychallenge-club-45fbadb9777f592fa7836287a3f62dcab4d47e97.tar.gz
perlweeklychallenge-club-45fbadb9777f592fa7836287a3f62dcab4d47e97.tar.bz2
perlweeklychallenge-club-45fbadb9777f592fa7836287a3f62dcab4d47e97.zip
Challenge 023 LK Perl Python
-rw-r--r--challenge-023/lubos-kolouch/perl/ch-1.pl28
-rw-r--r--challenge-023/lubos-kolouch/perl/ch-2.pl42
-rw-r--r--challenge-023/lubos-kolouch/python/ch-1.py74
-rw-r--r--challenge-023/lubos-kolouch/python/ch-2.py67
4 files changed, 211 insertions, 0 deletions
diff --git a/challenge-023/lubos-kolouch/perl/ch-1.pl b/challenge-023/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..f7b6e7cf06
--- /dev/null
+++ b/challenge-023/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+sub forward_difference {
+ my ($order, $nums_ref) = @_;
+
+ my @nums = @$nums_ref;
+
+ # Check if valid order is passed
+ die "Order should be a positive integer\n" if $order < 1;
+
+ my @diff = @nums;
+
+ # Calculate the nth order forward difference series
+ for (my $i = 1; $i <= $order; $i++) {
+ my @temp = ();
+ for (my $j = 0; $j < $#diff; $j++) {
+ push @temp, $diff[$j+1] - $diff[$j];
+ }
+ @diff = @temp;
+ }
+
+ # Return the final series
+ return @diff;
+}
+
diff --git a/challenge-023/lubos-kolouch/perl/ch-2.pl b/challenge-023/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..6409948d61
--- /dev/null
+++ b/challenge-023/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+# Subroutine to compute prime factors of a number
+sub prime_decomposition {
+ my $n = shift;
+
+ # Check if number is less than 2
+ if ($n < 2) {
+ die "Number should be greater than or equal to 2\n";
+ }
+
+ my @factors = ();
+ my $d = 2;
+
+ # Divide the number by prime factors
+ while ($n > 1) {
+ while ($n % $d == 0) {
+ push @factors, $d;
+ $n /= $d;
+ }
+ $d++;
+ if ($d * $d > $n && $n > 1) {
+ push @factors, $n;
+ last;
+ }
+ }
+
+ return @factors;
+}
+
+# Test cases
+is_deeply([prime_decomposition(228)], [2, 2, 3, 19], "prime decomposition of 228");
+is_deeply([prime_decomposition(131)], [131], "prime decomposition of 131");
+is_deeply([prime_decomposition(101)], [101], "prime decomposition of 101");
+
+done_testing();
+
+
diff --git a/challenge-023/lubos-kolouch/python/ch-1.py b/challenge-023/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..877379d121
--- /dev/null
+++ b/challenge-023/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,74 @@
+#!/usr/bin/env python
+
+import unittest
+import sys
+
+
+def forward_difference(order, nums):
+ # Check if valid order is passed
+ if order < 1:
+ raise ValueError("Order should be a positive integer")
+
+ diff = nums
+
+ # Calculate the nth order forward difference series
+ for i in range(order):
+ temp = []
+ for j in range(len(diff) - 1):
+ try:
+ temp.append(diff[j + 1] - diff[j])
+ except TypeError:
+ raise TypeError(f"not a number")
+ diff = temp
+
+ # Return the final series
+ return diff
+
+
+# Main code
+if __name__ == '__main__':
+ # Parse command line arguments
+ nums = []
+ for arg in sys.argv[1:-1]:
+ try:
+ nums.append(int(arg))
+ except ValueError:
+ raise ValueError(f"Invalid argument: {arg!r}")
+ order = int(sys.argv[-1])
+
+ # Calculate the nth order forward difference series
+ diff = forward_difference(order, nums)
+
+ # Print the final series
+ print(', '.join(map(str, diff)))
+
+# Tests
+
+
+class TestForwardDifference(unittest.TestCase):
+
+ def test_first_order(self):
+ nums = [5, 9, 2, 8, 1, 6]
+ expected_diff = [4, -7, 6, -7, 5]
+ actual_diff = forward_difference(1, nums)
+ self.assertListEqual(expected_diff, actual_diff)
+
+ def test_second_order(self):
+ nums = [5, 9, 2, 8, 1, 6]
+ expected_diff = [-11, 13, -13, 12]
+ actual_diff = forward_difference(2, nums)
+ self.assertListEqual(expected_diff, actual_diff)
+
+ def test_invalid_order(self):
+ nums = [5, 9, 2, 8, 1, 6]
+ with self.assertRaises(ValueError):
+ forward_difference(-1, nums)
+
+ def test_non_numeric_parameters(self):
+ nums = [5, 9, 2, 8, "foo", 6]
+ with self.assertRaises(TypeError):
+ forward_difference(2, nums)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/challenge-023/lubos-kolouch/python/ch-2.py b/challenge-023/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..87379a1b3a
--- /dev/null
+++ b/challenge-023/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import sys
+from typing import List
+
+
+def prime_decomposition(n: int) -> List[int]:
+ """
+ Compute the prime factors of a number.
+
+ Args:
+ n: An integer greater than or equal to 2.
+
+ Returns:
+ A list of prime factors of the input number.
+
+ Raises:
+ ValueError: If the input number is less than 2.
+ """
+ if n < 2:
+ raise ValueError("Number should be greater than or equal to 2")
+
+ factors = []
+ d = 2
+
+ # Divide the number by prime factors
+ while n > 1:
+ while n % d == 0:
+ factors.append(d)
+ n //= d
+ d += 1
+ if d * d > n and n > 1:
+ factors.append(n)
+ break
+
+ return factors
+
+
+# Test cases
+def test_prime_decomposition():
+ assert prime_decomposition(228) == [2, 2, 3, 19]
+ assert prime_decomposition(131) == [131]
+ assert prime_decomposition(101) == [101]
+ try:
+ prime_decomposition(1)
+ except ValueError:
+ pass
+ else:
+ assert False, "Expected ValueError for input 1"
+
+
+if __name__ == '__main__':
+ # Parse command line argument
+ if len(sys.argv) != 2:
+ print(f"Usage: {sys.argv[0]} <number>")
+ sys.exit(1)
+ n = int(sys.argv[1])
+
+ # Compute the prime decomposition
+ factors = prime_decomposition(n)
+
+ # Print the prime factors
+ print(", ".join(str(f) for f in factors))
+
+ # Run tests
+ test_prime_decomposition()