Fundamentals
Strings
reverse_string = lambda s: s[::-1]
def reverse_string(s):return s[::-1]- reverse_string = lambda s: s[::-1]
import codewars_test as test from solution import reverse_string @test.describe("Solution") def a(): @test.it("should test for something") def a(): test.assert_equals(reverse_string('abc'), 'cba') test.assert_equals(reverse_string('123'), '321') test.assert_equals(reverse_string('a1b2c3'), '3c2b1a') test.assert_equals(reverse_string('Hello World!'), '!dlroW olleH') test.assert_equals(reverse_string('Lorem ipsum dolor sit amet!'), '!tema tis rolod muspi meroL')
- import codewars_test as test
- from solution import reverse_string
- @test.describe("Solution")
- def a():
- @test.it("should test for something")
- def a():
- test.assert_equals(reverse_string('abc'), 'cba')
- test.assert_equals(reverse_string('123'), '321')
- test.assert_equals(reverse_string('a1b2c3'), '3c2b1a')
- test.assert_equals(reverse_string('Hello World!'), '!dlroW olleH')
Matrix
Determine whether a given square matrix of order n is a "magic" matrix. A magic matrix is a matrix in which the sum of numbers in each row, column and diagonal is the same.
def is_magic_square(matrix):
n = len(matrix)
diagonal_sum = sum(matrix[i][i] for i in range(n))
anti_diagonal_sum = sum(matrix[i][n - 1 - i] for i in range(n))
if diagonal_sum != anti_diagonal_sum:
return False
for i in range(n):
row_sum = sum(matrix[i])
col_sum = sum(matrix[j][i] for j in range(n))
if row_sum != diagonal_sum or col_sum != diagonal_sum:
return False
return True
import unittest
from solution import is_magic_square
class TestMagicSquare(unittest.TestCase):
def test_case_1(self):
matrix = [
[2, 7, 6],
[9, 5, 1],
[4, 3, 8]
]
self.assertTrue(is_magic_square(matrix))
def test_case_2(self):
matrix = [
[16, 23, 17],
[78, 32, 21],
[17, 16, 15]
]
self.assertFalse(is_magic_square(matrix))
def test_case_3(self):
matrix = [
[23, 28, 21],
[22, 24, 26],
[27, 20, 25]
]
self.assertTrue(is_magic_square(matrix))
if __name__ == '__main__':
unittest.main()
Fundamentals
Logic
def Greeting(n, rank='', formal=False): determinant = f'{rank} {n}' if formal and rank else f'{n}' return f"Hello, {determinant}." if formal else f"Hey, {determinant}!"
def Greeting(n, rank='', formal=0):determinant = f'{rank} {n}' if formal and rank else f'{n}'return f"Hello, {determinant}." if formal else f"Hey, {determinant}!"- def Greeting(n, rank='', formal=False):
- determinant = f'{rank} {n}' if formal and rank else f'{n}'
- return f"Hello, {determinant}." if formal else f"Hey, {determinant}!"
Arrays
Sorting
import re def longest_words(word_list, num_words): if not isinstance(word_list, list) or not isinstance(num_words, int) or num_words < 0: return 'Invalid Parameters' valid_words = [re.sub(r'[^A-Za-z]', '', word) for word in word_list if word and re.sub(r'[^A-Za-z]', '', word)] return sorted(valid_words, key=len, reverse=True)[:num_words] if num_words <= len(valid_words) else 'Invalid Parameters'
- import re
def longest_words(array, num):valid_words = [re.sub(r'[^A-Za-z]', '', word) for word in array if word and re.sub(r'[^A-Za-z]', '', word)]return sorted(valid_words, key=len, reverse=True)[:num] if num <= len(valid_words) else 'Invalid Parameters'- def longest_words(word_list, num_words):
- if not isinstance(word_list, list) or not isinstance(num_words, int) or num_words < 0:
- return 'Invalid Parameters'
- valid_words = [re.sub(r'[^A-Za-z]', '', word) for word in word_list if word and re.sub(r'[^A-Za-z]', '', word)]
- return sorted(valid_words, key=len, reverse=True)[:num_words] if num_words <= len(valid_words) else 'Invalid Parameters'
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(1 + 1, 2)
- import codewars_test as test
from solution import longest_wordsimport random- # TODO Write tests
- import solution # or from solution import example
# Working solutiondef _solve(array, num):new = [k for k in sorted([''.join([i for i in j if 97 <= ord(i) <= 122 or 65 <= ord(i) <= 90]) for j in array], key=lambda x: len(x), reverse=True) if k != '']print(new[:num] if num <= len(new) else 'Invalid Parameters')return new[:num] if num <= len(new) else 'Invalid Parameters'# Random word list generatordef gen_random(length):word_list = [('Programming', 'ƤŖø¶Ŗ@µµƗƕ¶', 'Prø¶r@mming'), ('is', 'Ɨ$', 'is'), ('often', 'øƒƮƩƕ', 'often'),('compared', '¢øµƤ@ŖƩƊ', 'comƤ@Ŗed'), ('to', 'Ʈø', 'to'), ('solving', '$øȴƔƗƕ¶', '$olvƗn¶'),('intricate', 'ƗƕƮŖƗ¢@ƮƩ', 'intri¢aƮe'), ('puzzles,', 'Ƥû§§ȴƩ$,', 'pu§§le$,'), ('', '', '',),('requiring', 'ŖƩǭûƗŖƗƕ¶', 'ŖeqûƗŖing'), ('a', '@', 'a'), ('sharp', '$ɧ@ŖƤ', 'sharp'),('analytical', '@ƕ@ȴ¥ƮƗ¢@ȴ', '@nal¥Ʈical'), ('mind', 'µƗƕƊ', 'mind'), ('to', 'Ʈø', 'tø'),('piece', 'ƤƗƩ¢Ʃ', 'piecƩ'), ('together', 'Ʈø¶ƩƮɧƩŖ', 'togetɧeŖ'), ('the', 'ƮɧƩ', 'the'),('correct', '¢øŖŖƩ¢Ʈ', 'correct'), ('code.', '¢øƊƩ.', 'code.'), ('One', 'øƕƩ', 'Oƕe'), ('of', 'øƒ', 'oƒ'),('the', 'ƮɧƩ', 'tɧe'), ('complexities', '¢øµƤȴƩ×ƗƮƗƩ$', 'cøµplexƗƮie$'), ('of', 'øƒ', 'of'),('programming', 'ƤŖø¶Ŗ@µµƗƕ¶', 'ƤrogrammƗng'), ('lies', 'ȴƗƩ$', 'liƩs'), ('in', 'Ɨƕ', 'in'),('managing', 'µ@ƕ@¶Ɨƕ¶', 'µaƕagƗn¶'), ('an', '@ƕ', '@n'), ('ever-growing', 'ƩƔƩŖ-¶ŖøƜƗƕ¶', 'eƔƩr-¶røƜing'),('body', 'ßøƊ¥', 'body'), ('of', 'øƒ', 'oƒ'), ('knowledge', 'ƙƕøƜȴƩƊ¶Ʃ', 'kƕoƜledge'), ('as', '@$', 'as'),('new', 'ƕƩƜ', 'nƩw'), ('languages', 'ȴ@ƕ¶û@¶Ʃ$', 'laƕgû@ges'), ('and', '@ƕƊ', '@nd'),('technologies', 'ƮƩ¢ɧƕøȴø¶ƗƩ$', 'technoløgies'), ('emerge.', 'ƩµƩŖ¶Ʃ.', 'emergƩ.'), ('', '', '',),('Debugging', 'ƊƩßû¶¶Ɨƕ¶', 'Ɗeßûg¶ing'), ('code', '¢øƊƩ', 'cøde'), ('can', '¢@ƕ', 'can'),('be', 'ßƩ', 'be'), ('like', 'ȴƗƙƩ', 'ȴikƩ'), ('searching', '$Ʃ@Ŗ¢ɧƗƕ¶', 'se@rcɧƗn¶'), ('for', 'ƒøŖ', 'for'),('a', '@', 'a'), ('needle', 'ƕƩƩƊȴƩ', 'ƕeƩdle'), ('in', 'Ɨƕ', 'Ɨn'), ('a', '@', 'a'),('haystack,', 'ɧ@¥$Ʈ@¢ƙ,', 'haystacƙ,'), ('as', '@$', '@s'), ('a', '@', 'a'), ('single', '$Ɨƕ¶ȴƩ', 'siƕgle'),('character', '¢ɧ@Ŗ@¢ƮƩŖ', 'cɧaŖ@cteŖ'), ('error', 'ƩŖŖøŖ', 'errør'), ('can', '¢@ƕ', 'caƕ'),('lead', 'ȴƩ@Ɗ', 'lƩ@Ɗ'), ('to', 'Ʈø', 'tø'), ('hours', 'ɧøûŖ$', 'hoûŖ$'), ('of', 'øƒ', 'oƒ'),('troubleshooting.', 'ƮŖøûßȴƩ$ɧøøƮƗƕ¶.', 'trøußlesɧooƮin¶.'), ('The', 'ƮɧƩ', 'Ʈhe'), ('', '', '',), ('', '', '',),('meticulous', 'µƩƮƗ¢ûȴøû$', 'metƗ¢uȴous'), ('attention', '@ƮƮƩƕƮƗøƕ', 'aƮtention'), ('to', 'Ʈø', 'to'),('detail', 'ƊƩƮ@Ɨȴ', 'detail'), ('needed', 'ƕƩƩƊƩƊ', 'nƩedƩd'), ('in', 'Ɨƕ', 'in'), ('', '', '',),('programming', 'ƤŖø¶Ŗ@µµƗƕ¶', 'Ƥro¶rammƗng'), ('can', '¢@ƕ', '¢an'), ('be', 'ßƩ', 'be'), ('', '', '',),('mentally', 'µƩƕƮ@ȴȴ¥', 'mƩnƮ@ȴȴy'), ('taxing,', 'Ʈ@×Ɨƕ¶,', 'Ʈ@xƗng,'), ('leading', 'ȴƩ@ƊƗƕ¶', 'ȴe@dƗng'),('to', 'Ʈø', 'to'), ('fatigue', 'ƒ@ƮƗ¶ûƩ', 'fatiguƩ'), ('and', '@ƕƊ', 'aƕd'), ('burnout.', 'ßûŖƕøûƮ.', 'ßuŖnoût.'),('Staying', '$Ʈ@¥Ɨƕ¶', '$taying'), ('updated', 'ûƤƊ@ƮƩƊ', 'upd@teƊ'), ('in', 'Ɨƕ', 'iƕ'), ('the', 'ƮɧƩ', 'tɧe'),('fast-paced', 'ƒ@$Ʈ-Ƥ@¢ƩƊ', 'fa$t-ƤacƩƊ'), ('tech', 'ƮƩ¢ɧ', 'tech'), ('world', 'ƜøŖȴƊ', 'wørȴƊ'), ('is', 'Ɨ$', 'is'),('a', '@', 'a'), ('constant', '¢øƕ$Ʈ@ƕƮ', 'con$Ʈaƕt'), ('challenge,', '¢ɧ@ȴȴƩƕ¶Ʃ,', 'ch@lleƕge,'), ('', '', '',),('with', 'ƜƗƮɧ', 'ƜiƮh'), ('new', 'ƕƩƜ', 'new'), ('frameworks', 'ƒŖ@µƩƜøŖƙ$', 'ƒŖ@meƜoŖks'), ('and', '@ƕƊ', '@nd'),('libraries', 'ȴƗßŖ@ŖƗƩ$', 'ȴƗbŖ@rƗes'), ('being', 'ßƩƗƕ¶', 'bƩƗƕ¶'), ('released', 'ŖƩȴƩ@$ƩƊ', 'rƩle@sƩƊ'),('regularly.', 'ŖƩ¶ûȴ@Ŗȴ¥.', 'ŖƩguȴarly.'), ('The', 'ƮɧƩ', 'ƮhƩ'), ('precise', 'ƤŖƩ¢Ɨ$Ʃ', 'precise'),('syntax', '$¥ƕƮ@×', 's¥nƮax'), ('and', '@ƕƊ', 'anƊ'), ('semantics', '$Ʃµ@ƕƮƗ¢$', 'seµaƕƮƗc$'), ('of', 'øƒ', 'of'),('each', 'Ʃ@¢ɧ', 'e@cɧ'), ('programming', 'ƤŖø¶Ŗ@µµƗƕ¶', 'pŖogŖamµing'), ('language', 'ȴ@ƕ¶û@¶Ʃ', 'ȴ@ngua¶e'),('add', '@ƊƊ', '@dd'), ('layers', 'ȴ@¥ƩŖ$', 'layƩr$'), ('of', 'øƒ', 'of'), ('difficulty,', 'ƊƗƒƒƗ¢ûȴƮ¥,', 'diƒfƗcûlty,'),('as', '@$', 'as'), ('small', '$µ@ȴȴ', 'smaȴȴ'), ('mistakes', 'µƗ$Ʈ@ƙƩ$', 'mƗstaƙe$'), ('can', '¢@ƕ', 'can'),('cause', '¢@û$Ʃ', 'caûsƩ'), ('major', 'µ@ǰøŖ', 'µ@ǰøŖ'), ('issues.', 'Ɨ$$ûƩ$.', 'Ɨ$sûes.'), ('', '', '',)]return [random.choice(word_list)[random.randint(0, 2)] for _ in range(length)]# Start of testing@test.describe("Example Tests")- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
@test.it("Testing...")- @test.it("test case")
- def test_case():
test.assert_equals(longest_words(["Hello", "world", "Coding", "is", "fun!"], 1), ['Coding'])test.assert_equals(longest_words(["Python", "Java", "C++", "JavaScript"], 4), ['JavaScript', 'Python', 'Java', 'C'])test.assert_equals(longest_words(["Hello", "world", "Coding", "is", "fun!"], 6), 'Invalid Parameters')@test.describe("Edge Cases")def test_group2():@test.it("Testing...")def test_edges():test.assert_equals(longest_words(["H3ll0", "w0rld", "C0d1ng", "1s", "fun!"], 4), ['wrld', 'Cdng', 'Hll', 'fun'])test.assert_equals(longest_words(["HELLO", '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], 1), ['HELLO'])test.assert_equals(longest_words(["HELLO", 'B123I123G123W123O123R123D', '1', '2', '3', '4', '5', '6', '7', '8', '9'], 2), ['BIGWORD', 'HELLO'])test.assert_equals(longest_words(["HELLO", '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], 2), 'Invalid Parameters')test.assert_equals(longest_words(['k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a'], 5), ['k', 'j', 'i', 'h', 'g'])@test.describe('Random Tests')def test_random():@test.it('Small Tests')def small_rand():for _ in range(10):for i in range(5, 10):rand_words = gen_random(i)rand_num = random.randint(0, i + 1)test.assert_equals(longest_words(rand_words[:], rand_num), _solve(rand_words[:], rand_num))@test.it('Medium Tests')def med_rand():for _ in range(10):for i in range(15, 20):rand_words = gen_random(i)rand_num = random.randint(0, i + 1)test.assert_equals(longest_words(rand_words[:], rand_num), _solve(rand_words[:], rand_num))@test.it('Large Tests')def med_rand():for i in range(25, 125):rand_words = gen_random(i)rand_num = random.randint(0, i + 1)test.assert_equals(longest_words(rand_words[:], rand_num), _solve(rand_words[:], rand_num))- test.assert_equals(1 + 1, 2)