aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-03-14 18:52:38 +0100
committerLubos Kolouch <lubos@kolouch.net>2023-03-14 18:52:38 +0100
commitf8fccce87c49ef9d9e5da19d23e4c8549ff024c9 (patch)
tree66611b8d21077569f77a5662a82402611d8358c2
parent0f4a809c0bead5e3fd8bc4616e1c011c9d6fb8a9 (diff)
downloadperlweeklychallenge-club-f8fccce87c49ef9d9e5da19d23e4c8549ff024c9.tar.gz
perlweeklychallenge-club-f8fccce87c49ef9d9e5da19d23e4c8549ff024c9.tar.bz2
perlweeklychallenge-club-f8fccce87c49ef9d9e5da19d23e4c8549ff024c9.zip
Challenge 208 LK Perl Python Bash
-rw-r--r--challenge-208/lubos-kolouch/bash/ch-2.sh30
-rw-r--r--challenge-208/lubos-kolouch/perl/ch-1.pl60
-rw-r--r--challenge-208/lubos-kolouch/perl/ch-2.pl41
-rw-r--r--challenge-208/lubos-kolouch/python/ch-1.py58
-rw-r--r--challenge-208/lubos-kolouch/python/ch-2.py52
5 files changed, 241 insertions, 0 deletions
diff --git a/challenge-208/lubos-kolouch/bash/ch-2.sh b/challenge-208/lubos-kolouch/bash/ch-2.sh
new file mode 100644
index 0000000000..dcf3b2fe89
--- /dev/null
+++ b/challenge-208/lubos-kolouch/bash/ch-2.sh
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+# Define the input array
+nums=(1 2 2 4)
+
+# Find the duplicate and missing integers
+n=${#nums[@]}
+missing=0
+duplicate=0
+for (( i=0; i<n; i++ )); do
+ index=${nums[i]#-}
+ if (( nums[index-1] > 0 )); then
+ nums[index-1]=-${nums[index-1]}
+ else
+ duplicate=$index
+ fi
+done
+for (( i=0; i<n; i++ )); do
+ if (( nums[i] > 0 )); then
+ missing=$((i+1))
+ fi
+done
+
+# Print the results
+if (( missing != 0 && duplicate != 0 )); then
+ echo "Duplicate is $duplicate and missing is $missing."
+else
+ echo "-1"
+fi
+
diff --git a/challenge-208/lubos-kolouch/perl/ch-1.pl b/challenge-208/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..be8ede0ef2
--- /dev/null
+++ b/challenge-208/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+
+sub find_common_strings {
+ my ($list1_ref, $list2_ref) = @_;
+
+ my @list1 = @$list1_ref;
+ my @list2 = @$list2_ref;
+
+ my %common;
+ my $min_index_sum = scalar(@list1) + scalar(@list2);
+
+ for (my $i = 0; $i < scalar(@list1); $i++) {
+ for (my $j = 0; $j < scalar(@list2); $j++) {
+ if ($list1[$i] eq $list2[$j]) {
+ my $index_sum = $i + $j;
+ if ($index_sum <= $min_index_sum) {
+ $min_index_sum = $index_sum;
+ $common{$list1[$i]} = $index_sum;
+ }
+ }
+ }
+ }
+
+ if (scalar(keys %common) == 0) {
+ return ();
+ } else {
+ my @result;
+ foreach my $key (keys %common) {
+ if ($common{$key} == $min_index_sum) {
+ push(@result, $key);
+ }
+ }
+ return @result;
+ }
+}
+
+# Example 1
+my @list1_1 = ("Perl", "Raku", "Love");
+my @list2_1 = ("Raku", "Perl", "Hate");
+my @common_1 = sort find_common_strings(\@list1_1, \@list2_1);
+is_deeply(\@common_1, ["Raku"], "Example 1");
+
+# Example 2
+my @list1_2 = ("A", "B", "C");
+my @list2_2 = ("D", "E", "F");
+my @common_2 = find_common_strings(\@list1_2, \@list2_2);
+is_deeply(\@common_2, [], "Example 2");
+
+# Example 3
+my @list1_3 = ("A", "B", "C");
+my @list2_3 = ("C", "A", "B");
+my @common_3 = find_common_strings(\@list1_3, \@list2_3);
+is_deeply(\@common_3, ["A"], "Example 3");
+
+done_testing();
+
diff --git a/challenge-208/lubos-kolouch/perl/ch-2.pl b/challenge-208/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..d88f83245d
--- /dev/null
+++ b/challenge-208/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,41 @@
+use strict;
+use warnings;
+use Test::More;
+
+sub find_missing_and_duplicate {
+ my @nums = @_;
+ my %count;
+ my $missing = 0;
+ my $duplicate = 0;
+ foreach my $num (@nums) {
+ $count{$num}++;
+ if ($count{$num} > 1) {
+ $duplicate = $num;
+ }
+ }
+ for (my $i = 1; $i <= scalar(@nums) + 1; $i++) {
+ if (!$count{$i}) {
+ $missing = $i;
+ last;
+ }
+ }
+ if ($missing && $duplicate) {
+ return [$duplicate, $missing];
+ } else {
+ return -1;
+ }
+}
+
+# Run the tests
+
+# Test 1: find_missing_and_duplicate returns the correct output for input (1,2,2,4)
+is_deeply(find_missing_and_duplicate(1,2,2,4), [2,3], "Test 1 passed");
+
+# Test 2: find_missing_and_duplicate returns the correct output for input (1,2,3,4)
+is(find_missing_and_duplicate(1,2,3,4), -1, "Test 2 passed");
+
+# Test 3: find_missing_and_duplicate returns the correct output for input (1,2,3,3)
+is_deeply(find_missing_and_duplicate(1,2,3,3), [3,4], "Test 3 passed");
+
+done_testing();
+
diff --git a/challenge-208/lubos-kolouch/python/ch-1.py b/challenge-208/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..f29f86e8a7
--- /dev/null
+++ b/challenge-208/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from typing import List
+
+
+def find_common_strings(list1: List[str], list2: List[str]) -> List[str]:
+ """
+ Finds all common strings in the given two arrays with minimum index sum.
+ If no common strings are found, returns an empty list.
+ Args:
+ list1: The first list of strings
+ list2: The second list of strings
+ Returns:
+ A list of common strings with minimum index sum, or an empty list if no common strings are found.
+ """
+ common = {}
+ min_index_sum = len(list1) + len(list2)
+ for i, elem1 in enumerate(list1):
+ for j, elem2 in enumerate(list2):
+ if elem1 == elem2:
+ index_sum = i + j
+ if index_sum <= min_index_sum:
+ min_index_sum = index_sum
+ common[list1[i]] = index_sum
+
+ result = []
+ for key in common.keys():
+ if common[key] == min_index_sum:
+ result.append(key)
+
+ return result
+
+
+# Tests
+
+
+def test_find_common_strings():
+ list1_1 = ["Perl", "Raku", "Love"]
+ list2_1 = ["Raku", "Perl", "Hate"]
+ common_1 = find_common_strings(list1_1, list2_1)
+ assert common_1 == ["Perl", "Raku"]
+
+ list1_2 = ["A", "B", "C"]
+ list2_2 = ["D", "E", "F"]
+ common_2 = find_common_strings(list1_2, list2_2)
+ assert common_2 == []
+
+ list1_3 = ["A", "B", "C"]
+ list2_3 = ["C", "A", "B"]
+ common_3 = find_common_strings(list1_3, list2_3)
+ assert common_3 == ["A"]
+
+ print("All tests pass")
+
+
+if __name__ == "__main__":
+ test_find_common_strings()
diff --git a/challenge-208/lubos-kolouch/python/ch-2.py b/challenge-208/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..38f78d4ba9
--- /dev/null
+++ b/challenge-208/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,52 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from typing import List, Tuple, Union
+
+
+def find_missing_and_duplicate(nums: List[int]) -> Union[Tuple[int, int], int]:
+ """
+ Finds the duplicate and missing integer in a given sequence of integers.
+
+ Args:
+ nums (List[int]): A list of integers with one missing and one duplicate.
+
+ Returns:
+ Union[Tuple[int, int], int]: If both a missing and duplicate integer are found, returns a tuple
+ containing the duplicate integer followed by the missing integer. If none are found, returns -1.
+
+ Example:
+ >>> find_missing_and_duplicate([1, 2, 2, 4])
+ (2, 3)
+ >>> find_missing_and_duplicate([1, 2, 3, 4])
+ -1
+ >>> find_missing_and_duplicate([1, 2, 3, 3])
+ (3, 4)
+ """
+ count = {}
+ missing = 0
+ duplicate = 0
+ for num in nums:
+ count[num] = count.get(num, 0) + 1
+ if count[num] > 1:
+ duplicate = num
+ for i in range(1, len(nums) + 2):
+ if i not in count:
+ missing = i
+ break
+ if missing and duplicate:
+ return (duplicate, missing)
+ else:
+ return -1
+
+
+# Run the tests
+
+
+def test_find_missing_and_duplicate():
+ assert find_missing_and_duplicate([1, 2, 2, 4]) == (2, 3)
+ assert find_missing_and_duplicate([1, 2, 3, 4]) == -1
+ assert find_missing_and_duplicate([1, 2, 3, 3]) == (3, 4)
+
+
+test_find_missing_and_duplicate()