Ad

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.

Code
Diff
  • 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 ... nD
    • def distanceND(pA, pB, nD = None):
    • return np.linalg.norm(np.array(pA) - np.array(pB))
    • distance2D = distanceND
    • distance3D =distanceND
    • def distance_count(a,b):
    • return sum([(i-j)**2 for (i,j) in zip(a,b)])**0.5

Find the prime number

Code
Diff
  • 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

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]