aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-18 21:10:57 +0000
committerGitHub <noreply@github.com>2023-03-18 21:10:57 +0000
commit71e907b5f05ddb891af9b2a3137bf1d0270c8adc (patch)
tree66b4b9fdd965d8b5e9fe7e5f384c700b10b24a77
parent0561437f19e42aab4a2d47934cfc94b83b6f632b (diff)
parent4e2e50d29d3dfdcc15613ab3259f206681304ed1 (diff)
downloadperlweeklychallenge-club-71e907b5f05ddb891af9b2a3137bf1d0270c8adc.tar.gz
perlweeklychallenge-club-71e907b5f05ddb891af9b2a3137bf1d0270c8adc.tar.bz2
perlweeklychallenge-club-71e907b5f05ddb891af9b2a3137bf1d0270c8adc.zip
Merge pull request #7730 from LubosKolouch/master
Challenge 208 LK Perl Python Bash
-rw-r--r--challenge-011/lubos-kolouch/perl/ch-1.pl15
-rw-r--r--challenge-011/lubos-kolouch/perl/ch-2.pl22
-rw-r--r--challenge-011/lubos-kolouch/python/ch-1.py10
-rw-r--r--challenge-011/lubos-kolouch/python/ch-2.py12
-rw-r--r--challenge-012/lubos-kolouch/perl/ch-1.pl25
-rw-r--r--challenge-012/lubos-kolouch/perl/ch-2.pl23
-rw-r--r--challenge-012/lubos-kolouch/python/ch-1.py25
-rw-r--r--challenge-012/lubos-kolouch/python/ch-2.py18
-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
13 files changed, 391 insertions, 0 deletions
diff --git a/challenge-011/lubos-kolouch/perl/ch-1.pl b/challenge-011/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..fae93c59ab
--- /dev/null
+++ b/challenge-011/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,15 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+my $fahrenheit;
+my $celsius;
+
+for ( $fahrenheit = -100 ; $fahrenheit <= 212 ; $fahrenheit++ ) {
+ $celsius = ( $fahrenheit - 32 ) * 5 / 9;
+ if ( $fahrenheit == $celsius ) {
+ print
+"The equal point in the Fahrenheit and Celsius scales is $fahrenheit °F and $celsius °C.\n";
+ last;
+ }
+}
diff --git a/challenge-011/lubos-kolouch/perl/ch-2.pl b/challenge-011/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..5a6165c080
--- /dev/null
+++ b/challenge-011/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+my $size = 4; # change this to the desired size
+my @identity_matrix;
+
+for my $i ( 0 .. $size - 1 ) {
+ for my $j ( 0 .. $size - 1 ) {
+ if ( $i == $j ) {
+ $identity_matrix[$i][$j] = 1;
+ }
+ else {
+ $identity_matrix[$i][$j] = 0;
+ }
+ }
+}
+
+# print the identity matrix
+for my $row (@identity_matrix) {
+ print "@$row\n";
+}
diff --git a/challenge-011/lubos-kolouch/python/ch-1.py b/challenge-011/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..21e817e97c
--- /dev/null
+++ b/challenge-011/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,10 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+for fahrenheit in range(-100, 213):
+ celsius = (fahrenheit - 32) * 5 / 9
+ if fahrenheit == celsius:
+ print(
+ f"The equal point in the Fahrenheit and Celsius scales is {fahrenheit} °F and {celsius} °C."
+ )
+ break
diff --git a/challenge-011/lubos-kolouch/python/ch-2.py b/challenge-011/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..442bd8d82a
--- /dev/null
+++ b/challenge-011/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,12 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+size = 4 # change this to the desired size
+identity_matrix = [[0 for x in range(size)] for y in range(size)]
+
+for i in range(size):
+ identity_matrix[i][i] = 1
+
+# print the identity matrix
+for row in identity_matrix:
+ print(row)
diff --git a/challenge-012/lubos-kolouch/perl/ch-1.pl b/challenge-012/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..5fd044a666
--- /dev/null
+++ b/challenge-012/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub is_prime {
+ my $n = shift;
+ return 0 if $n < 2;
+ for ( my $i = 2 ; $i <= sqrt($n) ; $i++ ) {
+ return 0 if ( $n % $i == 0 );
+ }
+ return 1;
+}
+
+my $p = 2;
+my $euclid_number = 3;
+
+while ( is_prime($euclid_number) ) {
+ $p++;
+ while ( !is_prime($p) ) {
+ $p++;
+ }
+ $euclid_number = ( 2**( $p - 1 ) ) * ( ( 2**$p ) - 1 );
+}
+
+print "The smallest Euclid number that is not prime is: ", $euclid_number, "\n";
diff --git a/challenge-012/lubos-kolouch/perl/ch-2.pl b/challenge-012/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..af21886dc0
--- /dev/null
+++ b/challenge-012/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,23 @@
+use strict;
+use warnings;
+
+my @paths = ( "/a/b/c/d", "/a/b/cd", "/a/b/cc", "/a/b/c/d/e" );
+my $separator = "/";
+
+my @common_path;
+for my $path (@paths) {
+ my @parts = split( $separator, $path );
+ if ( !@common_path ) {
+ @common_path = @parts;
+ }
+ else {
+ for ( my $i = 0 ; $i < @common_path ; $i++ ) {
+ if ( $common_path[$i] ne $parts[$i] ) {
+ splice( @common_path, $i );
+ last;
+ }
+ }
+ }
+}
+
+print "The common directory path is: ", join( $separator, @common_path ), "\n";
diff --git a/challenge-012/lubos-kolouch/python/ch-1.py b/challenge-012/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..d7f2eb96f4
--- /dev/null
+++ b/challenge-012/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from math import sqrt
+
+
+def is_prime(n):
+ if n < 2:
+ return False
+ for i in range(2, int(sqrt(n)) + 1):
+ if n % i == 0:
+ return False
+ return True
+
+
+p = 2
+euclid_number = 3
+
+while is_prime(euclid_number):
+ p += 1
+ while not is_prime(p):
+ p += 1
+ euclid_number = (2 ** (p - 1)) * ((2**p) - 1)
+
+print(f"The smallest Euclid number that is not prime is: {euclid_number}")
diff --git a/challenge-012/lubos-kolouch/python/ch-2.py b/challenge-012/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..43788cde70
--- /dev/null
+++ b/challenge-012/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+paths = ["/a/b/c/d", "/a/b/cd", "/a/b/cc", "/a/b/c/d/e"]
+separator = "/"
+
+common_path = []
+for path in paths:
+ parts = path.split(separator)
+ if not common_path:
+ common_path = parts
+ else:
+ for i in range(len(common_path)):
+ if common_path[i] != parts[i]:
+ common_path = common_path[:i]
+ break
+
+print(f"The common directory path is: {separator.join(common_path)}")
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()