python 检查值是否为NaN

isNan
def isNaN(num):
    return num != num

python 将函数应用于列

fToColumn.py
# We'll treat anyone as under 16 as a child, and then use the apply technique with a function to create a new column

# Revisit Lecture 45 for a refresher on how to do this.

# First let's make a function to sort through the sex 
def male_female_child(passenger):
    # Take the Age and Sex
    age,sex = passenger
    # Compare the age, otherwise leave the sex
    if age < 16:
        return 'child'
    else:
        return sex
    

# We'll define a new column called 'person', remember to specify axis=1 for columns and not index
titanic_df['person'] = titanic_df[['Age','Sex']].apply(male_female_child,axis=1)

python NumberOfDiscIntersections

保存自https://app.codility.com/demo/results/trainingY97TTZ-47W/

numberOfDiscIntersections.py
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
# reference:
# https://stackoverflow.com/questions/4801242/algorithm-to-calculate-number-of-intersecting-discs#
def solution(A):
    # write your code in Python 3.6
    start = [0] * len(A)
    stop = [0] * len(A)
    t = len(A) - 1
    for i in range(len(A)):
        s = i - A[i] if i > A[i] else 0
        e = i + A[i] if t - i > A[i] else t
        start[s] += 1
        stop[e] += 1
    t = 0
    result = 0
    for i in range(len(A)):
        if start[i] > 0:
            result += t * start[i]
            result += start[i] * (start[i] - 1) // 2
            if result > 10_000_000:
                return -1
            t += start[i]
        t -= stop[i]
    return result

python 三角形

保存自https://app.codility.com/demo/results/training7J3CHF-AEE/

triangle.py
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    # write your code in Python 3.6
    if len(A) < 3:
        return 0
    A.sort()
    for i in range(0, len(A)-2):
        if A[i] + A[i+1] > A[i+2]:
            return 1
    return 0

python 不同

保存自https://app.codility.com/demo/results/trainingHXGYST-EBG/

distinct.py
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    # write your code in Python 3.6
    return len(set(A))

python MaxProductOfThree

保存自https://app.codility.com/demo/results/trainingFMMFE6-4SD/

maxProductOfThree.py
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    # write your code in Python 3.6
    A.sort()
    return max(A[0] * A[1] * A[2], A[-1] * A[-2] * A[-3], A[-1] * A[0] * A[1])

python MinAvgTwoSlice

保存自https://app.codility.com/demo/results/trainingH3NT3N-MDD/

minAvgTwoSlice.py
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    # write your code in Python 3.6
    preSum = [0] * len(A)
    for i in range(len(A)):
        preSum[i] = preSum[i-1] + A[i]
    Min, MinI = preSum[1] / 2, 0
    for i in range(2, len(preSum)):
        cur = (preSum[i] - preSum[i-2]) / 2
        if Min > cur:
            Min, MinI = cur, i - 1
    if len(A) <= 2:
        return MinI
    cur = preSum[2] / 3
    if Min > cur:
        Min, MinI = cur, 0
    for i in range(3, len(preSum)):
        cur = (preSum[i] - preSum[i-3]) / 3
        if Min > cur:
            Min, MinI = cur, i - 2
    return MinI

python MinPerimeterRectangle

保存自https://app.codility.com/demo/results/trainingXA43NT-MVC/

minPerimeterRectangle.py
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

import math

def solution(N):
    # write your code in Python 3.6
    i = int(math.sqrt(N))
    while i >= 1:
        if N % i == 0:
            return 2 * (N // i + i)
        i -= 1

python CountFactors

保存自https://app.codility.com/demo/results/trainingVFA3B4-6ZK/

countFactors.py
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(N):
    # write your code in Python 3.6
    i = 1
    Sum = 0
    while i * i < N:
        if N % i == 0:
            Sum += 2
        i += 1
    if i * i == N:
        Sum += 1
    return Sum

python MaxDoubleSliceSum

保存自https://app.codility.com/demo/results/training8XSQCQ-AFY/

maxDoubleSliceSum.py
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    # write your code in Python 3.6
    k1 = [0] * len(A)
    k2 = [0] * len(A)
    for i, a in enumerate(A[1:]):
        k1[i+1] = max(k1[i] + a, 0, a)
    for i, a in enumerate(A[::-1][1:]):
        k2[-2-i] = max(k2[-1-i] + a, 0, a)
    return max(k1[i-1] + k2[i+1] for i in range(1, len(A) - 1))