Skip to content

Instantly share code, notes, and snippets.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@xeonqq
xeonqq / Codility_MinMaxDivision.py
Last active August 29, 2015 14:26
Divide array A into K blocks and minimize the largest sum of any block.
def blockPossible(A, bound, K):
s = A[0]
blocks = 1;
for a in A[1:]:
s += a
if s>bound:
blocks+=1
s=a
if blocks > K:
return False
@xeonqq
xeonqq / codility_NumberSolitaire.py
Created August 31, 2015 11:21
In a given array, find the subset of maximal sum in which the distance between consecutive elements is at most 6.
# you can use print for debugging purposes, e.g.
# print "this is a debug message"
def solution(A):
n = len(A)
MIN_INT = -n * 20000
v = [A[0]]+(n-1)*[MIN_INT]
for i in xrange(1, n):
for j in xrange(1, 7):
if j <= i:
@xeonqq
xeonqq / MaxNonoverlappingSegments.py
Created September 1, 2015 13:52
Codility MaxNonoverlappingSegments: Find a maximal set of non-overlapping segments.
def solution(A, B):
# write your code in Python 2.7
pre = -1
N = len(A)
ans=0
for i in xrange(N):
if A[i] > pre:
ans+=1
pre = B[i]
return ans
@xeonqq
xeonqq / Triangle.py
Created September 4, 2015 11:57
Determine whether a triangle can be built from a given set of edges.
def solution(A):
# write your code in Python 2.7
A.sort()
for i in xrange(len(A)-2):
if (((A[i] + A[i+1]) > A[i+2]) and ((A[i+2] + A[i+1]) > A[i]) and ((A[i+2] + A[i]) > A[i+1])):
return 1
return 0
@xeonqq
xeonqq / NumberOfDiscIntersections.py
Created September 4, 2015 16:13
Compute the number of intersections in a sequence of discs. follows the solution described here: http://stackoverflow.com/questions/4801242/algorithm-to-calculate-number-of-intersecting-discs
def solution(A):
# write your code in Python 2.7
B = []
for i in xrange(len(A)):
B.append((i-A[i],i+A[i]))
B = sorted(B, key=lambda x:x[0])
cnt = 0
for i in xrange(len(A)-1):
right = B[i][1]
start = i+1
@xeonqq
xeonqq / MaxDoubleSliceSum.py
Created September 6, 2015 22:34
Find the maximal sum of any double slice.
def solution(A):
# write your code in Python 2.7
n = len(A)
if n <=3:
return 0
leftMax = [0]*n
rightMax = [0]*n
for i in xrange(2,n-1):
leftMax[i] = max(0,leftMax[i-1]+A[i-1])
for i in xrange(n-3,0,-1):
@xeonqq
xeonqq / Equi.py
Created September 8, 2015 13:34
Find an index in an array such that its prefix sum equals its suffix sum.
def solution(A):
# write your code in Python 2.7
s = []
tmp = 0
n = len(A)
for a in A:
tmp = a + tmp
s.append(tmp)
s.append(0)
@xeonqq
xeonqq / try_bluetooth_function.c
Created December 3, 2015 14:24
try to find the baudrate of the bluetooth module (not tested)
void tryBT()
{
delay(20000);
led_green_flash();
long baudrates[5] = {9600, 19200, 38400, 57600, 115200};
String inputString="";
for (int i =0; i< 5; i++)
{
DebugSerial.begin(baudrates[i]); // debug output
@xeonqq
xeonqq / MinPerimeterRectangle.py
Created July 7, 2017 14:26
MinPerimeterRectangle. Find the minimal perimeter of any rectangle whose area equals N.
from math import sqrt
def solution(N):
# write your code in Python 2.7
minperimeter = 2*(N + 1)
if N == 1:
return minperimeter
denominator = 2