From 1e1739da53b6ecbe5f17fa6cf3b922ad6ff5d347 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sun, 15 Sep 2024 13:03:59 +0100 Subject: Add Python solution to challenge 059 --- challenge-059/paulo-custodio/python/ch-1.py | 114 ++++++++++++++++++++++++++++ challenge-059/paulo-custodio/python/ch-2.py | 42 ++++++++++ 2 files changed, 156 insertions(+) create mode 100644 challenge-059/paulo-custodio/python/ch-1.py create mode 100644 challenge-059/paulo-custodio/python/ch-2.py (limited to 'challenge-059/paulo-custodio/python') diff --git a/challenge-059/paulo-custodio/python/ch-1.py b/challenge-059/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..2451591238 --- /dev/null +++ b/challenge-059/paulo-custodio/python/ch-1.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python3 + +# Challenge 059 +# +# TASK #1 › Linked List +# Reviewed by Ryan Thompson +# You are given a linked list and a value k. Write a script to partition the +# linked list such that all nodes less than k come before nodes greater than or +# equal to k. Make sure you preserve the original relative order of the nodes in +# each of the two partitions. +# +# For example: +# +# Linked List: 1 -> 4 -> 3 -> 2 -> 5 -> 2 +# +# k = 3 +# +# Expected Output: 1 -> 2 -> 2 -> 4 -> 3 -> 5. + +import sys + +class Node: + def __init__(self, data): + self.data = data + self.next = None + + def __str__(self): + out = "["+str(self.data) + if self.next: + return out+","+str(self.next)+"]" + else: + return out+"]" + +class LinkedList: + def __init__(self): + self.head = None + + def __str__(self): + out = "" + current = self.head + while current: + out += str(current.data)+" -> " + current = current.next + out += "None" + return out + + def push_front(self, data): + new_node = Node(data) + new_node.next = self.head + self.head = new_node + + def push_back(self, data): + new_node = Node(data) + if not self.head: + self.head = new_node + else: + last_node = self.head + while last_node.next: + last_node = last_node.next + last_node.next = new_node + + def insert_at(self, index, data): + if index == 0: + self.push_front(data) + else: + new_node = Node(data) + current = self.head + for _ in range(index - 1): + if not current: + raise IndexError("Index out of bounds") + current = current.next + new_node.next = current.next + current.next = new_node + + def remove_node(self, key): + current = self.head + if current and current.data == key: + self.head = current.next + current = None + else: + prev = None + while current and current.data != key: + prev = current + current = current.next + if current: + prev.next = current.next + current = None + +def partition(list, k): + below = LinkedList() + above = LinkedList() + + p = list.head + while p: + if p.data < k: + below.push_back(p.data) + else: + above.push_back(p.data) + p = p.next + + list = below + p = above.head + while p: + list.push_back(p.data) + p = p.next + + return list + +k = int(sys.argv[1]) +list = LinkedList() +for x in sys.argv[2:]: + list.push_back(int(x)) +list = partition(list, k) +print(list) diff --git a/challenge-059/paulo-custodio/python/ch-2.py b/challenge-059/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..89d4f6741d --- /dev/null +++ b/challenge-059/paulo-custodio/python/ch-2.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +# Challenge 059 +# +# TASK #2 > Bit Sum +# Reviewed by Ryan Thompson +# Helper Function +# For this task, you will most likely need a function f(a,b) which returns the +# count of different bits of binary representation of a and b. +# +# For example, f(1,3) = 1, since: +# +# Binary representation of 1 = 01 +# +# Binary representation of 3 = 11 +# +# There is only 1 different bit. Therefore the subroutine should return 1. Note +# that if one number is longer than the other in binary, the most significant +# bits of the smaller number are padded (i.e., they are assumed to be zeroes). +# +# Script Output +# You script should accept n positive numbers. Your script should sum the result +# of f(a,b) for every pair of numbers given: +# +# For example, given 2, 3, 4, the output would be 6, +# since f(2,3) + f(2,4) + f(3,4) = 1 + 2 + 3 = 6 + +import sys +from itertools import combinations + +def f(a, b): + r = int(a) ^ int(b) + rt = bin(r) + return rt.count('1') + +n = list(map(int, sys.argv[1:])) +sum_ = 0 + +for combin in combinations(n, 2): + sum_ += f(combin[0], combin[1]) + +print(sum_) -- cgit