def judge(testfield, expected = None): for testrow in testfield: print(testrow) actual = validate_battlefield(deepcopy(testfield)) if expected == None: expected = refvalidate_battlefield(testfield) Test.assert_equals(actual, expected)
def tests2(): NAMES = ("alien", "terminator", "predator", "lethal weapon", "die hard", "speed", "matrix", "john wick", "avatar", "avengers", "titanic", "dogma", "mallrats", "hellboy", "aquaman", "carrie", "apocalipsys now", "oscar", "cube", "solaris", "inception", "batman") def torrent_solution_(files): time = max(file.get("size_GB") * 8000 / file.get("speed_Mbps") for file in files) return [file.get("name") for file in sorted(files, key=lambda x: (x.get("size_GB") / x.get("speed_Mbps"), x.get("name")))], time for _ in range(100): # create list of n files n = randint(1, len(NAMES)) names = sample(NAMES, n) sizes = [randint(1, 100) for _ in range(n)] speeds = [randint(1, 100) for _ in range(n)] files = [{"name": name, "size_GB": size, "speed_Mbps": speed} for name, size, speed in zip(names, sizes, speeds)] # test.assert_equals(torrent(files), torrent_solution_(files)) list_files_solution, float_time_solution = torrent_solution_( files) # create expected result before calculating user's solution list_files, float_time = torrent(files) test.assert_equals(list_files, list_files_solution) test.assert_approx_equals(float_time, float_time_solution)
def tests(): NAMES_TIE = ("alien", "aliens", "alien 3", "alien resurection", "alien covenant", "alien prometheus", "alien vs predator", "alien vs predator 2", "die hard", "die hard 2", "die hard 3", "die hard 4", "die hard 5") NUMERIC_TIE = (40, 80, 160) def torrent_solution_(files): time = max(file.get("size_GB") * 8000 / file.get("speed_Mbps") for file in files) return [file.get("name") for file in sorted(files, key=lambda x: (x.get("size_GB") / x.get("speed_Mbps"), x.get("name")))], time for _ in range(100): # create list of n files n = randint(1, len(NAMES_TIE)) names = sample(NAMES_TIE, n) sizes = [choice(NUMERIC_TIE) for _ in range(n)] speeds = [choice(NUMERIC_TIE) for _ in range(n)] files = [{"name": name, "size_GB": size, "speed_Mbps": speed} for name, size, speed in zip(names, sizes, speeds)] # test.assert_equals(torrent(files), torrent_solution_(files)) list_files_solution, float_time_solution = torrent_solution_( files) # create expected result before calculating user's solution list_files, float_time = torrent(files) test.assert_equals(list_files, list_files_solution) test.assert_approx_equals(float_time, float_time_solution)
def it_1(): for _ in range(100): a, n = (R(0, 255), R(0, 255), R(0, 255)), R(1, 20) result, user = Solution(a, n), Potion(a, n) for _ in range(R(1, 5)): a, n = (R(0, 255), R(0, 255), R(0, 255)), R(1, 20) result, user = result.mix(Solution(a, n)), user.mix(Potion(a, n)) Test.assert_equals(user.color, result.color) Test.assert_equals(user.volume, result.volume)
def basic_test_cases(): test.assert_equals(create_phone_number([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]), "(123) 456-7890") test.assert_equals(create_phone_number([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]), "(123) 456-7890") test.assert_equals(create_phone_number([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), "(111) 111-1111") test.assert_equals(create_phone_number([0, 2, 3, 0, 5, 6, 0, 8, 9, 0]), "(023) 056-0890") test.assert_equals(create_phone_number([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), "(000) 000-0000")
def random(): for loop in range(100): chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ' inp_str = ''.join(choice(chars) for char in range(randint(1, 500))) par_num = randint(1, 25) for par in range(par_num): rand_ind = randint(0, len(inp_str) - 1) inp_str = inp_str[: rand_ind] + f"({''.join(choice(chars) for char in range(randint(0, 25)))})" + inp_str[ rand_ind:] test.assert_equals(remove_parentheses(inp_str[:]), correct(inp_str[:]))
def example_tests(): # test.assert_equals(asterisc_it(5312708), '531270*8') # test.assert_equals(asterisc_it(9682135), '96*8*2135') # test.assert_equals(asterisc_it(2222), '2*2*2*2') # test.assert_equals(asterisc_it(1111), '1111') # test.assert_equals(asterisc_it(9999), '9999') # test.assert_equals(asterisc_it('0000'), '0*0*0*0') # test.assert_equals(asterisc_it(8), '8') # test.assert_equals(asterisc_it(2), '2') # test.assert_equals(asterisc_it(0), '0') test.assert_equals(asterisc_it([1, 4, 64, 68, 67, 23, 1]), '14*6*4*6*8*67231')
def basic_test_cases(): tests = [ # [expected, input] ["01011110001100111", "45385593107843568"], ["101000111101101", "509321967506747"], ["011011110000101010000011011", "366058562030849490134388085"], ["01111100", "15889923"], ["100111001111", "800857237867"], ] for exp, inp in tests: test.assert_equals(fake_bin(inp), exp)
def basic_tests(): tests = [ (True, ([66, 101], 66)), (False, ([78, 117, 110, 99, 104, 117, 107, 115], 8)), (True, ([101, 45, 75, 105, 99, 107], 107)), (True, ([80, 117, 115, 104, 45, 85, 112, 115], 45)), (True, (['t', 'e', 's', 't'], 'e')), (False, (["what", "a", "great", "kata"], "kat")), (True, ([66, "codewars", 11, "alex loves pushups"], "alex loves pushups")), (False, (["come", "on", 110, "2500", 10, '!', 7, 15], "Come")), (True, (["when's", "the", "next", "Katathon?", 9, 7], "Katathon?")), (False, ([8, 7, 5, "bored", "of", "writing", "tests", 115], 45)), (True, (["anyone", "want", "to", "hire", "me?"], "me?")), ] for exp, inp in tests: test.assert_equals(check(*inp), exp)
def medium_pyramids(): test.assert_equals( longest_slide_down([ [75], [95, 64], [17, 47, 82], [18, 35, 87, 10], [20, 4, 82, 47, 65], [19, 1, 23, 75, 3, 34], [88, 2, 77, 73, 7, 63, 67], [99, 65, 4, 28, 6, 16, 70, 92], [41, 41, 26, 56, 83, 40, 80, 70, 33], [41, 48, 72, 33, 47, 32, 37, 16, 94, 29], [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14], [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57], [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48], [63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31], [4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23], ]), 1074)
def it_1(): test.assert_equals(snail([[]]), []) test.assert_equals(snail([[1]]), [1]) test.assert_equals(snail([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), [1, 2, 3, 6, 9, 8, 7, 4, 5]) test.assert_equals( snail([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]), [ 1, 2, 3, 4, 5, 10, 15, 20, 25, 24, 23, 22, 21, 16, 11, 6, 7, 8, 9, 14, 19, 18, 17, 12, 13 ]) test.assert_equals( snail([[1, 2, 3, 4, 5, 6], [20, 21, 22, 23, 24, 7], [19, 32, 33, 34, 25, 8], [18, 31, 36, 35, 26, 9], [17, 30, 29, 28, 27, 10], [16, 15, 14, 13, 12, 11]]), [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 ])
def random_tests(): from string import ascii_letters as l, digits as d abc = l + d def random_string(friend=False): return "".join( choice(abc) for _ in range(friend and 4 or randint(0, 20))) def random_input(): return [ random_string(randint(0, 100) % 4 == 0) for _ in range(randint(0, 20)) ] def solution(l): return [w for w in l if len(w) == 4] for _ in range(100): ri = random_input() Test.assert_equals(friend(ri), solution(ri))
def example_test_case(): test.assert_equals( valid_solution([[5, 3, 4, 6, 7, 8, 9, 1, 2], [6, 7, 2, 1, 9, 5, 3, 4, 8], [1, 9, 8, 3, 4, 2, 5, 6, 7], [8, 5, 9, 7, 6, 1, 4, 2, 3], [4, 2, 6, 8, 5, 3, 7, 9, 1], [7, 1, 3, 9, 2, 4, 8, 5, 6], [9, 6, 1, 5, 3, 7, 2, 8, 4], [2, 8, 7, 4, 1, 9, 6, 3, 5], [3, 4, 5, 2, 8, 6, 1, 7, 9]]), True) test.assert_equals( valid_solution([[5, 3, 4, 6, 7, 8, 9, 1, 2], [6, 7, 2, 1, 9, 0, 3, 4, 9], [1, 0, 0, 3, 4, 2, 5, 6, 0], [8, 5, 9, 7, 6, 1, 0, 2, 0], [4, 2, 6, 8, 5, 3, 7, 9, 1], [7, 1, 3, 9, 2, 4, 8, 5, 6], [9, 0, 1, 5, 3, 7, 2, 1, 4], [2, 8, 7, 4, 1, 9, 6, 3, 5], [3, 0, 0, 4, 8, 1, 1, 7, 9]]), False) test.assert_equals( valid_solution([[1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9, 1], [3, 4, 5, 6, 7, 8, 9, 1, 2], [4, 5, 6, 7, 8, 9, 1, 2, 3], [5, 6, 7, 8, 9, 1, 2, 3, 4], [6, 7, 8, 9, 1, 2, 3, 4, 5], [7, 8, 9, 1, 2, 3, 4, 5, 6], [8, 9, 1, 2, 3, 4, 5, 6, 7], [9, 1, 2, 3, 4, 5, 6, 7, 8]]), False)
def example_test_cases(): s = "aaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyz" test.assert_equals(printer_error(s), "3/56") s = "kkkwwwaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyz" test.assert_equals(printer_error(s), "6/60") s = "kkkwwwaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbmmmmmmmmmmmmmmmmmmmxyzuuuuu" test.assert_equals(printer_error(s), "11/65")
def basic_tests(): test.assert_equals( divisible_by_three('123'), True, "Should return true if the sum of the given digits is divisible by 3.") test.assert_equals( divisible_by_three('19254'), True, "Should return true if the sum of the given digits is divisible by 3.") test.assert_equals( divisible_by_three('88'), False, "Should return false if the sum of the given digits is not divisible by 3." ) test.assert_equals( divisible_by_three('1'), False, "Should return false if the sum of the given digits is not divisible by 3." )
def example_tests(): naughty = [{'name': 'xDranik', 'was_nice': False}] nice = [{ 'name': 'Santa', 'was_nice': True }, { 'name': 'Warrior reading this kata', 'was_nice': True }] test.assert_equals(get_nice_names(naughty), []) test.assert_equals(get_nice_names(nice), ['Santa', 'Warrior reading this kata']) test.assert_equals(get_naughty_names(naughty), ['xDranik']) test.assert_equals(get_naughty_names(nice), [])
def basic_test_cases(): test.assert_equals(distance_between_points(Point(3, 3), Point(3, 3)), 0) test.assert_equals(distance_between_points(Point(1, 6), Point(4, 2)), 5) test.assert_equals( round( distance_between_points(Point(-10.2, 12.5), Point(0.3, 14.7)), 6), 10.728001)
def example_tests(): equals_9 = lambda x: x == 9 less_than_9 = lambda x: x < 9 greater_than_9 = lambda x: x > 9 arr = (6, 7, 8, 9, 10, 11) test.assert_equals(one(arr, equals_9), True) test.assert_equals(one(arr, less_than_9), False) test.assert_equals(one(arr, greater_than_9), False)
def example_tests(): seq = [1, 2, 3, 4, 5] reverse(seq) test.assert_equals([5, 4, 3, 2, 1], seq) seq = ['?', 'you', 'are', 'how', 'world', 'hello'] reverse(seq) test.assert_equals(['hello', 'world', 'how', 'are', 'you', '?'], seq) seq = [{'b': 2}, {'a': 1}] reverse(seq) test.assert_equals([{'a': 1}, {'b': 2}], seq)
def sample_test_cases(): test.assert_equals(friend([ "Ryan", "Kieran", "Mark", ]), ["Ryan", "Mark"]) test.assert_equals(friend(["Ryan", "Jimmy", "123", "4", "Cool Man"]), ["Ryan"]) test.assert_equals( friend([ "Jimm", "Cari", "aret", "truehdnviegkwgvke", "sixtyiscooooool" ]), ["Jimm", "Cari", "aret"])
def example_tests(): state_capitals = [{'state': 'Maine', 'capital': 'Augusta'}] test.assert_equals(capital(state_capitals), ["The capital of Maine is Augusta"]) country_capitals = [{'country': 'Spain', 'capital': 'Madrid'}] test.assert_equals(capital(country_capitals), ["The capital of Spain is Madrid"]) mixed_capitals = [{ "state": 'Maine', 'capital': 'Augusta' }, { 'country': 'Spain', "capital": "Madrid" }] test.assert_equals( capital(mixed_capitals), ["The capital of Maine is Augusta", "The capital of Spain is Madrid"])
def to_jaden_case(string): words = [word.capitalize() for word in string.split(" ")] return " ".join(words) if __name__== "__main__": import codewars_test as test # Sample Tests quote = "How can mirrors be real if our eyes aren't real" test.assert_equals(to_jaden_case(quote), "How Can Mirrors Be Real If Our Eyes Aren't Real") # Test Cases try: to_jaden_case = toJadenCase except NameError: pass quotes = [ [ "most trees are blue", "Most Trees Are Blue" ], [ "How can mirrors be real if our eyes aren't real", "How Can Mirrors Be Real If Our Eyes Aren't Real" ], [ "All the rules in this world were made by someone no smarter than you. So make your own.", "All The Rules In This World Were Made By Someone No Smarter Than You. So Make Your Own." ],
def it_1(): for _ in range(100): n = R(1, 20) a = [[R(1, 1000) for _ in range(n)] for _ in range(n)] b = [x[:] for x in a] test.assert_equals(snail(a), solution(b))
for x in snail_map: s.append(x.pop()) # bottom s.extend(snail_map.pop()[::-1]) if snail_map: # left l = [] for x in snail_map: l.append(x.pop(0)) s.extend(l[::-1]) return s array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] expected = [1, 2, 3, 6, 9, 8, 7, 4, 5] test.assert_equals(snail(array), expected) @test.describe("Fixed tests") def fixed_tests(): @test.it("Tests") def it_1(): test.assert_equals(snail([[]]), []) test.assert_equals(snail([[1]]), [1]) test.assert_equals(snail([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), [1, 2, 3, 6, 9, 8, 7, 4, 5]) test.assert_equals( snail([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]), [ 1, 2, 3, 4, 5, 10, 15, 20, 25, 24, 23, 22, 21, 16, 11, 6, 7, 8, 9, 14, 19, 18, 17, 12, 13
# ------------ KATA DESCRIPTION ------------ """ 8 kyu Find the smallest integer in the array Given an array of integers your solution should find the smallest integer. For example: Given [34, 15, 88, 2] your solution will return 2 Given [34, -345, -1, 100] your solution will return -345 You can assume, for the purpose of this kata, that the supplied array will not be empty. """ # --------------- SOLUTION --------------- import codewars_test as test find_smallest_int = min # --------------- TEST CASES --------------- test.assert_equals(find_smallest_int([78, 56, 232, 12, 11, 43]), 11) test.assert_equals(find_smallest_int([78, 56, -2, 12, 8, -33]), -33) test.assert_equals(find_smallest_int([0, 1 - 2**64, 2**64]), 1 - 2**64)
def example_tests(): test.assert_equals(multiply_and_filter([1,2,3,4], 1.5), [1.5, 3, 4.5, 6]) test.assert_equals(multiply_and_filter([1,2,3], 0), [0, 0, 0]) test.assert_equals(multiply_and_filter([0,0,0], 2), [0, 0, 0]) test.assert_equals(multiply_and_filter([1, None, lambda x: x, 2.5, 'string', 10, None, {}, []], 3), [3,7.5,30]) test.assert_equals(multiply_and_filter([1, None, lambda x: x, 2.5, 'string', 10, None, {}, [], True, False], 3), [3,7.5,30])
char) # counts number of "chars" in s1 and s2 strings if l1 > 1 or l2 > 1: if l1 == l2: mas.append((l1, "=:" + str(char) * l1)) elif l1 != l2: mas.append((max(l1, l2), ("1" if l1 > l2 else "2") + ":" + str(char) * max(l1, l2))) mas.sort(key=lambda row: row[1]) mas.sort(key=lambda row: row[0], reverse=True) return '/'.join([item[1] for item in mas]) # --------------- TEST CASES --------------- Test.describe("Mix") Test.it("Basic Tests") Test.assert_equals(mix("Are they here", "yes, they are here"), "2:eeeee/2:yy/=:hh/=:rr") Test.assert_equals( mix("looping is fun but dangerous", "less dangerous than coding"), "1:ooo/1:uuu/2:sss/=:nnn/1:ii/2:aa/2:dd/2:ee/=:gg") Test.assert_equals(mix(" In many languages", " there's a pair of functions"), "1:aaa/1:nnn/1:gg/2:ee/2:ff/2:ii/2:oo/2:rr/2:ss/2:tt") Test.assert_equals(mix("Lords of the Fallen", "gamekult"), "1:ee/1:ll/1:oo") Test.assert_equals(mix("codewars", "codewars"), "") Test.assert_equals(mix("A generation must confront the looming ", "codewarrs"), "1:nnnnn/1:ooooo/1:tttt/1:eee/1:gg/1:ii/1:mm/=:rr")
""" # --------------- SOLUTION --------------- import codewars_test as test def cakes(recipe, available): mn = float( 'inf') # used to store minimum, initialized with positive infinity for ing in recipe: if ing in available: # find how many cakes you can bake for that ingredient and save minimum amount mn = min(mn, available[ing] // recipe[ing]) else: return 0 # if there is no ingredient available no cakes can be baked return mn # --------------- TEST CASES --------------- test.describe('Testing Pete, the Baker') test.it('gives us the right number of cakes') recipe = {"flour": 500, "sugar": 200, "eggs": 1} available = {"flour": 1200, "sugar": 1200, "eggs": 5, "milk": 200} test.assert_equals(cakes(recipe, available), 2, 'Wrong result for example #1') recipe = {"apples": 3, "flour": 300, "sugar": 150, "milk": 100, "oil": 100} available = {"sugar": 500, "flour": 2000, "milk": 2000} test.assert_equals(cakes(recipe, available), 0, 'Wrong result for example #2')
# ------------ KATA DESCRIPTION ------------ """ 8 kyu - SpeedCode #2 - Array Madness SpeedCode #2 - Array Madness Objective Given two integer arrays a, b, both of length >= 1, create a program that returns true if the sum of the squares of each element in a is strictly greater than the sum of the cubes of each element in b. E.g. array_madness([4, 5, 6], [1, 2, 3]) => True #because 4 ** 2 + 5 ** 2 + 6 ** 2 > 1 ** 3 + 2 ** 3 + 3 ** 3 Get your timer out. Are you ready? Ready, get set, GO!!! """ # --------------- SOLUTION --------------- import codewars_test as test def array_madness(a, b): return sum(map(lambda x: x**2, a)) > sum(map(lambda y: y**3, b)) # --------------- TEST CASES --------------- test.assert_equals(array_madness([4, 5, 6], [1, 2, 3]),True) test.assert_equals(array_madness( [1, 2, 3],[4, 5, 6]),False)
def iloveyou(): test.assert_equals(word_mesh(["beacon", "condominium", "umbilical", "california"]), "conumcal") test.assert_equals(word_mesh(["allow", "lowering", "ringmaster", "terror"]), "lowringter") test.assert_equals(word_mesh(["abandon", "donation", "onion", "ongoing"]), "dononon") test.assert_equals(word_mesh(["jamestown", "ownership", "hippocampus", "pushcart", "cartographer", "pheromone"]), "ownhippuscartpher")