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
6 changes: 3 additions & 3 deletions Algorithms/disguised_sequence.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""A disguised sequence (I)"""

from helpers.test_wrapper import Test

import sys
sys.path.append('..')

from helpers.test_wrapper import Test


def fcn(n):
f, s = 1, 2
for i in range(2, n + 1):
for _ in range(2, n + 1):
nxt = (6 * f * s) // (5 * f - s)
f, s = s, nxt
return nxt
Expand Down
20 changes: 12 additions & 8 deletions Fundamentals/ease_the_stockbroker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@

URL: https://www.codewars.com/kata/ease-the-stockbroker
"""
from helpers.test_wrapper import Test
import re
import sys
sys.path.append('..')

from helpers.test_wrapper import Test


def balance_statement(strng):
prices, badly_formed = {'B': 0, 'S': 0}, []
for i, order in enumerate(strng.split(', ')):
for _, order in enumerate(strng.split(', ')):
# Quote /space/ Quantity /space/ Price /space/ Status
match = re.match(r'^([A-z0-9\.]+)\s(\d+)\s(\d*\.\d+)\s(B|S)$', order)
if match:
Expand All @@ -34,16 +33,21 @@ def balance_statement(strng):

def run_tests():
with Test() as test:
s = 'ZNGA 1300 2.66 B, CLH15.NYM 50 56.32 B, OWW 1000 11.623 B, OGG 20 580.1 B'
s = 'ZNGA 1300 2.66 B, CLH15.NYM 50 56.32 B, \
OWW 1000 11.623 B, OGG 20 580.1 B'
test.assert_equals(balance_statement(s), 'Buy: 29499 Sell: 0')
s = 'GOOG 300 542.93 B, CLH15.NYM 50 56.32 S, CSCO 250 29.46 B, OGG 20 580.1 B'
s = 'GOOG 300 542.93 B, CLH15.NYM 50 56.32 S, \
CSCO 250 29.46 B, OGG 20 580.1 B'
test.assert_equals(balance_statement(s), 'Buy: 181846 Sell: 2816')
test.assert_equals(balance_statement(''), 'Buy: 0 Sell: 0')
s = 'GOOG 300 542.0 B, AAPL 50 145.0 B, CSCO 250.0 29 B, GOOG 200 580.0 S'
expected = 'Buy: 169850 Sell: 116000; Badly formed 1: CSCO 250.0 29 B ;'
s = 'GOOG 300 542.0 B, AAPL 50 145.0 B, \
CSCO 250.0 29 B, GOOG 200 580.0 S'
expected = 'Buy: 169850 Sell: 116000; \
Badly formed 1: CSCO 250.0 29 B ;'
test.assert_equals(balance_statement(s), expected)
s = 'CAP 1300 .2 B, CLH16.NYM 50 56 S, OWW 1000 11 S, OGG 20 580.1 S'
expected = 'Buy: 260 Sell: 11602; Badly formed 2: CLH16.NYM 50 56 S ;OWW 1000 11 S ;'
expected = 'Buy: 260 Sell: 11602; Badly formed 2: \
CLH16.NYM 50 56 S ;OWW 1000 11 S ;'
test.assert_equals(balance_statement(s), expected)


Expand Down
5 changes: 2 additions & 3 deletions Fundamentals/generating_numbers_from_digits.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@
Number of tests: 100
Arrays of length between 30 and 100
"""
from helpers.test_wrapper import Test
from collections import Counter
from functools import reduce
from math import factorial
import operator as op
import sys
sys.path.append('..')

from helpers.test_wrapper import Test


def ncr(n, r):
r = min(r, n - r)
Expand All @@ -72,7 +71,7 @@ def proc_arr(arr):
diff = 1
unique = 0

for n, c in Counter(arr).most_common():
for _, c in Counter(arr).most_common():
if c == 1:
unique += 1
else:
Expand Down
7 changes: 4 additions & 3 deletions Fundamentals/two_to_one.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
longest(a, a) -> "abcdefghijklmnopqrstuvwxyz"
"""
import string
import time


def longest(s1, s2):
Expand Down Expand Up @@ -39,5 +38,7 @@ def longest2(a1, a2):

if __name__ == '__main__':
assert(longest("aretheyhere", "yestheyarehere") == "aehrsty")
assert(longest("loopingisfunbutdangerous", "lessdangerousthancoding") == "abcdefghilnoprstu")
assert(longest("inmanylanguages", "theresapairoffunctions") == "acefghilmnoprstuy")
assert(longest("loopingisfunbutdangerous",
"lessdangerousthancoding") == "abcdefghilnoprstu")
assert(longest("inmanylanguages", "theresapairoffunctions")
== "acefghilmnoprstuy")
22 changes: 11 additions & 11 deletions Games/mastermind.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

Rules

1. The Mastermind (computer) will select 4 colours. The colours are randomly selected
from ["Red", "Blue", "Green", "Orange", "Purple", "Yellow"]. Colours can be duplicated
but there will always be exactly 4.
2. The Mastermind will return an array back to you. For every correctly positioned
colour in the array an element of “Black” is returned. For every correct colour
but in the wrong position an element of “White” will be returned.
1. The Mastermind (computer) will select 4 colours. The colours are randomly
selected from ["Red", "Blue", "Green", "Orange", "Purple", "Yellow"].
Colours can be duplicated but there will always be exactly 4.
2. The Mastermind will return an array back to you. For every correctly
positioned colour in the array an element of “Black” is returned.
For every correct colour but in the wrong position an element of “White”
will be returned.
3. Passing the correct array will pass the Kata test.
4. Passing an invalid colour will fail the test with the error "Error: you have given
an invalid colour!"
4. Passing an invalid colour will fail the test with the error
"Error: you have given an invalid colour!"
5. Passing an invalid array length will fail the test with the error "Error: you
must pass 4 colours!"
6. Guessing more than 60 times will fail the test with the error "Error: you have
Expand Down Expand Up @@ -59,13 +60,12 @@ def mastermind(game):
print (answer)
Good luck and enjoy!
"""
from helpers.test_wrapper import Test
from itertools import permutations
from random import choice, shuffle
import sys
sys.path.append('..')

from helpers.test_wrapper import Test


class MasterMind(object):
"""Mastermind game"""
Expand Down Expand Up @@ -142,7 +142,7 @@ def run_tests():

with Test() as test:
test.describe('Mastermind randomly generated tests')
for i in range(10):
for _ in range(10):
solution = [choice(colors) for _ in range(4)]
game = HackedMastermind(solution)
test.assert_equals(mastermind(game), solution)
Expand Down
9 changes: 4 additions & 5 deletions helpers/test_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def _handle_failure(self, msg):

def _format_msg(self, a, b, msg):
if not msg:
# a, b = map(lambda x: list(x) if isinstance(x, Iterable) else x, [a, b])
# a, b = map(lambda x: list(x)
# if isinstance(x, Iterable) else x, [a, b])
msg = 'expect {} to equal {}'.format(a, b)
return msg

Expand All @@ -47,12 +48,10 @@ def _assert(self, func, a, b, msg):
self._handle_failure(msg)

def assert_equals(self, a, b, msg=None):
eq = lambda a, b: a == b
self._assert(eq, a, b, msg)
self._assert(lambda a, b: a == b, a, b, msg)

def assert_not_equals(self, a, b, msg=None):
neq = lambda a, b: a != b
self._assert(neq, a, b, msg)
self._assert(lambda a, b: a != b, a, b, msg)

def expect(self, expected, msg=None):
if not msg:
Expand Down