aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-06-30 02:45:11 +0100
committerGitHub <noreply@github.com>2025-06-30 02:45:11 +0100
commit1e6a4a6daaeb24c321de840c6b60d343c3167e7f (patch)
treee5b71de18391efd7e712bef51d55d37361bc94a4
parent286ac34a88c6baeaa1ef57cc1ef19b77c1d7a553 (diff)
parent204693e9ecff8f799dbcf84e3085d68f8537c0ff (diff)
downloadperlweeklychallenge-club-1e6a4a6daaeb24c321de840c6b60d343c3167e7f.tar.gz
perlweeklychallenge-club-1e6a4a6daaeb24c321de840c6b60d343c3167e7f.tar.bz2
perlweeklychallenge-club-1e6a4a6daaeb24c321de840c6b60d343c3167e7f.zip
Merge pull request #12251 from simongreen-net/master
sgreen solutions to challenge 327
-rw-r--r--challenge-327/sgreen/README.md4
-rwxr-xr-xchallenge-327/sgreen/perl/ch-1.pl21
-rwxr-xr-xchallenge-327/sgreen/perl/ch-2.pl27
-rwxr-xr-xchallenge-327/sgreen/python/ch-1.py27
-rwxr-xr-xchallenge-327/sgreen/python/ch-2.py37
-rwxr-xr-xchallenge-327/sgreen/python/test.py21
6 files changed, 135 insertions, 2 deletions
diff --git a/challenge-327/sgreen/README.md b/challenge-327/sgreen/README.md
index dd5a6d6624..81e0c1c823 100644
--- a/challenge-327/sgreen/README.md
+++ b/challenge-327/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 326
+# The Weekly Challenge 327
-Blog: [Blog](https://dev.to/simongreennet/weekly-challenge-3ngd)
+No blog this week.
diff --git a/challenge-327/sgreen/perl/ch-1.pl b/challenge-327/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..05bcd73f8f
--- /dev/null
+++ b/challenge-327/sgreen/perl/ch-1.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::MoreUtils 'first_index';
+
+sub main (@ints) {
+ my @missing = ();
+ for my $i (1 .. scalar(@ints)) {
+ if ((first_index { $_ == $i } @ints) == -1) {
+ push @missing, $i;
+ }
+ }
+
+ say '(' . join(', ', @missing) . ')';
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-327/sgreen/perl/ch-2.pl b/challenge-327/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..1cf804e9f3
--- /dev/null
+++ b/challenge-327/sgreen/perl/ch-2.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@ints) {
+ @ints = sort { $a <=> $b} @ints;
+
+ my $min_diff = undef;
+ my @min_diff_pairs = ();
+ foreach my $i (0 .. $#ints-1) {
+ my $diff = $ints[$i + 1] - $ints[$i];
+ if (not defined $min_diff or $diff < $min_diff) {
+ $min_diff = $diff;
+ @min_diff_pairs = ();
+ }
+ if ($diff == $min_diff) {
+ push @min_diff_pairs, [$ints[$i], $ints[$i + 1]];
+ }
+ }
+
+ say join(', ', map { "[$_->[0], $_->[1]]" } @min_diff_pairs);
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-327/sgreen/python/ch-1.py b/challenge-327/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..6c52c9dc2f
--- /dev/null
+++ b/challenge-327/sgreen/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def missing_integers(ints: list) -> list[int]:
+ """Find missing integers in a sequence.
+
+ Args:
+ ints (list): List of integers.
+
+ Returns:
+ list[int]: List of missing integers in the sequence.
+ """
+
+ return [i for i in range(1, len(ints) + 1) if i not in ints]
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = missing_integers(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-327/sgreen/python/ch-2.py b/challenge-327/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..02a0964aaf
--- /dev/null
+++ b/challenge-327/sgreen/python/ch-2.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def mad(ints: list) -> list[list[int]]:
+ """Find pairs of integers with the minimum absolute difference.
+
+ Args:
+ ints (list): List of integers.
+
+ Returns:
+ list[list[int]]: List of pairs of integers with the minimum absolute difference.
+ """
+
+ ints = sorted(ints)
+ min_diff = None
+ min_diff_pairs = []
+ for i in range(len(ints) - 1):
+ diff = ints[i + 1] - ints[i]
+ if min_diff is None or diff < min_diff:
+ min_diff = diff
+ min_diff_pairs = []
+ if diff == min_diff:
+ min_diff_pairs.append([ints[i], ints[i + 1]])
+
+ return min_diff_pairs
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = mad(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-327/sgreen/python/test.py b/challenge-327/sgreen/python/test.py
new file mode 100755
index 0000000000..816ecc6abb
--- /dev/null
+++ b/challenge-327/sgreen/python/test.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(ch_1.missing_integers([1, 2, 1, 3, 2, 5]), [4, 6])
+ self.assertEqual(ch_1.missing_integers([1, 1, 1]), [2, 3])
+ self.assertEqual(ch_1.missing_integers([2, 2, 1]), [3])
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.mad([4, 1, 2, 3]), [[1,2], [2,3], [3,4]])
+ self.assertEqual(ch_2.mad([1, 3, 7, 11, 15]), [[1,3]])
+ self.assertEqual(ch_2.mad([1, 5, 3, 8]), [[1,3], [3,5]])
+
+
+if __name__ == '__main__':
+ unittest.main()