aboutsummaryrefslogtreecommitdiff
path: root/challenge-059/lubos-kolouch/python
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-04-16 15:24:13 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-04-16 15:24:13 +0200
commit8091d154c159948b012ce9728cbd8942ea3d148c (patch)
tree11224395d1559dd0e7df4e865fbf0c132fd16a10 /challenge-059/lubos-kolouch/python
parentfc7f30eaf4f1b81354ea7a10b3240efccf28a084 (diff)
downloadperlweeklychallenge-club-8091d154c159948b012ce9728cbd8942ea3d148c.tar.gz
perlweeklychallenge-club-8091d154c159948b012ce9728cbd8942ea3d148c.tar.bz2
perlweeklychallenge-club-8091d154c159948b012ce9728cbd8942ea3d148c.zip
Challenge 057 059 LK Perl Python
Diffstat (limited to 'challenge-059/lubos-kolouch/python')
-rw-r--r--challenge-059/lubos-kolouch/python/ch-1.py69
-rw-r--r--challenge-059/lubos-kolouch/python/ch-2.py25
2 files changed, 94 insertions, 0 deletions
diff --git a/challenge-059/lubos-kolouch/python/ch-1.py b/challenge-059/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..e463f94a8a
--- /dev/null
+++ b/challenge-059/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+class LinkedList:
+ def __init__(self, value):
+ self.value = value
+ self.next = None
+
+ def append(self, value):
+ current = self
+ while current.next:
+ current = current.next
+ current.next = LinkedList(value)
+
+ def partition(self, k):
+ less_head = None
+ greater_head = None
+ less_tail = None
+ greater_tail = None
+
+ current = self
+ while current:
+ if current.value < k:
+ if less_tail:
+ less_tail.next = current
+ else:
+ less_head = current
+ less_tail = current
+ else:
+ if greater_tail:
+ greater_tail.next = current
+ else:
+ greater_head = current
+ greater_tail = current
+ current = current.next
+
+ if less_tail:
+ less_tail.next = greater_head
+ else:
+ less_head = greater_head
+
+ if greater_tail:
+ greater_tail.next = None
+
+ return less_head
+
+ def __str__(self):
+ current = self
+ result = []
+ while current:
+ result.append(str(current.value) + " → ")
+ current = current.next
+ result.append("END")
+ return "".join(result)
+
+
+if __name__ == "__main__":
+ linked_list = LinkedList(1)
+ linked_list.append(4)
+ linked_list.append(3)
+ linked_list.append(2)
+ linked_list.append(5)
+ linked_list.append(2)
+
+ print("Original list:", linked_list)
+ k_val = 3
+ partitioned_list = linked_list.partition(k_val)
+ print(f"Partitioned list with k={k_val}:", partitioned_list)
+
diff --git a/challenge-059/lubos-kolouch/python/ch-2.py b/challenge-059/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..f482a546d6
--- /dev/null
+++ b/challenge-059/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from typing import List
+
+
+def count_different_bits(a: int, b: int) -> int:
+ count = 0
+ while a > 0 or b > 0:
+ count += (a % 2) ^ (b % 2)
+ a >>= 1
+ b >>= 1
+ return count
+
+def sum_different_bits(numbers: List[int]) -> int:
+ total = 0
+ for i in range(len(numbers)):
+ for j in range(i+1, len(numbers)):
+ total += count_different_bits(numbers[i], numbers[j])
+ return total
+
+if __name__ == "__main__":
+ input_list = [2, 3, 4]
+ result = sum_different_bits(input_list)
+ print(result)