Skip to content

Instantly share code, notes, and snippets.

@kevincolten
Last active September 13, 2015 15:37
Show Gist options
  • Save kevincolten/c898e3db79b69d130d66 to your computer and use it in GitHub Desktop.
Save kevincolten/c898e3db79b69d130d66 to your computer and use it in GitHub Desktop.
Algorithms
00000 claimable
00001 reinstate
00002 relaunching
00003 prototyping
00004 signpost
00005 trilled
00006 deftness
00007 icecap
00008 comprehensive
00009 umbrage
00010 postmodern
00011 stricter
00012 calendars
00013 marvels
00014 documenting
00015 condensate
00016 procurement
00017 transversely
00018 senate
00019 numbing
00020 electioneering
00021 inveighing
00022 pleated
00023 pupating
00024 endured
00025 scaffolding
00026 sprouts
00027 motif
00028 tailor
00029 brasilia
00030 ankles
00031 headstock
00032 cord
00033 frocks
00034 raining
00035 blanketed
00036 fallacies
00037 equaliser
00038 foreground
00039 kindergartens
00040 mistrusting
00041 jackasses
00042 scatological
'use strict';
// https://www.youtube.com/watch?v=kPRA0W1kECg
var LineByLineReader = require('line-by-line');
var prompt = require('prompt');
var wordList = new LineByLineReader('./sample_words.txt');
var words = [];
wordList.on('line', function (line) {
words.push(line.slice(6, line.length));
});
wordList.on('end', function () {
bubbleSort(words);
prompt.get('word', function(error, result) {
console.log(iterativeSearch(result.word, words));
});
});
function switchIdx(arr, idx1, idx2) {
var temp = arr[idx1];
arr[idx1] = arr[idx2];
arr[idx2] = temp;
}
function bubbleSort(arr) {
var sorted = false;
while (sorted === false) {
sorted = true;
for (var i = 1; i < arr.length; i++) {
if (arr[i - 1] > arr[i]) {
sorted = false;
switchIdx(arr, i - 1, i);
}
}
}
return arr;
}
function mergeSort(arr) {
var midIdx = Math.floor((arr.length) / 2);
var firstHalf = list.slice(0, midIdx);
var secondHalf = list.slice(midIdx, list.length);
// TODO finish
}
var foundRecusively = false;
function recursiveSearch(word, list) {
// base cases
if (list.length === 1) {
if (list[0] === word) {
foundRecusively = true;
return;
}
return;
} else if (list.length === 0) {
return;
}
var midIdx = Math.floor((list.length) / 2);
var firstHalf = list.slice(0, midIdx);
var secondHalf = list.slice(midIdx, list.length);
// call ourselves
if (word < secondHalf[0]) {
searchWord(word, firstHalf);
} else {
searchWord(word, secondHalf);
}
return foundRecusively;
}
function iterativeSearch(word, list) {
var midIdx = Math.floor((list.length) / 2);
var foundIteratively = false;
while (foundIteratively === false || list.length === 0) {
// base cases
if (list.length === 0) {
return false;
} else if (list.length === 1) {
if (list[0] === word) {
return true;
}
return false;
}
for (var i = 0; i < midIdx; i++) {
if (list[i] === word) {
foundIteratively = true;
break;
}
}
list = list.slice(midIdx, list.length);
midIdx = Math.floor((list.length) / 2);
}
return foundIteratively;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment