Last active
August 29, 2015 14:15
-
-
Save alexhawkins/0a3a3ae7dbbb22eb28b8 to your computer and use it in GitHub Desktop.
Code Challengees
This file contains hidden or 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
//1) reverse a string without using any variables or | |
/*****************************************************/ | |
function reverseString(s) { | |
if (s === '') | |
return ''; | |
else | |
return s.slice(1) + s[0]; | |
} | |
var reversed = reverseString('alexander'); | |
console.log(reversed); | |
//2) Create a linkedList and write add, remove and reverse methods | |
/*****************************************************/ | |
function LinkedList() { | |
this.head = null; | |
} | |
LinkedList.prototype.makeNode = function(value) { | |
var node = {}; | |
node.value = value; | |
node.next = null; | |
return node; | |
} | |
LinkedList.prototype.add = function(value) { | |
var currentNode = this.makeNode(value); | |
if (this.head === null) { | |
this.head = currentNode; | |
} else { | |
var current = this.head; | |
while (current.next) { | |
current = current.next; | |
} | |
current.next = currentNode; | |
} | |
} | |
LinkedList.prototype.remove = function(value) { | |
var current = this.head; | |
if (!current) | |
return null; | |
//if the head is the value to remove, | |
//replace the head with the next node | |
if (current.value === value) { | |
this.head = current.next; | |
} | |
//else the node we are seeking is somewhere in the middle | |
else { | |
var previous = current; | |
while (current) { | |
if (current.value === value) { | |
previous.next = current.next; | |
break; | |
} | |
previous = current; | |
current = current.next; | |
} | |
} | |
} | |
LinkedList.prototype.reverse = function() { | |
var current = this.head; | |
if (!current || !current.next) | |
return this; | |
else { | |
var previous = null; | |
var save; | |
while (current) { | |
save = current.next; | |
current.next = previous; | |
previous = current; | |
current = save; | |
} | |
this.head = previous; | |
} | |
return this; | |
} | |
LinkedList.prototype.contains = function(value) { | |
var current = this.head; | |
if (!current) { | |
return null | |
} else { | |
while (current) { | |
if (current.value === value) { | |
return true; | |
} | |
current = current.next; | |
} | |
return false; | |
} | |
} | |
var list = new LinkedList(); | |
list.add(10); | |
list.add(20); | |
list.add(30); | |
//list.remove(10); | |
console.log(list); | |
console.log(list.reverse()); | |
console.log(list.contains(30)); | |
console.log(list.contains(60)); | |
//3) Given two sorted arrays, i.e. [1,2,3,4,8,20] and [2,4,4,8] | |
//combine them into one sorted array | |
/*****************************************************/ | |
//4) /return an array with the anagrams grouped together. Makes sure the anagrams are unique. | |
/*****************************************************/ | |
var arr = ['cat', 'dog', 'tac', 'god', 'act']; | |
var allAnagrams = function(arr) { | |
var anagrams = {}; | |
arr.forEach(function(str) { | |
var recurse = function(ana, str) { | |
if (str === '') { | |
anagrams[ana] = 1; | |
} | |
for (var i = 0; i < str.length; i++) { | |
console.log('STR[i]', str[i]); | |
recurse(ana + str[i], str.slice(0, i) + str.slice(i + 1)); | |
} | |
} | |
recurse('', str); | |
}); | |
return Object.keys(anagrams); | |
} | |
//5) check to see if a word a palindrome | |
var isPalindrome = function(word){ | |
var stack = ''; | |
var queue = ''; | |
for(var i = 0; i < word.length; i++){ | |
stack += word[i]; | |
queue += word[word.length - i - 1]; | |
} | |
console.log(stack); | |
console.log(queue); | |
return stack === queue ? true : false; | |
}; | |
var result = isPalindrome('good'); | |
console.log(result); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment