aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-05-16 15:17:54 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-05-16 15:17:54 +0200
commitf6acc228ec94edeeb17e950d00d2831733bb421f (patch)
tree1d0ae4d28a2d9af0f58c6e2c837adf4b736a4191
parentb7b029f85595734938066ed2efefded43aacf260 (diff)
downloadperlweeklychallenge-club-f6acc228ec94edeeb17e950d00d2831733bb421f.tar.gz
perlweeklychallenge-club-f6acc228ec94edeeb17e950d00d2831733bb421f.tar.bz2
perlweeklychallenge-club-f6acc228ec94edeeb17e950d00d2831733bb421f.zip
feat(challenge-068/lubos-kolouch/p[erl,ython]/ch[1,2].p[ly]): Challenge 068 LK Python Perl
-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
4 files changed, 195 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