python FloodDepth

保存自https://app.codility.com/demo/results/trainingAYY9XW-3AM/

floodDepth.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
    lefMax = [0] * len(A)
    rigMax = [0] * len(A)
    for i in range(len(A)):
        lefMax[i] = max(A[i], lefMax[i-1])
    rigMax[-1] = A[-1]
    for i in range(len(A)-2, -1, -1):
        rigMax[i] = max(rigMax[i+1], A[i])
    maxDepth = 0
    for i in range(1, len(A)-1):
        l, r = lefMax[i-1], rigMax[i+1]
        if l > A[i] and r > A[i]:
            maxDepth = max(maxDepth, min(l, r) - A[i])
    return maxDepth

python FloodDepth

保存自https://app.codility.com/demo/results/trainingAYY9XW-3AM/

floodDepth.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
    lefMax = [0] * len(A)
    rigMax = [0] * len(A)
    for i in range(len(A)):
        lefMax[i] = max(A[i], lefMax[i-1])
    rigMax[-1] = A[-1]
    for i in range(len(A)-2, -1, -1):
        rigMax[i] = max(rigMax[i+1], A[i])
    maxDepth = 0
    for i in range(1, len(A)-1):
        l, r = lefMax[i-1], rigMax[i+1]
        if l > A[i] and r > A[i]:
            maxDepth = max(maxDepth, min(l, r) - A[i])
    return maxDepth

python LongestPassword

保存自https://app.codility.com/demo/results/trainingABGSHR-ZEE/

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

def solution(S):
    # write your code in Python 3.6
    maxL, length = -1, 0
    isPasswd = True
    numCount, letCount = 0, 0
    for s in S:
        if s == " ":
            if isPasswd and numCount % 2 == 1 and letCount % 2 == 0:
               maxL = max(maxL, length)
            isPasswd = True
            numCount, letCount, length = 0, 0, 0
        elif not isPasswd:
            continue
        elif '0' <= s <= '9':
            numCount += 1
            length += 1
        elif 'a' <= s <= 'z' or 'A' <= s <= 'Z':
            length += 1
            letCount += 1
        else:
            isPasswd = False
    if isPasswd and numCount % 2 == 1 and letCount % 2 == 0:
        maxL = max(maxL, length)
    return maxL

python LongestPassword

保存自https://app.codility.com/demo/results/trainingABGSHR-ZEE/

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

def solution(S):
    # write your code in Python 3.6
    maxL, length = -1, 0
    isPasswd = True
    numCount, letCount = 0, 0
    for s in S:
        if s == " ":
            if isPasswd and numCount % 2 == 1 and letCount % 2 == 0:
               maxL = max(maxL, length)
            isPasswd = True
            numCount, letCount, length = 0, 0, 0
        elif not isPasswd:
            continue
        elif '0' <= s <= '9':
            numCount += 1
            length += 1
        elif 'a' <= s <= 'z' or 'A' <= s <= 'Z':
            length += 1
            letCount += 1
        else:
            isPasswd = False
    if isPasswd and numCount % 2 == 1 and letCount % 2 == 0:
        maxL = max(maxL, length)
    return maxL

python NumberSolitaire

保存自https://app.codility.com/demo/results/trainingRAVSHS-V8K/

numberSolitaire.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
    maxL = [A[0]] + [-float("inf")] * 5
    index = 0
    for i in range(1, len(A)):
        index = (index + 1) % 6
        maxL[index] = max(maxL) + A[i]
    return maxL[index]

python MaxNonoverlappingSegments

保存自https://app.codility.com/demo/results/trainingNGS6Z5-DQS/

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

def solution(A, B):
    # write your code in Python 3.6
    Sum, end = 0, -1
    for i in range(len(A)):
        if A[i] > end:
            Sum += 1
            end = B[i]
    return Sum

python TieRopes

保存自https://app.codility.com/demo/results/training76MSHS-WHV/

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

def solution(K, A):
    # write your code in Python 3.6
    Sum, length = 0, 0
    for a in A:
        length += a
        if length >= K:
            Sum += 1
            length = 0
    return Sum

python CountSemiprimes

保存自https://app.codility.com/demo/results/training7C68SH-C5T/

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

from math import sqrt

def isPrime(a):
    if a == 1:
        return False
    if a == 2:
        return True
    for i in range(int(sqrt(a)), 1, -1):
        if a % i == 0:
            return False
    return True

def solution(N, P, Q):
    # write your code in Python 3.6
    primes = [i for i in range(2, N+1) if isPrime(i)]
    counter = [0] * (N + 1)
    for i in range(len(primes)):
        for j in range(i, len(primes)):
            k = primes[i] * primes[j]
            if k < N + 1:
                counter[k] = 1
            else:
                break
    preSum = [0] * (N + 1)
    for i in range(2, N+1):
        if counter[i] == 1:
            preSum[i] += 1
        preSum[i] += preSum[i-1]
    return [preSum[Q[i]]-preSum[P[i]]+counter[P[i]] for i in range(len(P))]

python CountNonDivisible

保存自https://app.codility.com/demo/results/training4H636A-YAW/

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

from math import sqrt

def divisors(a):
    if a == 1:
        return [1]
    if a <= 3:
        return [1, a]
    query = [1, a]
    start = int(sqrt(a))
    for i in range(2, start):
        if a % i == 0:
            query.extend([i, a // i])
    if a % start == 0:
        if a == start * start:
            query.append(start)
        else:
            query.extend([start, a // start])
    return query

def solution(A):
    # write your code in Python 3.6
    counter = [0] * (2 * len(A) + 1)
    for a in A:
        counter[a] += 1
    divisor = [None] * (2 * len(A) + 1)
    for a in A:
        if divisor[a] is None:
            divisor[a] = sum(counter[q] for q in divisors(a))
    result = [len(A)] * len(A)
    for i, a in enumerate(A):
        result[i] -= divisor[a]
    return result

python 根据条件创建新列

FM DF.py
# A new column is created and populated based on conditions applying to the CommentsLow column
# If a string is found in CommentsLow, a new flag value will be created in the Success label column 

# It is a good practice to set all strings to lower case
df["CommentsLow"] = df["Comments"].str.lower()

# If the string "stopped" or "terminated" is found then the "Failed" label will be applied, otherwise the "OK" label will be used
df["Success label"] = pd.np.where(df["CommentsLow"].str.contains("stopped"), "Failed",
                      pd.np.where(df["CommentsLow"].str.contains("terminated"), "Failed", "OK"))