aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-16 15:50:22 +0100
committerGitHub <noreply@github.com>2023-05-16 15:50:22 +0100
commitc2627db4cc5ba2df9c76189901d439bd20bfcbf4 (patch)
treef187a1bfd45a621a4dc6a1cc033b77c62fbe0455
parentdcef53a5d2200928fdcd05504867da75d03c2f36 (diff)
parent0cbb2f6950487fbb5c37180c54c9a3df66d85f7f (diff)
downloadperlweeklychallenge-club-c2627db4cc5ba2df9c76189901d439bd20bfcbf4.tar.gz
perlweeklychallenge-club-c2627db4cc5ba2df9c76189901d439bd20bfcbf4.tar.bz2
perlweeklychallenge-club-c2627db4cc5ba2df9c76189901d439bd20bfcbf4.zip
Merge pull request #8087 from LubosKolouch/master
feat(challenge-217/lubos-kolouch/[perl,python]/ch-[12].p[ly]): Challenge 217 LK Perl Python
-rw-r--r--challenge-068/lubos-kolouch/perl/ch-1.pl40
-rw-r--r--challenge-068/lubos-kolouch/perl/ch-2.pl81
-rw-r--r--challenge-068/lubos-kolouch/python/ch-1.py29
-rw-r--r--challenge-068/lubos-kolouch/python/ch-2.py45
-rw-r--r--challenge-069/lubos-kolouch/perl/ch-1.pl23
-rw-r--r--challenge-069/lubos-kolouch/perl/ch-2.pl15
-rw-r--r--challenge-069/lubos-kolouch/python/ch-1.py21
-rw-r--r--challenge-069/lubos-kolouch/python/ch-2.py13
-rw-r--r--challenge-217/lubos-kolouch/perl/ch-1.pl18
-rw-r--r--challenge-217/lubos-kolouch/perl/ch-2.pl15
-rw-r--r--challenge-217/lubos-kolouch/python/ch-1.py16
-rw-r--r--challenge-217/lubos-kolouch/python/ch-2.py15
12 files changed, 331 insertions, 0 deletions
diff --git a/challenge-068/lubos-kolouch/perl/ch-1.pl b/challenge-068/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..f3318d9f45
--- /dev/null
+++ b/challenge-068/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,40 @@
+use strict;
+use warnings;
+use Data::Dumper;
+
+sub zero_matrix {
+ my ($matrix) = @_;
+ my @zero_rows;
+ my @zero_cols;
+
+ # Identify rows and columns that should be zeroed out
+ for my $i (0 .. scalar @$matrix - 1) {
+ for my $j (0 .. scalar @{$matrix->[$i]} - 1) {
+ if ($matrix->[$i][$j] == 0) {
+ push @zero_rows, $i;
+ push @zero_cols, $j;
+ }
+ }
+ }
+
+ # Zero out identified rows and columns
+ for my $i (@zero_rows) {
+ for my $j (0 .. scalar @{$matrix->[$i]} - 1) {
+ $matrix->[$i][$j] = 0;
+ }
+ }
+
+ for my $j (@zero_cols) {
+ for my $i (0 .. scalar @$matrix - 1) {
+ $matrix->[$i][$j] = 0;
+ }
+ }
+
+ return $matrix;
+}
+
+my $matrix1 = [[1, 0, 1], [1, 1, 1], [1, 1, 1]];
+my $matrix2 = [[1, 0, 1], [1, 1, 1], [1, 0, 1]];
+print Dumper(zero_matrix($matrix1));
+print Dumper(zero_matrix($matrix2));
+
diff --git a/challenge-068/lubos-kolouch/perl/ch-2.pl b/challenge-068/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..bd0e0eedb2
--- /dev/null
+++ b/challenge-068/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,81 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+# Node definition
+sub new_node {
+ my ($value) = @_;
+ return { value => $value, next => undef };
+}
+
+# Insert node to the list
+sub insert_node {
+ my ($head, $value) = @_;
+ my $node = new_node($value);
+ $node->{next} = $head;
+ return $node;
+}
+
+# Reverse list
+sub reverse_list {
+ my ($head) = @_;
+ my $prev = undef;
+ while ($head) {
+ my $next = $head->{next};
+ $head->{next} = $prev;
+ $prev = $head;
+ $head = $next;
+ }
+ return $prev;
+}
+
+# Merge two lists
+sub merge {
+ my ($head1, $head2) = @_;
+ my $head = new_node(0); # dummy node
+ my $node = $head;
+ while ($head1 && $head2) {
+ $node->{next} = $head1;
+ $head1 = $head1->{next};
+ $node = $node->{next};
+ $node->{next} = $head2;
+ $head2 = $head2->{next};
+ $node = $node->{next};
+ }
+ $node->{next} = $head1 if $head1;
+ return $head->{next};
+}
+
+# Reorder list
+sub reorder_list {
+ my ($head) = @_;
+ # Find the middle of the list
+ my $slow = $head;
+ my $fast = $head;
+ while ($fast && $fast->{next}) {
+ $slow = $slow->{next};
+ $fast = $fast->{next}{next};
+ }
+ # Reverse the second half of the list
+ my $head2 = reverse_list($slow->{next});
+ $slow->{next} = undef;
+ # Merge two lists
+ return merge($head, $head2);
+}
+
+# Test the function
+my $head = undef;
+for my $value (reverse 1..4) {
+ $head = insert_node($head, $value);
+}
+
+$head = reorder_list($head);
+
+# Print the reordered list
+my $node = $head;
+while ($node) {
+ print $node->{value}, " ";
+ $node = $node->{next};
+}
+print "\n";
+
diff --git a/challenge-068/lubos-kolouch/python/ch-1.py b/challenge-068/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..d7c4fb0afc
--- /dev/null
+++ b/challenge-068/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+def zero_matrix(matrix):
+ m = len(matrix)
+ n = len(matrix[0])
+ zero_rows = set()
+ zero_cols = set()
+
+ # Identify rows and columns that should be zeroed out
+ for i in range(m):
+ for j in range(n):
+ if matrix[i][j] == 0:
+ zero_rows.add(i)
+ zero_cols.add(j)
+
+ # Zero out identified rows and columns
+ for i in range(m):
+ for j in range(n):
+ if i in zero_rows or j in zero_cols:
+ matrix[i][j] = 0
+
+ return matrix
+
+
+matrix1 = [[1, 0, 1], [1, 1, 1], [1, 1, 1]]
+matrix2 = [[1, 0, 1], [1, 1, 1], [1, 0, 1]]
+print(zero_matrix(matrix1))
+print(zero_matrix(matrix2))
diff --git a/challenge-068/lubos-kolouch/python/ch-2.py b/challenge-068/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..7f73a59f9b
--- /dev/null
+++ b/challenge-068/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+class Node:
+ def __init__(self, x):
+ self.val = x
+ self.next = None
+
+
+def reorderList(head):
+ # Find the middle of the linked list
+ slow = fast = head
+ while fast and fast.next:
+ slow = slow.next
+ fast = fast.next.next
+
+ # Reverse the second half of the linked list
+ prev = None
+ while slow:
+ next_node = slow.next
+ slow.next = prev
+ prev = slow
+ slow = next_node
+
+ # Merge the first half and the reversed second half
+ first_half = head
+ second_half = prev
+ while second_half.next:
+ first_half.next, first_half = second_half, first_half.next
+ second_half.next, second_half = first_half, second_half.next
+
+ return head
+
+
+# Test the function
+head = Node(1)
+head.next = Node(2)
+head.next.next = Node(3)
+head.next.next.next = Node(4)
+reordered_list = reorderList(head)
+
+node = reordered_list
+while node:
+ print(node.val, end=" ")
+ node = node.next
diff --git a/challenge-069/lubos-kolouch/perl/ch-1.pl b/challenge-069/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..1fe61abdb1
--- /dev/null
+++ b/challenge-069/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,23 @@
+use strict;
+use warnings;
+use feature 'say';
+use List::Util 'all';
+
+sub is_strobogrammatic {
+ my $n = shift;
+ my %strobo_pairs = ('0' => '0', '1' => '1', '6' => '9', '8' => '8', '9' => '6');
+ my @str_n = split //, $n;
+ return all { $strobo_pairs{$str_n[$_]} eq $str_n[-$_-1] } 0..@str_n/2;
+}
+
+sub find_strobogrammatic {
+ my ($A, $B) = @_;
+ return grep {is_strobogrammatic($_)} $A..$B;
+}
+
+# test cases to validate the solution
+
+use Test::More;
+is_deeply([find_strobogrammatic(50, 100)], [69, 88, 96], 'Test Case 1');
+done_testing();
+
diff --git a/challenge-069/lubos-kolouch/perl/ch-2.pl b/challenge-069/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..54529d0196
--- /dev/null
+++ b/challenge-069/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,15 @@
+use strict;
+use warnings;
+use feature 'say';
+
+sub generate_string {
+ my $n = shift;
+ return "" if $n == 0;
+ return "0" if $n == 1;
+ my $s_n_1 = generate_string($n - 1);
+ my $reversed_switched = join "", map { $_ eq '0' ? '1' : '0' } reverse split //, $s_n_1;
+ return $s_n_1 . "0" . $reversed_switched;
+}
+
+say generate_string(30);
+
diff --git a/challenge-069/lubos-kolouch/python/ch-1.py b/challenge-069/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..b5e736fa47
--- /dev/null
+++ b/challenge-069/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from typing import List
+
+
+def is_strobogrammatic(n: int) -> bool:
+ """Check if a number is strobogrammatic."""
+ strobo_pairs = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}
+ str_n = str(n)
+ return all(strobo_pairs.get(i) == j for i, j in zip(str_n, str_n[::-1]))
+
+
+def find_strobogrammatic(A: int, B: int) -> List[int]:
+ """Find all strobogrammatic numbers in a given range."""
+ return [n for n in range(A, B+1) if is_strobogrammatic(n)]
+
+
+# test cases to validate the solution
+
+assert (find_strobogrammatic(50, 100) == [69, 88, 96])
diff --git a/challenge-069/lubos-kolouch/python/ch-2.py b/challenge-069/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..5efef48bb8
--- /dev/null
+++ b/challenge-069/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,13 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+def generate_string(n: int) -> str:
+ if n == 0:
+ return ""
+ if n == 1:
+ return "0"
+ s_n_1 = generate_string(n - 1)
+ return s_n_1 + "0" + "".join('0' if c == '1' else '1' for c in s_n_1[::-1])
+
+
+print(generate_string(30))
diff --git a/challenge-217/lubos-kolouch/perl/ch-1.pl b/challenge-217/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..0fae6d6897
--- /dev/null
+++ b/challenge-217/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,18 @@
+use strict;
+use warnings;
+
+sub third_smallest {
+ my @matrix = @_;
+ my @flattened = map { @$_ } @matrix;
+ @flattened = sort { $a <=> $b } @flattened;
+ return $flattened[2];
+}
+
+my @matrix1 = ([3, 1, 2], [5, 2, 4], [0, 1, 3]);
+my @matrix2 = ([2, 1], [4, 5]);
+my @matrix3 = ([1, 0, 3], [0, 0, 0], [1, 2, 1]);
+
+print third_smallest(@matrix1), "\n";
+print third_smallest(@matrix2), "\n";
+print third_smallest(@matrix3), "\n";
+
diff --git a/challenge-217/lubos-kolouch/perl/ch-2.pl b/challenge-217/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..63441a1796
--- /dev/null
+++ b/challenge-217/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,15 @@
+use strict;
+use warnings;
+
+sub highest_value {
+ my @nums = @_;
+ @nums = sort { $b . $a cmp $a . $b } @nums;
+ return join '', @nums;
+}
+
+print highest_value(1, 23), "\n";
+print highest_value(10, 3, 2), "\n";
+print highest_value(31, 2, 4, 10), "\n";
+print highest_value(5, 11, 4, 1, 2), "\n";
+print highest_value(1, 10), "\n";
+
diff --git a/challenge-217/lubos-kolouch/python/ch-1.py b/challenge-217/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..3dc8b355e2
--- /dev/null
+++ b/challenge-217/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+def third_smallest(matrix):
+ flattened = [elem for sublist in matrix for elem in sublist]
+ flattened.sort()
+ return flattened[2]
+
+
+matrix1 = [[3, 1, 2], [5, 2, 4], [0, 1, 3]]
+matrix2 = [[2, 1], [4, 5]]
+matrix3 = [[1, 0, 3], [0, 0, 0], [1, 2, 1]]
+
+print(third_smallest(matrix1))
+print(third_smallest(matrix2))
+print(third_smallest(matrix3))
diff --git a/challenge-217/lubos-kolouch/python/ch-2.py b/challenge-217/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..d1ae89e84d
--- /dev/null
+++ b/challenge-217/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,15 @@
+from functools import cmp_to_key
+
+
+def highest_value(nums):
+ nums = map(str, nums) # convert numbers to strings
+ result = ''.join(sorted(nums, key=cmp_to_key(
+ lambda x, y: 1 if y+x > x+y else -1)))
+ return int(result)
+
+
+print(highest_value([1, 23]))
+print(highest_value([10, 3, 2]))
+print(highest_value([31, 2, 4, 10]))
+print(highest_value([5, 11, 4, 1, 2]))
+print(highest_value([1, 10]))