diff --git a/Algorithms/disguised_sequence.py b/Algorithms/disguised_sequence.py index 6b52bb3..a10252c 100644 --- a/Algorithms/disguised_sequence.py +++ b/Algorithms/disguised_sequence.py @@ -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 diff --git a/Fundamentals/ease_the_stockbroker.py b/Fundamentals/ease_the_stockbroker.py index 7a1cf56..8c0d3d9 100644 --- a/Fundamentals/ease_the_stockbroker.py +++ b/Fundamentals/ease_the_stockbroker.py @@ -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: @@ -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) diff --git a/Fundamentals/generating_numbers_from_digits.py b/Fundamentals/generating_numbers_from_digits.py index e3c6f45..a94950b 100644 --- a/Fundamentals/generating_numbers_from_digits.py +++ b/Fundamentals/generating_numbers_from_digits.py @@ -44,6 +44,7 @@ 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 @@ -51,8 +52,6 @@ import sys sys.path.append('..') -from helpers.test_wrapper import Test - def ncr(n, r): r = min(r, n - r) @@ -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: diff --git a/Fundamentals/two_to_one.py b/Fundamentals/two_to_one.py index 96f16c0..c184c03 100644 --- a/Fundamentals/two_to_one.py +++ b/Fundamentals/two_to_one.py @@ -11,7 +11,6 @@ longest(a, a) -> "abcdefghijklmnopqrstuvwxyz" """ import string -import time def longest(s1, s2): @@ -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") diff --git a/Games/mastermind.py b/Games/mastermind.py index c3d1858..4740083 100644 --- a/Games/mastermind.py +++ b/Games/mastermind.py @@ -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 @@ -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""" @@ -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) diff --git a/helpers/test_wrapper.py b/helpers/test_wrapper.py index 19d732f..93259a2 100644 --- a/helpers/test_wrapper.py +++ b/helpers/test_wrapper.py @@ -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 @@ -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: