Skip to content

Instantly share code, notes, and snippets.

@divmgl
divmgl / perm_missing_elem.py
Created Mar 24, 2017
Codility.com PermMissingElem 100%/100%
View perm_missing_elem.py
def solution(A):
total = sum(A)
diff = sum(range(1, len(A) + 2))
return abs(diff - total)
@divmgl
divmgl / frogjmp.py
Created Mar 23, 2017
Codility.com FrogJmp 100%
View frogjmp.py
import math
def solution(X, Y, D):
return int(math.ceil(float(Y - X) / float(D)))
@divmgl
divmgl / cyclicrotation.py
Created Mar 23, 2017
Codility.com CyclicRotation 100%
View cyclicrotation.py
def solution(A, K):
if len(A) == 0:
return A
for _ in range(K):
A.insert(0, A.pop())
return A
@divmgl
divmgl / oddoccurrencesinarray.py
Last active Mar 23, 2017
Codility.com OddOccurrencesInArray 100%/100%
View oddoccurrencesinarray.py
def solution(A):
table = dict()
for num in A:
if not table.get(num):
table[num] = 1
else:
table.pop(num)
return table.keys()[0]
@divmgl
divmgl / binarygap.py
Last active Apr 8, 2017
Codility.com BinaryGap 100%
View binarygap.py
def solution(N):
max = 0
bits = 0
bin = "{0:b}".format(N)
for c in bin:
if c == "0":
bits += 1
if c == "1":
if max < bits:
@divmgl
divmgl / binarygap.c
Created Mar 23, 2017
Codility.com BinaryGap 100% in C
View binarygap.c
long bits = 0;
long max = 0;
void binary(long N) {
if (N > 1) {
binary(N / 2);
}
if (N % 2 == 0) {
bits += 1;
return;
@divmgl
divmgl / random.cpp
Last active Jan 7, 2017
Weighted random function
View random.cpp
#include <iostream>
#include <array>
#include <map>
#include <random>
using std::cout;
using std::endl;
using std::string;
using std::array;
@divmgl
divmgl / maxcounters.js
Created Dec 1, 2015
MaxCounters Javascript Solution 100%/100%
View maxcounters.js
function solution(N, A) {
var M = A.length; // Length of the entry array
var C = []; // Counters
var H = 0; // Highest counter
var PH = 0; // Previously recorded highest counter
for(K = 0; K < N; K++) { // Initialize the array
C[K] = 0;
}
@divmgl
divmgl / missinginteger.js
Created Dec 1, 2015
MissingInteger Javascript solution 100%/100%
View missinginteger.js
function solution(A) {
var F = []; // Found list
var I = 0, V = 0; // Counter, container
while (I < A.length) { // Iterate through the array
V = A[I]; // Store the value
I++; // Increase counter
if (F[V]) continue; // If the value exists, continue
F[V] = true; // Store the value
}
@divmgl
divmgl / permcheck.js
Created Dec 1, 2015
PermCheck Javascript 100%/100%
View permcheck.js
function solution(A) {
var L = A.length; // Length of the array
var X = ((L + 1) * L) / 2; // Sum from 1..L
var Y = 0; // Sum of evaluated values
var I = 0; // Counter
var F = []; // Found elements
var V = -1; // Container
while (I < L) { // Start searching the array
V = A[I]; // Get the value