Skip to content

Instantly share code, notes, and snippets.

View newmind's full-sized avatar
🎯
Focusing

JG newmind

🎯
Focusing
View GitHub Profile
@newmind
newmind / ChocolatesByNumbers.py
Created October 29, 2017 14:33
1. ChocolatesByNumbers - There are N chocolates in a circle. Count the number of chocolates you will eat.
# 100/25 %
# https://codility.com/demo/results/trainingFXETBT-37D/
def solution(N, M):
# write your code in Python 2.7
X = 0
count = 1
result = [0]
while True:
@newmind
newmind / CountNonDivisible.py
Last active October 29, 2017 13:22
1. CountNonDivisible - Calculate the number of elements of an array that are not divisors of each element.
# 1차 100/25
# https://codility.com/demo/results/training8VZAZX-573/
import bisect
def solution(A):
# write your code in Python 2.7
N = len(A)
A2 = A[:]
@newmind
newmind / CountSemiprimes.py
Created October 29, 2017 09:54
1. CountSemiprimes Count the semiprime numbers in the given range [a..b]
# https://codility.com/demo/results/training3WQQR7-7GW/
def primality(n):
i=2
while(i*i<=n):
if (n % i == 0):
return False
i += 1
return True
@newmind
newmind / Picks.py
Last active October 29, 2017 04:11
1. Peaks - Divide an array into the maximum number of same-sized blocks, each of which should contain an index P such that A[P - 1] < A[P] > A[P + 1].
# 100%/100%
# 코드를 정리해야하는데.
# https://codility.com/demo/results/trainingUZ442K-7GV/
def solution(A):
# write your code in Python 2.7
N = len(A)
if N < 3:
return 0
@newmind
newmind / Flags.py
Last active October 29, 2017 08:23
# 1차 시도 75% / 28%
# https://codility.com/demo/results/training6CWMMR-TDG/
# 2차 시도 75% / 42%
# https://codility.com/demo/results/trainingJ28BCV-GHW/
# 3차 100% / 100%
# https://codility.com/demo/results/trainingSN3WBN-QAA/
def solution(A):
@newmind
newmind / CountFactors.py
Created October 28, 2017 21:25
1. CountFactors - Count factors of given number n.
# https://codility.com/demo/results/trainingEYVYU7-76G/
import math
def solution(N):
if N <= 2: return N
root = int(round(math.sqrt(N)))
count = 0
@newmind
newmind / MaxProfit.py
Created October 28, 2017 18:41
1. MaxProfit - Given a log of stock prices compute the maximum possible earning.
# https://codility.com/demo/results/trainingQ8Y3XP-RGC/
def solution(A):
# write your code in Python 2.7
N = len(A)
if N < 2:
return 0
lower = A[0]
maxProfit = 0
@newmind
newmind / MaxSliceSum.py
Created October 28, 2017 09:08
1. MaxSliceSum - Find a maximum sum of a compact subsequence of array elements.
# performance 100/100
# https://codility.com/demo/results/trainingRW2RK4-3UA/
def solution(A):
# write your code in Python 2.7
max_ending = 0
max_slice = -2147483648
for a in A:
max_ending = max(a, max_ending + a)
max_slice = max(max_slice, max_ending)
@newmind
newmind / EquiLeader.py
Last active October 27, 2017 12:55
1. EquiLeader - Find the index S such that the leaders of the sequences A[0], A[1], ..., A[S] and A[S + 1], A[S + 2], ..., A[N - 1] are the same.
# performance 100%
# https://codility.com/demo/results/trainingAM667U-NCM/
NO_LEADER = -1000000001
# NO_LEADER = -1
def solution(A):
# write your code in Python 2.7
N = len(A)
if N < 2:
@newmind
newmind / Dominator.py
Created October 27, 2017 09:33
1. Dominator - Find an index of an array such that its value occurs at more than half of indices in the array.
# https://codility.com/demo/results/trainingRF8B69-7SJ/
def solution(A):
# write your code in Python 2.7
N = len(A)
if N <= 1:
return N-1
A2 = A[:]
A2.sort()