diff --git a/.gitignore b/.gitignore index 92cd3f7..a901a34 100644 --- a/.gitignore +++ b/.gitignore @@ -89,5 +89,3 @@ ENV/ # Rope project settings .ropeproject - -bin/ diff --git a/README.md b/README.md index 82dbc43..bdb62cd 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Repo containing my solutions from Code War katas. +Repo containing my solutions from Code Katas. ###8kyu @@ -140,4 +140,15 @@ def find_it(xs): ``` def countBits(n): return bin(n).count("1") -``` \ No newline at end of file +``` + + +###Other + + +####Interview Challenge: Proper Parenthetics +- Module: proper-parenthetics.py +- TESTS: test_proper-parenthetics.py +- LINKS: None. +- DERIVED FROM: [data-structures repo. Specifically stack branch](https://github.com/ellezv/data_structures/tree/stack), collaborated with Maelle Vance. + diff --git a/src/count_bits.py b/src/count_bits.py index f131316..a58e73f 100644 --- a/src/count_bits.py +++ b/src/count_bits.py @@ -1,9 +1,8 @@ -"""Implementation of the Bit Counting Kata.""" + +"""Implemntation of the Bit Counting Kata.""" 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/tests/test_parenthetics.py b/src/tests/test_parenthetics.py new file mode 100644 index 0000000..ae87c75 --- /dev/null +++ b/src/tests/test_parenthetics.py @@ -0,0 +1,28 @@ +"""Tests for parenthetics module.""" + + +import pytest + + +PAREN_TABLE = [ + ['((()))', 0], + ['((())', 1], + [')))(((', -1] +] + + +@pytest.mark.parametrize("uni_string, result", PAREN_TABLE) +def test_parenthetics(uni_string, result): + """Test the parenthetics function.""" + from parenthetics import parenthetics + assert parenthetics(uni_string) == result + + +def test_pop_empty(): + """Test the parenthetics function.""" + from parenthetics import LinkedList + list = LinkedList() + try: + list.pop() + except Exception as e: + assert str(e) == 'cannot pop from empty list'