diff options
Diffstat (limited to 'challenge-068/lubos-kolouch/python')
| -rw-r--r-- | challenge-068/lubos-kolouch/python/ch-1.py | 29 | ||||
| -rw-r--r-- | challenge-068/lubos-kolouch/python/ch-2.py | 45 |
2 files changed, 74 insertions, 0 deletions
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 |
