from itertools import * def groups_combinations(arr2D): res = list(product(*arr2D)) return [len(res), res]
- from itertools import *
def product_(arr2D):return product(*arr2D)- def groups_combinations(arr2D):
res = []for comb in product_(arr2D):res.append(comb)- res = list(product(*arr2D))
- return [len(res), res]
from math import sqrt, pow # calculate the distance between two points in 2d,3d ... nD def distanceND(pA, pB, nD = None): return sqrt(sum(pow(a-b,2) for a,b in zip(pA,pB))) distance2D = distanceND distance3D =distanceND
- from math import sqrt, pow
- # calculate the distance between two points in 2d,3d ... nD
- def distanceND(pA, pB, nD = None):
- return sqrt(sum(pow(a-b,2) for a,b in zip(pA,pB)))
def distance2D(pA, pB):return(distanceND(pA, pB))- distance2D = distanceND
def distance3D(pA, pB):return(distanceND(pA, pB))- distance3D =distanceND
def getVillianName(birthday): return '{} {}'.format( FIRSTNAME[birthday.strftime("%B")], LASTNAME[birthday.strftime("%d")[1]])
- def getVillianName(birthday):
result = ""result = FIRSTNAME[birthday.strftime("%B")] + " "result += LASTNAME[birthday.strftime("%d")[1]]return result- return '{} {}'.format( FIRSTNAME[birthday.strftime("%B")],
- LASTNAME[birthday.strftime("%d")[1]])
import itertools def eratosthenes(): # From Coobook recipies # Credits to: Credit: David Eppstein, Tim Peters, Alex Martelli, # Wim Stolker, Kazuo Moriwaka, Hallvard Furuseth, Pierre Denis, # Tobias Klausmann, David Lees, Raymond Hettinger D = {} yield 2 for q in itertools.islice(itertools.count(3), 0, None, 2): p = D.pop(q, None) if p is None: D[q * q] = q yield q else: x = p + q while x in D or not (x & 1): x += p D[x] = p def count_primes(limit): count = 0 for prime in eratosthenes(): if prime > limit: return count count += 1
- import itertools
from itertools import *- def eratosthenes():
#From Coobook recipies#Credits to: Credit: David Eppstein, Tim Peters, Alex Martelli,#Wim Stolker, Kazuo Moriwaka, Hallvard Furuseth, Pierre Denis,#Tobias Klausmann, David Lees, Raymond HettingerD = { }- # From Coobook recipies
- # Credits to: Credit: David Eppstein, Tim Peters, Alex Martelli,
- # Wim Stolker, Kazuo Moriwaka, Hallvard Furuseth, Pierre Denis,
- # Tobias Klausmann, David Lees, Raymond Hettinger
- D = {}
- yield 2
- for q in itertools.islice(itertools.count(3), 0, None, 2):
- p = D.pop(q, None)
- if p is None:
D[q*q] = q- D[q * q] = q
- yield q
- else:
- x = p + q
while x in D or not (x&1):- while x in D or not (x & 1):
- x += p
- D[x] = p
- def count_primes(limit):
- count = 0
- for prime in eratosthenes():
if prime > limit: return count- if prime > limit:
- return count
- count += 1
import itertools def eratosthenes(): D = { } yield 2 for q in itertools.islice(itertools.count(3), 0, None, 2): p = D.pop(q, None) if p is None: D[q*q] = q yield q else: x = p + q while x in D or not (x&1): x += p D[x] = p def count_primes_sol(limit): count = 0 for prime in eratosthenes(): if prime > limit: return count count += 1 test.describe("Basic Tests") test.assert_equals(count_primes(10), 4) test.assert_equals(count_primes(20), 8) test.assert_equals(count_primes(50), 15) test.assert_equals(count_primes(100), 25) test.describe("Random Tests") from random import randint test.describe("100 <= limit <= 1000") for h in range(20): limit = randint(100, 1000) result = count_primes_sol(limit) res = count_primes(limit) test.it("Testing for limit = " + str(limit)) test.assert_equals(res, result) test.it("Result = " + str(result)) test.describe("1000 <= limit <= 5000") for h in range(20): limit = randint(1000, 5000) result = count_primes_sol(limit) res = count_primes(limit) test.it("Testing for limit = " + str(limit)) test.assert_equals(res, result) test.it("Result = " + str(result)) test.describe("5000 <= limit <= 10000") for h in range(20): limit = randint(5000, 10000) result = count_primes_sol(limit) res = count_primes(limit) test.it("Testing for limit = " + str(limit)) test.assert_equals(res, result) test.it("Result = " + str(result)) test.describe("10000 <= limit <= 50000") for h in range(20): limit = randint(10000, 50000) result = count_primes_sol(limit) res = count_primes(limit) test.it("Testing for limit = " + str(limit)) test.assert_equals(res, result) test.it("Result = " + str(result)) test.describe("1000 <= limit <= 5000") for h in range(20): limit = randint(50000, 100000) result = count_primes_sol(limit) res = count_primes(limit) test.it("Testing for limit = " + str(limit)) test.assert_equals(res, result) test.it("Result = " + str(result)) test.describe("100000 <= limit <= 500000") for h in range(10): limit = randint(100000, 500000) result = count_primes_sol(limit) res = count_primes(limit) test.it("Testing for limit = " + str(limit)) test.assert_equals(res, result) test.it("Result = " + str(result)) test.describe("500000 <= limit <= 1000000") for h in range(3): limit = randint(500000, 1000000) result = count_primes_sol(limit) res = count_primes(limit) test.it("Testing for limit = " + str(limit)) test.assert_equals(res, result) test.it("Result = " + str(result)) test.describe("1000000 <= limit <= 5000000") for h in range(2): limit = randint(1000000, 5000000) result = count_primes_sol(limit) res = count_primes(limit) test.it("Testing for limit = " + str(limit)) test.assert_equals(res, result) test.it("Result = " + str(result))
- import itertools
from itertools import *- def eratosthenes():
- D = { }
- yield 2
- for q in itertools.islice(itertools.count(3), 0, None, 2):
- p = D.pop(q, None)
- if p is None:
- D[q*q] = q
- yield q
- else:
- x = p + q
- while x in D or not (x&1):
- x += p
- D[x] = p
- def count_primes_sol(limit):
- count = 0
- for prime in eratosthenes():
- if prime > limit: return count
- count += 1
- test.describe("Basic Tests")
- test.assert_equals(count_primes(10), 4)
- test.assert_equals(count_primes(20), 8)
- test.assert_equals(count_primes(50), 15)
- test.assert_equals(count_primes(100), 25)
- test.describe("Random Tests")
- from random import randint
- test.describe("100 <= limit <= 1000")
- for h in range(20):
- limit = randint(100, 1000)
- result = count_primes_sol(limit)
- res = count_primes(limit)
- test.it("Testing for limit = " + str(limit))
- test.assert_equals(res, result)
- test.it("Result = " + str(result))
- test.describe("1000 <= limit <= 5000")
- for h in range(20):
- limit = randint(1000, 5000)
- result = count_primes_sol(limit)
- res = count_primes(limit)
- test.it("Testing for limit = " + str(limit))
- test.assert_equals(res, result)
- test.it("Result = " + str(result))
- test.describe("5000 <= limit <= 10000")
- for h in range(20):
- limit = randint(5000, 10000)
- result = count_primes_sol(limit)
- res = count_primes(limit)
- test.it("Testing for limit = " + str(limit))
- test.assert_equals(res, result)
- test.it("Result = " + str(result))
- test.describe("10000 <= limit <= 50000")
- for h in range(20):
- limit = randint(10000, 50000)
- result = count_primes_sol(limit)
- res = count_primes(limit)
- test.it("Testing for limit = " + str(limit))
- test.assert_equals(res, result)
- test.it("Result = " + str(result))
- test.describe("1000 <= limit <= 5000")
- for h in range(20):
- limit = randint(50000, 100000)
- result = count_primes_sol(limit)
- res = count_primes(limit)
- test.it("Testing for limit = " + str(limit))
- test.assert_equals(res, result)
- test.it("Result = " + str(result))
- test.describe("100000 <= limit <= 500000")
- for h in range(10):
- limit = randint(100000, 500000)
- result = count_primes_sol(limit)
- res = count_primes(limit)
- test.it("Testing for limit = " + str(limit))
- test.assert_equals(res, result)
- test.it("Result = " + str(result))
- test.describe("500000 <= limit <= 1000000")
- for h in range(3):
- limit = randint(500000, 1000000)
- result = count_primes_sol(limit)
- res = count_primes(limit)
- test.it("Testing for limit = " + str(limit))
- test.assert_equals(res, result)
- test.it("Result = " + str(result))
- test.describe("1000000 <= limit <= 5000000")
- for h in range(2):
- limit = randint(1000000, 5000000)
- result = count_primes_sol(limit)
- res = count_primes(limit)
- test.it("Testing for limit = " + str(limit))
- test.assert_equals(res, result)
- test.it("Result = " + str(result))