python 平均编码

mean_encoding
means = X_tr.groupby(col).target.mean()
train_new[col+'_mean_target'] = train_new[col].map(means)
val_new[col+'_mean_target'] = val_new[col].map(means)

python AWS Backup删除

根据删除日期删除快照

backup.js
if created_on <= deletion_date and can_delete == True:
                      print('Snapshot id {0}, ({1}) from {2} is {3} or more days old... deleting'.format(snap.id, name, created_on_string, delete_after_days))
                      deleted_size_counter += snap.volume_size
                      snap.delete()
                      deletion_counter += 1

python MaxSliceSum

保存自https://app.codility.com/demo/results/trainingDKKM2X-FZS/

maxSliceSum.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
    s, maxS = 0, -float("inf")
    for a in A:
        if s > 0:
            s += a
        else:
            s = a
        if s > maxS:
            maxS = s
    return maxS

python MAXPROFIT

保存自https://app.codility.com/demo/results/training5WHWXE-HMR/

profit.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 A == []:
        return 0
    cost = [0] * (len(A)-1)
    for i in range(len(A)-1):
        cost[i] = A[i+1] - A[i]
    s = 0
    maxS = 0
    for c in cost:
        if s > 0:
            s += c
        else:
            s = c
        if s > maxS:
            maxS = s
    return max(maxS, 0)

python 石墙

保存自https://app.codility.com/demo/results/trainingVYC6T5-QRH/

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

def solution(H):
    # write your code in Python 3.6
    Sum = 1
    stack = [H[0]]
    for i in range(1, len(H)):
        while stack != []:
            if stack[-1] > H[i]:
                stack.pop()
            else:
                break
        if stack == [] or stack[-1] < H[i]:
            Sum += 1
            stack.append(H[i])
    return Sum

python 嵌套

保存自https://app.codility.com/demo/results/trainingB4FVP3-W6J/

nesting.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
    stack = []
    for s in S:
        if s == "(":
            stack.append(s)
        else:
            if stack == []:
                return 0
            stack.pop()
    if stack != []:
        return 0
    return 1

python

保存自https://app.codility.com/demo/results/trainingYWY6S2-FR2/

fish.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
    stack = []
    Sum = len(B)
    for i in range(len(B)):
        if B[i] == 1:
            stack.append(i)
        else:
            while stack != []:
                Sum -= 1
                if A[i] > A[stack[-1]]:
                    stack.pop()
                else:
                    break
    return Sum

python 括号

保存自https://app.codility.com/demo/results/training3ZSA4B-33R/

brackets.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
    d = {"(":")","[":"]","{":"}"}
    stack = []
    for s in S:
        if s in d:
            stack.append(s)
        else:
            if stack == [] or d[stack.pop()] != s:
                return 0
    if stack != []:
        return 0
    return 1

python MaxCounters

保存自https://app.codility.com/demo/results/training47QAPG-U3E/

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

def solution(N, A):
    # write your code in Python 3.6
    curN, maxN = 0, 0
    result = [0] * (N + 1)
    for a in A:
        if a == N + 1:
            maxN = curN
        else:
            if result[a] < maxN:
                result[a] = maxN
            result[a] += 1
            if result[a] > curN:
                curN = result[a]
    for i in range(1, N+1):
        if result[i] < maxN:
            result[i] = maxN
    return result[1:]

python FrogRiverOne

保存自https://app.codility.com/demo/results/trainingJB9WZX-DJ7/

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

def solution(X, A):
    # write your code in Python 3.6
    count = [0] * (X + 1)
    Sum = 0
    for i, a in enumerate(A):
        if a <= X and count[a] != 1:
            count[a] = 1
            Sum += 1
            if Sum == X:
                return i
    return -1