In the study of math, we usually assert (x,y) to set the point in 2-D condition. (x,y,z) to set the point in 3-D condition. As so on.
When we count the distance of two points in 2-D condition, like (x1,y1) and (x2,y2) . We do:
sqrt[(x1-x2)**2+(y1-y2)**2]
The same idea to apply in N-dimensions.
def distance_count(a,b): return sum([(i-j)**2 for (i,j) in zip(a,b)])**0.5
import numpy as np# calculate the distance between two points in 2d,3d ... nDdef distanceND(pA, pB, nD = None):return np.linalg.norm(np.array(pA) - np.array(pB))distance2D = distanceNDdistance3D =distanceND- def distance_count(a,b):
- return sum([(i-j)**2 for (i,j) in zip(a,b)])**0.5
test.assert_equals(distance_count([1,1],[2,2]),2**0.5) test.assert_equals(distance_count([2,1,1],[4,1,1]),2)
from math import sqrtdef distance2D_check(pA, pB):if pA == pB: return 0xA, yA = tuple(pA); xB, yB = tuple(pB)return sqrt((xA - xB)**2 + (yA - yB)**2)def distance3D_check(pA, pB):if pA == pB: return 0xA, yA, zA = tuple(pA); xB, yB, zB = tuple(pB)return sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2)def assertFuzzyEquals(actual, expected, msg=""):merr = 1e-10inrange = abs(actual - expected) <= merrif (inrange == False):msg = "At 1e-10: Expected value must be {:.10f} but got {:.10f}"msg = msg.format(expected, actual)return Test.expect(inrange, msg)test.describe("Basic Tests for 2D")pA = [1, 1]; pB = [2, 2]assertFuzzyEquals(distance2D(pA, pB), 1.41421356237)pA = [0, 0]; pB = [5, 6]assertFuzzyEquals(distance2D(pA, pB), 7.81024967591)pA = [-5, 4]; pB = [-10, 6]assertFuzzyEquals(distance2D(pA, pB), 5.38516480713)pA = [-5.1, 4.0]; pB = [10.2, -6.3]assertFuzzyEquals(distance2D(pA, pB), 18.4439692041)test.describe("Basic Tests for 3D")pA = [1, 1, 1]; pB = [2, 2, 2]assertFuzzyEquals(distance3D(pA, pB), 1.73205080757)pA = [0, 0, 0]; pB = [7, 8, 9]assertFuzzyEquals(distance3D(pA, pB), 13.9283882772)pA = [-5, 4, 7]; pB = [-10, 6, -18]assertFuzzyEquals(distance3D(pA, pB), 25.5734237051)pA = [-5.1, 4.0, 1]; pB = [10.2, -6.3, -1]assertFuzzyEquals(distance3D(pA, pB), 18.5520888312)test.describe("Random Tests for 2D")test.describe("Integer coordinates")import randomfrom random import randintfor h in range(20):pA = [randint(-100, 100), randint(-100, 100)]pB = [randint(-100, 100), randint(-100, 100)]result = distance2D_check(pA, pB)res = distance2D(pA, pB)assertFuzzyEquals(res, result)test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))test.describe("Random Tests for 2D")test.describe("Decimal coordinates")for h in range(20):pA = [randint(-100, 100) + random.random(), randint(-100, 100) + random.random() ]pB = [randint(-100, 100) + random.random() , randint(-100, 100) + random.random()]result = distance2D_check(pA, pB)res = distance2D(pA, pB)assertFuzzyEquals(res, result)test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))test.describe("Random Tests for 3D")test.describe("Integer coordinates")for h in range(20):pA = [randint(-100, 100), randint(-100, 100), randint(-100, 100)]pB = [randint(-100, 100), randint(-100, 100), randint(-100, 100)]result = distance3D_check(pA, pB)res = distance3D(pA, pB)assertFuzzyEquals(res, result)test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))test.describe("Random Tests for 3D")test.describe("Decimal coordinates")for h in range(10):pA = [randint(-100, 100) + random.random(), randint(-100, 100) + random.random(), randint(-100, 100) + random.random() ]pB = [randint(-100, 100) + random.random() , randint(-100, 100) + random.random(), randint(-100, 100) + random.random()]result = distance3D_check(pA, pB)res = distance3D(pA, pB)assertFuzzyEquals(res, result)test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))- test.assert_equals(distance_count([1,1],[2,2]),2**0.5)
- test.assert_equals(distance_count([2,1,1],[4,1,1]),2)
Find the prime number
def is_prime(n): de=[n%i for i in range(2,int(n**0.5)+1)] if 0 in de: return False else : return True
from math import sqrt- def is_prime(n):
return n > 1 and all(n % i for i in range(2, int(sqrt(n))+1))- de=[n%i for i in range(2,int(n**0.5)+1)]
- if 0 in de: return False
- else : return True
test.expect(is_prime(3) == True) test.expect(is_prime(11) == True) test.expect(is_prime(15) == False) test.expect(is_prime(21) == False)
test.expect(is_prime(7) == True)test.expect(is_prime(17) == True)- test.expect(is_prime(3) == True)
- test.expect(is_prime(11) == True)
- test.expect(is_prime(15) == False)
- test.expect(is_prime(21) == False)
Given a list contains string, such as ['I','love','you'] .Then output 'I love you'.
A kumite try by beginer.
Note that the extra space should be added between strings , but not at the begining or the ending.
def get_list(may):
out=''
for i in may:
out+=i+' '
return out[:-1]
# TODO: Replace examples and use TDD development by writing your own tests
# These are some of the methods available:
# test.expect(boolean, [optional] message)
# test.assert_equals(actual, expected, [optional] message)
# test.assert_not_equals(actual, expected, [optional] message)
# You can use Test.describe and Test.it to write BDD style test groupings
test.assert_equals(get_list(['I','love','you']),'I love you')