Skip to content

Instantly share code, notes, and snippets.

View divmgl's full-sized avatar

Dexter Miguel divmgl

  • San Francisco, CA
  • 12:52 (UTC -12:00)
View GitHub Profile
@divmgl
divmgl / permcheck.js
Created December 1, 2015 06:09
PermCheck Javascript 100%/100%
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
@divmgl
divmgl / random.cpp
Last active January 7, 2017 05:05
Weighted random function
#include <iostream>
#include <array>
#include <map>
#include <random>
using std::cout;
using std::endl;
using std::string;
using std::array;
@divmgl
divmgl / oddoccurrencesinarray.py
Last active March 23, 2017 21:11
Codility.com OddOccurrencesInArray 100%/100%
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 / cyclicrotation.py
Created March 23, 2017 21:20
Codility.com CyclicRotation 100%
def solution(A, K):
if len(A) == 0:
return A
for _ in range(K):
A.insert(0, A.pop())
return A
@divmgl
divmgl / frogjmp.py
Created March 23, 2017 21:31
Codility.com FrogJmp 100%
import math
def solution(X, Y, D):
return int(math.ceil(float(Y - X) / float(D)))
@divmgl
divmgl / perm_missing_elem.py
Created March 24, 2017 00:16
Codility.com PermMissingElem 100%/100%
def solution(A):
total = sum(A)
diff = sum(range(1, len(A) + 2))
return abs(diff - total)
@divmgl
divmgl / binarygap.py
Last active April 8, 2017 09:48
Codility.com BinaryGap 100%
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 / missinginteger.js
Created December 1, 2015 06:28
MissingInteger Javascript solution 100%/100%
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 / maxcounters.js
Created December 1, 2015 07:23
MaxCounters Javascript Solution 100%/100%
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 / frogriverone.js
Created December 1, 2015 05:50
FrogRiverOne JavaScript solution 100%/100%
function solution(X, A) {
if (A.length === 1) { // If the array is one element
// And if its first item is 1 as well as the value to search for,
// the frog doesn't need to move
if (A[0] === 1 && X === 1) return 0;
// If not, it's impossible to go anywhere
else return -1;
}
var i = -1, // Counter