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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,24 @@ 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


Sort Cards (7kyu)
-Module: sort_cards.py
-Tests: test_sort_cards.py
-Link: https://www.codewars.com/kata/sort-deck-of-cards/train/python
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
py_modules=["last", "string_to_array", "fake_binary",
"count_positives_sum_negatives", "digitize",
"binary_list_to_number", "sea_sick", "reverse_and_mirror",
"flatten_me", "sum_from_string", "multiples"],
"flatten_me", "sum_from_string", "multiples", "sort_cards",
"priority"],
install_requires=[],
extras_require={
"test": ["pytest", "pytest-watch", "pytest-cov", "tox"]
Expand Down
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.
2 changes: 2 additions & 0 deletions src/code_katas.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ src/digitize.py
src/flatten_me.py
src/last.py
src/multiples.py
src/priority.py
src/reverse_and_mirror.py
src/sea_sick.py
src/sort_cards.py
src/string_to_array.py
src/sum_from_string.py
src/code_katas.egg-info/PKG-INFO
Expand Down
2 changes: 2 additions & 0 deletions src/code_katas.egg-info/top_level.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ fake_binary
flatten_me
last
multiples
priority
reverse_and_mirror
sea_sick
sort_cards
string_to_array
sum_from_string
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.
40 changes: 40 additions & 0 deletions src/priority.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Priority Queue data structure."""


class PriorityQueue(object):
"""Priority Queue data structure.

.insert(item) - adds an item to the queue and sorts
.pop() - removes the highest priority item, returns its value.
.peek() - returns the value of the highest priority item.
"""

def __init__(self, iterable=None):
"""Initialize the Priority Queue."""
self._pq_dict = {}
if hasattr(iterable, "__iter__"):
for val in iterable:
self.insert(val[1], priority=val[0])

def insert(self, value, priority=0):
"""Insert values and priorities in to priority queue."""
if priority not in self._pq_dict.keys():
self._pq_dict[priority] = []
self._pq_dict[priority].append(value)

def pop(self):
"""Remove the highest priority item, returns its value."""
pop_val = self.peek()
hi_pri = sorted(self._pq_dict.keys())[0]
if len(self._pq_dict[hi_pri]) == 1:
del self._pq_dict[hi_pri]
else:
self._pq_dict[hi_pri] = self._pq_dict[hi_pri][1:]
return pop_val

def peek(self):
if len(self._pq_dict.keys()) == 0:
raise KeyError("The Priority Queue is empty.")
hi_pri = sorted(self._pq_dict.keys())[0]
pop_val = self._pq_dict[hi_pri][0]
return pop_val
18 changes: 18 additions & 0 deletions src/proper_parenthetics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""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 == '(':
parenthetics_stack.push(i)
if i == ')':
if parenthetics_stack.head_node is None:
return -1
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.
49 changes: 49 additions & 0 deletions src/sort_cards.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Sort a deck of cards code-kata using a Priority Queue."""

from priority import PriorityQueue

card_dict = {
'A': 1,
'T': 10,
'J': 11,
'Q': 12,
'K': 13,
1: 'A',
10: 'T',
11: 'J',
12: 'Q',
13: 'K',
}


def sort_cards(cards):
"""Sort shuffled list of cards, sorted by rank."""
card_list = []
for card in cards:
try:
card_list.append(int(card))
except ValueError:
card_list.append(card_dict[card])
card_list.sort()
result = []
for card in card_list:
if card > 1 and card < 10:
result.append(str(card))
else:
result.append(card_dict[card])
return result


def sort_cards_w_pq(cards):
"""Sort shuffled list of cards, sorted by rank, using a priority queue."""
card_list = []
for card in cards:
try:
card_list.append((int(card), card))
except ValueError:
card_list.append(card_dict[card])
card_q = PriorityQueue(card_list)
result = []
for i in range(len(card_list)):
result.append(card_q.pop())
return result
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.
69 changes: 69 additions & 0 deletions src/test_priority.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""Test the priority queue's functionality."""


import pytest

INPUT_VALUES = [
([], {}, None, None),
([(1, 'a'), (2, 'b'), (3, 'c')], {1: ['a'], 2: ['b'], 3: ['c']}, 'a', 1),
([(2, 'a'), (5, 'b'), (12, 'c')], {2: ['a'], 5: ['b'], 12: ['c']}, 'a', 2),
([(15, 'words'), (6, 'are'), (70, 'hard')], {6: ['are'], 15: ['words'], 70: ['hard']}, 'are', 6),
([(1, 15), (2, 20), (3, 25)], {1: [15], 2: [20], 3: [25]}, 15, 1),
([(1, 'a'), (1, 'b'), (2, 'c')], {1: ['a', 'b'], 2: ['c']}, 'a', 1),
]


@pytest.fixture(scope="module", params=INPUT_VALUES)
def pri(request):
"""Paramaterized fixture for testing."""
from priority import PriorityQueue
processed = PriorityQueue(request.param[0])
prioq = (processed, request.param[1], request.param[2], request.param[3])
return prioq


def test_init(pri):
"""Test that the class initializes."""
from priority import PriorityQueue
assert isinstance(pri[0], PriorityQueue)


def test_insert(pri):
"""Test that values are inserted at the right priority in the right order."""
assert pri[0]._pq_dict == pri[1]
# (the_input, the_expected_output) = pri


def test_pop(pri):
"""Test that pop returns the right value."""
try:
assert pri[0].pop() == pri[2]
except KeyError:
assert True


def test_del(pri):
"""Test that empty keys are deleted."""
try:
pri[0].pop()
pri[0].pop()
assert pri[3] not in pri[0]._pq_dict
except KeyError:
assert True


def test_pop_remove(pri):
"""Test that pop properly removes values."""
try:
pri[0].pop()
assert pri[2] not in pri[0]._pq_dict[pri[3]]
except KeyError:
assert True


def test_peek(pri):
"""Test that peek returns the right value."""
try:
assert pri[0].peek() == pri[2]
except KeyError:
assert True
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
26 changes: 26 additions & 0 deletions src/test_sort_cards.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Tests for count_positives_sum_negatives.count_positives_sum_negatives."""
import pytest


ASSERTIONS = [
[list('39A5T824Q7J6K'), list('A23456789TJQK')],
[list('Q286JK395A47T'), list('A23456789TJQK')],
[list('54TQKJ69327A8'), list('A23456789TJQK')],
[list('AAA4555336773'), list('AAA3334555677')],
[list('3'), list('3')],
[list('TTTTTTTTTTTAK'), list('ATTTTTTTTTTTK')],
]


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


@pytest.mark.parametrize("n, result", ASSERTIONS)
def test_sort_cards_w_pq(n, result):
"""Test sort_cards_w_pq() for proper output in test cases."""
from sort_cards import sort_cards
assert sort_cards(n) == result