From d6dbddeb6611d3008519fb53dc6943c8286c31b7 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 23 Feb 2017 19:51:04 -0800 Subject: [PATCH 1/3] corpse code and print statements. --- src/count_bits.py | 2 -- src/parenthetics.py | 85 +++++++++++++++++++++++++++++++++++++++++++++ src/sort_cards.py | 52 +++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 src/parenthetics.py create mode 100644 src/sort_cards.py diff --git a/src/count_bits.py b/src/count_bits.py index f131316..e49dcb9 100644 --- a/src/count_bits.py +++ b/src/count_bits.py @@ -3,7 +3,5 @@ def count_bits(n): """Convert a number to binary and counts the number of 1 bits.""" - print(n) val = bin(n) - print(val) return val.count("1") diff --git a/src/parenthetics.py b/src/parenthetics.py new file mode 100644 index 0000000..d4c4c53 --- /dev/null +++ b/src/parenthetics.py @@ -0,0 +1,85 @@ +"""Implementation of the Kata Proper Parenthetics.""" + + +def parenthetics(uni_string): + """Take unicode string as input and return value.""" + stack = Stack() + charac_array = list(uni_string) + for charac in charac_array: + if charac == '(': + stack.push(charac) + elif charac == ')': + if stack.size() == 0: + return -1 + else: + stack.pop() + if stack.size() == 0: + return 0 + elif stack.size() > 0: + return 1 + + +class Stack(object): + """Create stack of parenthetics.""" + + def __init__(self): + """Create a new stack, from LinkedList using composition.""" + self._linkedlist = LinkedList() + + def push(self, value): + """Push a new value on top of the stack.""" + self._linkedlist.push(value) + + def size(self): + """Return side of a.""" + return self._linkedlist.size() + + def pop(self): + """Pop the first value of the stack.""" + return self._linkedlist.pop() + + +"""Python implementation of a linked list.""" + + +class Node(): + """Instantiate a Node.""" + + def __init__(self, value=None, next=None): + """Instantiate a node with value and next params.""" + self.value = value + self.next = next + + +class LinkedList(): + """Instantiate a Linked List.""" + + def __init__(self): + """Instantiate an empty Linked list.""" + self.head = None + + def push(self, val): + """Push a new node as the head of the linked list.""" + new_node = Node(val, self.head) + self.head = new_node + + def pop(self): + """Pop first value off linked list and return value.""" + # if self.head is None: + if self.head is not None: + pop_head = self.head.value + self.head = self.head.next + return pop_head + else: + raise IndexError('cannot pop from empty list') + + def size(self): + """Return the length of the linked list.""" + if self.head is not None: + size = 1 + curr = self.head + while curr.next is not None: + size += 1 + curr = curr.next + return size + return 0 diff --git a/src/sort_cards.py b/src/sort_cards.py new file mode 100644 index 0000000..1adc41d --- /dev/null +++ b/src/sort_cards.py @@ -0,0 +1,52 @@ +"""Implementation of the Sort Cards Kata.""" + +class PriorityQueue(object): + """Priority list queue.""" + + def __init__(self, iterable=None): + """Construct priority queue.""" + self.queues = {} + self.num_items = 0 + if iterable is not None: + for data, priority in iterable: + self.insert(data, priority) + + def length(self): + """Give length of card deck.""" + return self.num_items + + def insert(self, data, priority=0): + """Test insert into pq.""" + self.num_items+1 + if priority in self.queues: + self.queues[priority].insert(0, data) + else: + self.queues[priority] = [data] + + def pop(self): + """Test pop from pq.""" + self.length-1 + for priority in sorted(self.queues): + if len(self.queues[priority]) > 0: + return self.queues[priority].pop() + raise IndexError('Cannot pop from empty priority queue.') + + def peek(self): + """Peek at the highest priority tuple.""" + for priority in sorted(self.queues): + if len(self.queues[priority]) > 0: + return self.queues[priority][-1] + +def sort_cards(cards): + """Sorted Cards Priority list queue.""" + sort_dict = {'A': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'T': 10, 'J': 11, 'Q': 12, 'K': 13} + dec = PriorityQueue() + + for card in cards: + dec.insert(card, sort_dict[card]) + + sorted_deck = [] + while dec.length() > 0: + pop_card = dec.pop() + sorted_deck.push(pop_card) + return sorted_deck From c1329d3f407a841b01fe1418ead18e12163a4a20 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 23 Feb 2017 19:51:59 -0800 Subject: [PATCH 2/3] .gitignore --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index ab13cef..72364f9 100644 --- a/.gitignore +++ b/.gitignore @@ -87,5 +87,3 @@ ENV/ # Rope project settings .ropeproject - -bin/ From 3f4e4527534c6574c5565cd90d5277f8587e2774 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 23 Feb 2017 19:53:26 -0800 Subject: [PATCH 3/3] minor syntax fix --- src/sort_cards.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/sort_cards.py b/src/sort_cards.py index 1adc41d..c4631c5 100644 --- a/src/sort_cards.py +++ b/src/sort_cards.py @@ -1,5 +1,6 @@ """Implementation of the Sort Cards Kata.""" + class PriorityQueue(object): """Priority list queue.""" @@ -17,7 +18,7 @@ def length(self): def insert(self, data, priority=0): """Test insert into pq.""" - self.num_items+1 + self.num_items + 1 if priority in self.queues: self.queues[priority].insert(0, data) else: @@ -25,7 +26,7 @@ def insert(self, data, priority=0): def pop(self): """Test pop from pq.""" - self.length-1 + self.length - 1 for priority in sorted(self.queues): if len(self.queues[priority]) > 0: return self.queues[priority].pop() @@ -37,6 +38,7 @@ def peek(self): if len(self.queues[priority]) > 0: return self.queues[priority][-1] + def sort_cards(cards): """Sorted Cards Priority list queue.""" sort_dict = {'A': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'T': 10, 'J': 11, 'Q': 12, 'K': 13}