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 @@ -89,5 +89,3 @@ ENV/

# Rope project settings
.ropeproject

bin/
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Repo containing my solutions from Code War katas.
Repo containing my solutions from Code Katas.

###8kyu

Expand Down Expand Up @@ -140,4 +140,15 @@ def find_it(xs):
```
def countBits(n):
return bin(n).count("1")
```
```


###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.

5 changes: 2 additions & 3 deletions src/count_bits.py
Original file line number Diff line number Diff line change
@@ -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")
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why turn this into a list if strings are iterable in both Python 2 and 3?

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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this elif

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
28 changes: 28 additions & 0 deletions src/tests/test_parenthetics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Tests for parenthetics module."""


import pytest


PAREN_TABLE = [
['((()))', 0],
['((())', 1],
[')))(((', -1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need more test cases.

]


@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'