Regular Expressions
Declarative Programming
Advanced Language Features
Programming Paradigms
Fundamentals
Strings
class StringParity { public static boolean isEvenLength(String str) { return (str.length() & 1) == 0; } }
- class StringParity {
public static boolean isEvenLength(String str) {return str.length() % 2 == 0;}- public static boolean isEvenLength(String str) {
- return (str.length() & 1) == 0;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { @Test void simpleTests() { String ss[] = {"even", "odd", "", "Instant Noodles", "\n\n"}; for (String s : ss) { boolean expected = s.length() % 2 == 0; boolean actual = StringParity.isEvenLength(s); if (expected != actual) System.out.println("input string: " + s); assertEquals(expected, actual); } } @Test void randomTests() { for (int i = 0; i < 1000; ++i) { String s = java.util.UUID.randomUUID().toString(); boolean expected = s.length() % 2 == 0; boolean actual = StringParity.isEvenLength(s); if (expected != actual) System.out.println("input string: " + s); assertEquals(expected, actual); } } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- @Test
void testSomething() {assertEquals(true, StringParity.isEvenLength("even"));assertEquals(false, StringParity.isEvenLength("odd"));assertEquals(true, StringParity.isEvenLength(""));assertEquals(false, StringParity.isEvenLength("Instant Noodles"));assertEquals(true, StringParity.isEvenLength("\n\n"));- void simpleTests() {
- String ss[] = {"even", "odd", "", "Instant Noodles", "\n\n"};
- for (String s : ss) {
- boolean expected = s.length() % 2 == 0;
- boolean actual = StringParity.isEvenLength(s);
- if (expected != actual) System.out.println("input string: " + s);
- assertEquals(expected, actual);
- }
- }
- @Test
- void randomTests() {
- for (int i = 0; i < 1000; ++i) {
- String s = java.util.UUID.randomUUID().toString();
- boolean expected = s.length() % 2 == 0;
- boolean actual = StringParity.isEvenLength(s);
- if (expected != actual) System.out.println("input string: " + s);
- assertEquals(expected, actual);
- }
- }
- }
Mathematics
Algorithms
Logic
Numbers
Updated tests.
%timeit prime_checker(10000000019)
-> 13.4 ms ± 40.5 µs
def prime_checker(n): if n < 4: return n > 1 if not (n % 2 and n % 3): return False for i in range(5, int(n ** 0.5)+1, 6): if not ((n % i) and (n % (i + 2))): return False return True
"""Simple and fast, around 2-3ms"""# One line removed, one is edited with wit but actually works, you really should git gud < idk >- def prime_checker(n):
for i in range(3, int(n**.5)+1 if n > 0 else 10, 2):if n%i == 0: return Falsereturn (n > 2 and n%2 != 0) or n == 2- if n < 4:
- return n > 1
- if not (n % 2 and n % 3):
- return False
- for i in range(5, int(n ** 0.5)+1, 6):
- if not ((n % i) and (n % (i + 2))):
- return False
- return True
import codewars_test as test from solution import prime_checker @test.describe("Example") def test_group(): @test.it("test case") def test_case(): # primes from https://oeis.org/A000040/a000040.txt primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997} for n in range(-8, max(primes) + 1): if n in primes: test.assert_equals(prime_checker(n), True) else: test.assert_equals(prime_checker(n), False) test.assert_equals(prime_checker(1021), True) test.assert_equals(prime_checker(39 * 41), False) test.assert_equals(prime_checker(553_105_253), True) test.assert_equals(prime_checker(982_451_653), True)
- import codewars_test as test
- from solution import prime_checker
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
test.assert_equals(prime_checker(653), True)test.assert_equals(prime_checker(654), False)test.assert_equals(prime_checker(5), True)test.assert_equals(prime_checker(777), False)#test.assert_equals(prime_checker(977), True)test.assert_equals(prime_checker(125), False)#test.assert_equals(prime_checker(997), True)test.assert_equals(prime_checker(709), True)test.assert_equals(prime_checker(15), False)#test.assert_equals(prime_checker(11), True)test.assert_equals(prime_checker(13), True)test.assert_equals(prime_checker(17), True)test.assert_equals(prime_checker(19), True)test.assert_equals(prime_checker(23), True)test.assert_equals(prime_checker(29), True)- # primes from https://oeis.org/A000040/a000040.txt
- primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997}
- for n in range(-8, max(primes) + 1):
- if n in primes:
- test.assert_equals(prime_checker(n), True)
- else:
- test.assert_equals(prime_checker(n), False)
- test.assert_equals(prime_checker(1021), True)
- test.assert_equals(prime_checker(39 * 41), False)
- test.assert_equals(prime_checker(553_105_253), True)
test.assert_equals(prime_checker(982_451_653), True)test.assert_equals(prime_checker(-1), False)test.assert_equals(prime_checker(4), False)test.assert_equals(prime_checker(2), True)- test.assert_equals(prime_checker(982_451_653), True)
Mathematics
Algorithms
Logic
Numbers
""" https://en.wikipedia.org/wiki/Primality_test An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3 -- Using generator instead of list (works faster) -- range(0, 30 * sqrt_n + 1, 30) instead of (30 * k for k in range(sqrt_n + 1)) """ def prime_checker(n): if n in [2, 3, 5]: return True elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0 or n <= 1: return False sqrt_n = int(n ** 0.5 / 30) b = [7, 11, 13, 17, 19, 23, 29, 31] for i in range(0, 30 * sqrt_n + 1, 30): if any(n % (i + j) == 0 for j in b if i + j != n): return False return True
- """
- https://en.wikipedia.org/wiki/Primality_test
- An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3
- -- Using generator instead of list (works faster)
- -- range(0, 30 * sqrt_n + 1, 30) instead of (30 * k for k in range(sqrt_n + 1))
- """
- def prime_checker(n):
- if n in [2, 3, 5]:
- return True
- elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0 or n <= 1:
- return False
- sqrt_n = int(n ** 0.5 / 30)
- b = [7, 11, 13, 17, 19, 23, 29, 31]
for i in (30 * k for k in range(sqrt_n + 1)):if any(n % (i + j) == 0 for j in b if i + j is not n):- for i in range(0, 30 * sqrt_n + 1, 30):
- if any(n % (i + j) == 0 for j in b if i + j != n):
- return False
Mathematics
Algorithms
Logic
Numbers
""" https://en.wikipedia.org/wiki/Primality_test An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3 -- Using generator instead of list (works faster) """ def prime_checker(n): if n in [2, 3, 5]: return True elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0 or n <= 1: return False sqrt_n = int(n ** 0.5 / 30) b = [7, 11, 13, 17, 19, 23, 29, 31] for i in (30 * k for k in range(sqrt_n + 1)): if any(n % (i + j) == 0 for j in b if i + j is not n): return False return True
- """
- https://en.wikipedia.org/wiki/Primality_test
This one has lesser tests or usage of % operator.An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3- An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3
- -- Using generator instead of list (works faster)
- """
- def prime_checker(n):
- if n in [2, 3, 5]:
- return True
elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0:- elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0 or n <= 1:
- return False
a = int(n ** 0.5 / 30)- sqrt_n = int(n ** 0.5 / 30)
- b = [7, 11, 13, 17, 19, 23, 29, 31]
for i in [30 * j for j in range(a + 1)]:if True in [n % (i + q) == 0 for q in b if i + q is not n]:- for i in (30 * k for k in range(sqrt_n + 1)):
- if any(n % (i + j) == 0 for j in b if i + j is not n):
- return False
return True- return True
Mathematics
Algorithms
Logic
Numbers
def prime_checker(n): if n < 4: return n > 1 if n % 2 == 0 or n % 3 == 0: return False i = 5 sqrt_n = int(n ** 0.5) while i <= sqrt_n: if n % i == 0 or n % (i + 2) == 0: return False i += 6 return True
import math- def prime_checker(n):
if n == 1:- if n < 4:
- return n > 1
- if n % 2 == 0 or n % 3 == 0:
- return False
max_divisor = math.floor(math.sqrt(n))if n % 2 == 0:return Falseif n % 5 == 0 and n > 5:return Falsefor d in range(3, 1 + max_divisor, 2):if n % d == 0:- i = 5
- sqrt_n = int(n ** 0.5)
- while i <= sqrt_n:
- if n % i == 0 or n % (i + 2) == 0:
- return False
- i += 6
- return True