This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function find(array, searchCriteria) { | |
let current = array | |
let next = [] | |
while (current) { | |
if (searchCriteria(current)) { | |
return current |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.slice.call(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |