Mathematics
Algorithms
Logic
Numbers
""" 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 """ def prime_checker(n): if n in [2, 3, 5]: return True elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0: return False a = 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]: 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
- """
- def prime_checker(n):
- if n in [2, 3, 5]:
- return True
- elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0:
- return False
- a = int(n ** 0.5 / 30)
- b = [7, 11, 13, 17, 19, 23, 29, 31]
for i in [30 * j for j in range(a)]:if True in [n % (i + q) == 0 for q in b]:- 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]:
- 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(): 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) 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)
- 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)
// Actually worse than the original solution involving so much mathematics // but it can give you the nth group if you seek just that. const groupCombinations = ( arr2D, len = arr2D.length, mapToLengths = e => e.length, lenArr1D = Array.from(arr2D, mapToLengths), reduceToProduct = (acc, cV) => [acc[0] * cV].concat(acc), lenProducts = lenArr1D.reduceRight(reduceToProduct, [1]), mapToGroupElement = (i) => (__, _i) => arr2D[_i][((~~(i / lenProducts[_i + 1]) % lenArr1D[_i]))], mapToGroup = (_, i) => Array.from({length: len}, mapToGroupElement(i)) ) => [lenProducts[0], Array.from({length: lenProducts[0]}, mapToGroup)];
- // Actually worse than the original solution involving so much mathematics
- // but it can give you the nth group if you seek just that.
- const groupCombinations = (
- arr2D,
- len = arr2D.length,
- mapToLengths = e => e.length,
- lenArr1D = Array.from(arr2D, mapToLengths),
- reduceToProduct = (acc, cV) => [acc[0] * cV].concat(acc),
- lenProducts = lenArr1D.reduceRight(reduceToProduct, [1]),
mapToGroupElement = (i) => (__, _i) => arr2D[_i][((~~(i / lenProducts[_i + 1]) % lenProducts[_i]))],- mapToGroupElement = (i) => (__, _i) => arr2D[_i][((~~(i / lenProducts[_i + 1]) % lenArr1D[_i]))],
- mapToGroup = (_, i) => Array.from({length: len}, mapToGroupElement(i))
- ) => [lenProducts[0], Array.from({length: lenProducts[0]}, mapToGroup)];
// Actually worse than the original solution involving so much mathematics // but it can give you the nth group if you seek just that. const groupCombinations = ( arr2D, len = arr2D.length, mapToLengths = e => e.length, lenArr1D = Array.from(arr2D, mapToLengths), reduceToProduct = (acc, cV) => [acc[0] * cV].concat(acc), lenProducts = lenArr1D.reduceRight(reduceToProduct, [1]), mapToGroupElement = (i) => (__, _i) => arr2D[_i][((~~(i / lenProducts[_i + 1]) % lenProducts[_i]))], mapToGroup = (_, i) => Array.from({length: len}, mapToGroupElement(i)) ) => [lenProducts[0], Array.from({length: lenProducts[0]}, mapToGroup)];
function groupCombinations(arr2D) {let solution = [[]];for (let arr1D of arr2D.reverse()) {solution = arr1D.flatMap(x => solution.map(y => [x, ...y])); // O(N) cons}return [solution.length, solution];}- // Actually worse than the original solution involving so much mathematics
- // but it can give you the nth group if you seek just that.
- const groupCombinations = (
- arr2D,
- len = arr2D.length,
- mapToLengths = e => e.length,
- lenArr1D = Array.from(arr2D, mapToLengths),
- reduceToProduct = (acc, cV) => [acc[0] * cV].concat(acc),
- lenProducts = lenArr1D.reduceRight(reduceToProduct, [1]),
- mapToGroupElement = (i) => (__, _i) => arr2D[_i][((~~(i / lenProducts[_i + 1]) % lenProducts[_i]))],
- mapToGroup = (_, i) => Array.from({length: len}, mapToGroupElement(i))
- ) => [lenProducts[0], Array.from({length: lenProducts[0]}, mapToGroup)];
const revstr = ( str, reduceToRevString = (acc, cV) => cV + acc, resStr = [...str].reduce(reduceToRevString, '') ) => resStr;
function revstr(str) {// i = str.length; newstr = "";// while (i > 0) {// newstr += str[i - 1]; i--;// }// return newstr;return str.split('').reverse().join('');}- const revstr = (
- str,
- reduceToRevString = (acc, cV) => cV + acc,
- resStr = [...str].reduce(reduceToRevString, '')
- ) => resStr;
Mathematics
Algorithms
Logic
Numbers
""" 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 """ def prime_checker(n): if n in [2, 3, 5]: return True elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0: return False a = int(n ** 0.5 / 30) b = [7, 11, 13, 17, 19, 23, 29, 31] for i in [30 * j for j in range(a)]: if True in [n % (i + q) == 0 for q in b]: return False return True
- """
- https://en.wikipedia.org/wiki/Primality_test
This one has lesser tests or usage of % operator- This one has lesser tests or usage of % operator.
- An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3
- """
- def prime_checker(n):
if n in [2, 3, 5, 7]:- if n in [2, 3, 5]:
- return True
elif n % 2 == 0 or n % 3 == 0:- elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0:
- return False
a = int(n ** 0.5 / 6)- a = int(n ** 0.5 / 30)
- b = [7, 11, 13, 17, 19, 23, 29, 31]
for i in [5 + 6 * j for j in range(a)]:if n % i == 0 or n % (i + 2) == 0:- for i in [30 * j for j in range(a)]:
- if True in [n % (i + q) == 0 for q in b]:
- return False
- return True
Mathematics
Algorithms
Logic
Numbers
""" https://en.wikipedia.org/wiki/Primality_test This one has lesser tests or usage of % operator """ def prime_checker(n): if n in [2, 3, 5, 7]: return True elif n % 2 == 0 or n % 3 == 0: return False a = int(n ** 0.5 / 6) for i in [5 + 6 * j for j in range(a)]: if n % i == 0 or n % (i + 2) == 0: return False return True
- """
- https://en.wikipedia.org/wiki/Primality_test
- This one has lesser tests or usage of % operator
- """
- def prime_checker(n):
a = 0for i in range(2,n):if n == 2:return Truebreakelif n%i == 0:- if n in [2, 3, 5, 7]:
- return True
- elif n % 2 == 0 or n % 3 == 0:
- return False
- a = int(n ** 0.5 / 6)
- for i in [5 + 6 * j for j in range(a)]:
- if n % i == 0 or n % (i + 2) == 0:
- return False
a += 1breakif a == 0:return True- return True