Lists
Data Structures
Numbers
Data Types
Functions
Control Flow
Basic Language Features
Fundamentals
Algorithms
Logic
Lists
Data Structures
Numbers
Data Types
Functions
Control Flow
Basic Language Features
Fundamentals
Algorithms
Logic
def is_prime(n: int) -> bool: if n < 5: return n in (2, 3) if not n & 1 or not n % 3: return False return not any(not n%i or not n%(i+2) for i in range(5, int(n**0.5) + 1, 6))
is_prime = lambda n: n == 2 or n > 2 and n % 2 and all(n % i for i in range(3, int(n**.5) + 1, 2))- def is_prime(n: int) -> bool:
- if n < 5:
- return n in (2, 3)
- if not n & 1 or not n % 3:
- return False
- return not any(not n%i or not n%(i+2) for i in range(5, int(n**0.5) + 1, 6))
def is_prime(n: int) -> bool: if n <= 1: return False if n <= 3: return True if n % 2 == 0 or n % 3 == 0: return False sqrtn: int = int(n**0.5) + 1 for i in range(5, sqrtn, 6): if n % i == 0 or n % (i + 2) == 0: return False return True
export const checkIsNumberSimple = (number: number) => {// Check if the number is integerif (number % 1 != 0) return false;if (number < 2) return false;if (number != 2 && number % 2 == 0) return false;if (number != 3 && number % 3 == 0) return false;return true;};- def is_prime(n: int) -> bool:
- if n <= 1:
- return False
- if n <= 3:
- return True
- if n % 2 == 0 or n % 3 == 0:
- return False
- sqrtn: int = int(n**0.5) + 1
- for i in range(5, sqrtn, 6):
- if n % i == 0 or n % (i + 2) == 0:
- return False
- return True
test.assert_equals(is_prime(2), True) test.assert_equals(is_prime(3), True) test.assert_equals(is_prime(5), True) test.assert_equals(is_prime(3571), True) test.assert_equals(is_prime(0), False) test.assert_equals(is_prime(1), False) test.assert_equals(is_prime(4), False) test.assert_equals(is_prime(3572), False)
// See https://www.chaijs.com for how to use Chai.import { assert } from "chai";import { checkIsNumberSimple } from "./solution";// TODO Add your tests heredescribe("Simple numbers", function() {it("should not be negative numbers", function() {assert.strictEqual(checkIsNumberSimple(-1), false);});it("should be integer numbers", function() {assert.strictEqual(checkIsNumberSimple(3.1), false);});it("should be simple numbers", function() {assert.strictEqual(checkIsNumberSimple(2), true);assert.strictEqual(checkIsNumberSimple(3), true);assert.strictEqual(checkIsNumberSimple(5), true);assert.strictEqual(checkIsNumberSimple(3571), true);assert.strictEqual(checkIsNumberSimple(0), false);assert.strictEqual(checkIsNumberSimple(1), false);assert.strictEqual(checkIsNumberSimple(4), false);assert.strictEqual(checkIsNumberSimple(3572), false);});});- test.assert_equals(is_prime(2), True)
- test.assert_equals(is_prime(3), True)
- test.assert_equals(is_prime(5), True)
- test.assert_equals(is_prime(3571), True)
- test.assert_equals(is_prime(0), False)
- test.assert_equals(is_prime(1), False)
- test.assert_equals(is_prime(4), False)
- test.assert_equals(is_prime(3572), False)
def remove(string): return ''.join(x for x in string if x.islower())
import string- def remove(string):
arr = list(string)test = ""for i in range (len(arr)):if arr[i].isupper() == True or arr[i] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] or arr[i] == " ":arr[i] = "`"else:continuefor i in arr:test += ireturn(test.replace("`", ""))- return ''.join(x for x in string if x.islower())
import codewars_test as test import solution # or from solution import example @test.describe("remove") def test_group(): @test.it("test cases") def test_case(): test.assert_equals(remove('hell o'), 'hello') test.assert_equals(remove('Hello'), 'ello') test.assert_equals(remove('hel1o'), 'helo') test.assert_equals(remove('Hel1 o'), 'elo')
- import codewars_test as test
- import solution # or from solution import example
- @test.describe("remove")
- def test_group():
- @test.it("test cases")
- def test_case():
- test.assert_equals(remove('hell o'), 'hello')
- test.assert_equals(remove('Hello'), 'ello')
- test.assert_equals(remove('hel1o'), 'helo')
- test.assert_equals(remove('Hel1 o'), 'elo')
Sorting
Algorithms
Logic
Added tests
def sort_by_salary(workers): return tuple(x[0] for x in sorted([x for x in workers if x[1]!='-'], key=lambda x: int(x[1]), reverse=True)[:5])
- def sort_by_salary(workers):
filtered = list(filter(lambda x: x[1] != "-", workers))if not filtered:return tuple()new = list(sorted(filtered, key=lambda x: int(x[1])))[::-1]return tuple(i[0] for i in new)[:5]- return tuple(x[0] for x in sorted([x for x in workers if x[1]!='-'], key=lambda x: int(x[1]), reverse=True)[:5])
test.it("Basic tests") test.assert_equals(sort_by_salary([("max", "20000"), ("jason", "40000"), ("christian", "70000"), ("carl", "50000")]),("christian", "carl", "jason", "max")) test.assert_equals(sort_by_salary([("worker0", "-"), ("stephy", "100000"), ("king", "10000")]),("stephy", "king")) test.assert_equals(sort_by_salary([("ironman", "20000"), ("batman", "15000"), ("kenobi", "150000"), ("mr. x", "40000"), ("spiderman", "75000"), ("ramsay", "115000")]),("kenobi", "ramsay", "spiderman", "mr. x", "ironman"))
import codewars_test as test# TODO Write testsimport 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)- test.it("Basic tests")
- test.assert_equals(sort_by_salary([("max", "20000"), ("jason", "40000"), ("christian", "70000"), ("carl", "50000")]),("christian", "carl", "jason", "max"))
- test.assert_equals(sort_by_salary([("worker0", "-"), ("stephy", "100000"), ("king", "10000")]),("stephy", "king"))
- test.assert_equals(sort_by_salary([("ironman", "20000"), ("batman", "15000"), ("kenobi", "150000"), ("mr. x", "40000"), ("spiderman", "75000"), ("ramsay", "115000")]),("kenobi", "ramsay", "spiderman", "mr. x", "ironman"))
def flip_the_number(n): # takes int, flips int, return int r = 0 while n > 0: r *= 10 r += n % 10 n //= 10 return r
def flip_the_number(number):number = list(number)print(number)num = []for i in range(len(number)):lenth = len(number)num.append(str(number[lenth-1-i]))print(num)join = ""return join.join(num)- def flip_the_number(n):
- # takes int, flips int, return int
- r = 0
- while n > 0:
- r *= 10
- r += n % 10
- n //= 10
- return r
test.assert_equals(flip_the_number(12345), 54321); test.assert_equals(flip_the_number(1973572), 2753791);
Test.assert_equals(flip_the_number("12345"), "54321");- test.assert_equals(flip_the_number(12345), 54321);
Test.assert_equals(flip_the_number("1973572"), "2753791");- test.assert_equals(flip_the_number(1973572), 2753791);