Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,3 @@ ENV/

# Rope project settings
.ropeproject

bin/
2 changes: 0 additions & 2 deletions src/count_bits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
85 changes: 85 additions & 0 deletions src/parenthetics.py
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions src/sort_cards.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""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