Skip to content

Instantly share code, notes, and snippets.

Avatar

Adames Hodelin adames

View GitHub Profile
View UUID_generator.js
function generateUUID () {
let time = new Date().getTime();
if (typeof performance !== 'undefined' && typeof performance.now === 'function'){
time += performance.now();
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
let random = (time + Math.random() * 16) % 16 | 0;
time = Math.floor(time / 16);
return (c === 'x' ? random : (random & 0x3 | 0x8)).toString(16);
});
@adames
adames / partition.rb
Created Oct 13, 2017
Splitting an array between an integer for a quicksort method
View partition.rb
def partition(array, pivot)
return [], [] if array.empty?
j = 0
for i in 0...array.length
if array[i] <= pivot
array[j], array[i] = array[i], array[j]
j += 1
end
end
return array[0...j], array[j..-1]
@adames
adames / quicksort.rb
Last active Oct 16, 2017
Very concise quicksort
View quicksort.rb
class Array
def quicksort
return [] if empty?
pivot = delete_at(rand(size))
left, right = partition(&pivot.method(:>))
return *left.quicksort, pivot, *right.quicksort
end
end
@adames
adames / eight_puzzle_solver.rb
Last active Aug 16, 2017
A* search for 8 puzzles
View eight_puzzle_solver.rb
# finds path to board solution
def a_star_search(board)
tree = []
leaves = []
solution = [1, 2, 3, 4, 5, 6, 7, 8, 9]
leaf = build_leaf(board, {board: nil, branch: []})
i = 0
while i < 10000 && leaf[:board] != solution
@adames
adames / argsToArray.js
Last active Jul 20, 2017
Coerce args or nodes an to array
View argsToArray.js
Array.prototype.slice.call(args)
View breadthFirstSearch.js
function find(array, searchCriteria) {
let current = array
let next = []
while (current) {
if (searchCriteria(current)) {
return current
You can’t perform that action at this time.