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
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# code-katas
CodeWars Assignments for Snow Day.
Collection of Code Kata problems and tests. Most are from codewars.com and have links to the original problem and
examples of other solutions


Convert a String to an Array (8 kyu)
- Module:string_to_array.py
Expand Down Expand Up @@ -130,3 +132,18 @@ def multiples(s1, s2, s3):
return [a for a in xrange(1, s3) if not(a % s1 or a % s2)]
```

Sum of the first nth terms of a series (7 kyu)
- Module: series_sum.py
- Tests: test_series_sum.py
- Link: http://www.codewars.com/kata/sum-of-the-first-nth-term-of-series/train/python
- Interesting Solution: Making the numerator a float() was smart.
```python
def series_sum(n):
"""Solution by MMMAAANNN"""
return '{:.2f}'.format(sum(1.0/(3 * i + 1) for i in range(n)))
```


Proper Parenthetics
- Module: proper_parenthetics.py
- Tests: test_proper_parenthetics.py
Binary file modified src/__pycache__/test_binary_list_to_number.cpython-27-PYTEST.pyc
Binary file not shown.
Binary file not shown.
Binary file modified src/__pycache__/test_digitize.cpython-27-PYTEST.pyc
Binary file not shown.
Binary file modified src/__pycache__/test_fake_bin.cpython-27-PYTEST.pyc
Binary file not shown.
Binary file modified src/__pycache__/test_flatten_me.cpython-27-PYTEST.pyc
Binary file not shown.
Binary file modified src/__pycache__/test_last.cpython-27-PYTEST.pyc
Binary file not shown.
Binary file modified src/__pycache__/test_multiples.cpython-27-PYTEST.pyc
Binary file not shown.
Binary file modified src/__pycache__/test_reverse_and_mirror.cpython-27-PYTEST.pyc
Binary file not shown.
Binary file modified src/__pycache__/test_sea_sick.cpython-27-PYTEST.pyc
Binary file not shown.
Binary file modified src/__pycache__/test_string_to_array.cpython-27-PYTEST.pyc
Binary file not shown.
Binary file modified src/__pycache__/test_sum.cpython-27-PYTEST.pyc
Binary file not shown.
Binary file modified src/__pycache__/test_sum_from_string.cpython-27-PYTEST.pyc
Binary file not shown.
Binary file modified src/binary_list_to_number.pyc
Binary file not shown.
Binary file modified src/count_positives_sum_negatives.pyc
Binary file not shown.
Binary file modified src/digitize.pyc
Binary file not shown.
Binary file modified src/fake_bin.pyc
Binary file not shown.
Binary file modified src/flatten_me.pyc
Binary file not shown.
Binary file modified src/last.pyc
Binary file not shown.
83 changes: 83 additions & 0 deletions src/linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""Implementation of Linked_List data type."""


class LinkedList(object):
"""Class representation of linked list."""

def __init__(self, iterable=None):
"""Instantiate linked list."""
self.head_node = None
self.length = 0
try:
for item in iterable:
self.push(item)
except TypeError:
if iterable:
return "Please only enter iterable values"

def push(self, contents):
"""Add node to this linked list."""
self.head_node = Node(contents, self.head_node)
self.length += 1

def pop(self):
"""Remove and return the current head node."""
if not self.head_node:
return "Linked list is already empty"
old_head_node = self.head_node
self.head_node = self.head_node.next_node
self.length -= 1
return old_head_node.contents

def size(self):
"""Return the current size of this linked list."""
return self.length

def search(self, search_value):
"""Return the node with the searched contents if found."""
if self.length:
if search_value == self.head_node.contents:
return self.head_node
current_node = self.head_node
while current_node.contents != search_value:
if current_node.next_node is None:
return None
current_node = current_node.next_node
return current_node
else:
return None

def remove(self, remove_node):
"""Remove a node from linked list."""
if remove_node == self.head_node:
self.head_node = self.head_node.next_node
self.length -= 1
return None
elif remove_node is None:
raise ValueError("Provided value not in list.")
current_node = self.head_node
while current_node.next_node != remove_node:
current_node = current_node.next_node
current_node.next_node = current_node.next_node.next_node
self.length -= 1

def display(self):
"""Return the tuple of all values in linked list."""
if self.length == 0:
return None
else:
new_list = [self.head_node.contents]
current_node = self.head_node
while current_node.next_node is not None:
current_node = current_node.next_node
new_list.append(current_node.contents)
return tuple(new_list)


class Node(object):
"""Class representation of linked list node."""

def __init__(self, contents, next_node):
"""Instantiate linked list node."""
self.contents = contents
self.next_node = next_node
Binary file modified src/multiples.pyc
Binary file not shown.
19 changes: 19 additions & 0 deletions src/proper_parenthetics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Ensure the same number of opening and closing parenthetics in a string."""

from stack import Stack


def proper_parenthetics(input_string):
"""Ensure the balance of opening and closing parenthtics."""
parenthetics_stack = Stack()
for i in input_string:
if i == '(':
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Better variable names plz

parenthetics_stack.push(i)
if i == ')':
if parenthetics_stack.head_node is None:
return -1
else:
parenthetics_stack.pop()
if parenthetics_stack.head_node is None:
return 0
return 1
Binary file modified src/reverse_and_mirror.pyc
Binary file not shown.
Binary file modified src/sea_sick.pyc
Binary file not shown.
24 changes: 24 additions & 0 deletions src/stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Implementation of Stack data type."""

from linked_list import LinkedList


class Stack(object):
"""Class representation of a stack."""

def __init__(self, iterable=None):
"""Instantiate stack."""
self.linked_list = LinkedList(iterable)
self.length = self.linked_list.length
self.head_node = self.linked_list.head_node

def push(self, contents):
"""Add node to this stack."""
self.linked_list.push(contents)
self.head_node = self.linked_list.head_node

def pop(self):
"""Remove and return the current head node."""
old_head_node_value = self.linked_list.pop()
self.head_node = self.linked_list.head_node
return old_head_node_value
Binary file modified src/string_to_array.pyc
Binary file not shown.
Binary file modified src/sum_from_string.pyc
Binary file not shown.
21 changes: 21 additions & 0 deletions src/test_proper_parenthetics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Tests for proper_parenthetics."""
import pytest


ASSERTIONS = [
['(', 1],
['(This is (a string)', 1],
['()', 0],
['some (strings) and (others)', 0],
['((Nestes)ones)', 0],
[')', -1],
['(bloop))', -1],
[')its backwards(', -1]
]


@pytest.mark.parametrize("input, result", ASSERTIONS)
def test_proper_parenthetics(input, result):
"""Test proper_parenthetics for proper output in test cases."""
from proper_parenthetics import proper_parenthetics
assert proper_parenthetics(input) == result