Skip to content

Instantly share code, notes, and snippets.

View adames's full-sized avatar

Adames Hodelin adames

  • New York, NY
View GitHub Profile
@adames
adames / hashtable.js
Last active May 13, 2023 18:52
simple hash table
// Implement hash table
let hash = (string, max) => {
let hash = 0;
for (let i = 0; i< string.length; i++) {
hash += string.charCodeAt(i);
}
return hash % max;
}
@adames
adames / reverseSentence.js
Created May 13, 2023 18:15
Reverse sentence function
// Create a method that reverses a sentence, no punctuation
let sentence = "the quick brown fox jumped over the lazy dog"
if (process.argv.length > 2) {
sentence = process.argv.slice(2).join(" ");
}
let reversedSentence= [];
function reverseSentence(sentence) {
let splitSentence = sentence.split(" ");
for (let index = splitSentence.length - 1; index >= 0; index--) {
reversedSentence.push(splitSentence[index]);
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 October 13, 2017 20:38
Splitting an array between an integer for a quicksort method
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 October 16, 2017 17:36
Very concise quicksort
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 August 16, 2017 03:42
A* search for 8 puzzles
# 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 July 20, 2017 15:13
Coerce args or nodes an to array
Array.prototype.slice.call(args)
function find(array, searchCriteria) {
let current = array
let next = []
while (current) {
if (searchCriteria(current)) {
return current