Skip to content

Instantly share code, notes, and snippets.

@wanyanxie
Last active March 29, 2016 06:55
Show Gist options
  • Save wanyanxie/666ad1585c811cacaee7 to your computer and use it in GitHub Desktop.
Save wanyanxie/666ad1585c811cacaee7 to your computer and use it in GitHub Desktop.
MSAN 622 Homework 2: Javascript Anagrams (4/24)

Due 5pm PST Tuesday 3/29

For this homework you will submit as a fork of this gist

Create a Javascript function to find asociated anagrams in an input list of strings. For the input list, output every string (only once) that has an associated anagram elsewhere in the input list. See an example input and output below:

input_list = ['man', 'list', 'acme', 'talk', 'cat', 'beach', 'came', 'tac', 'naan', 'slit', 'act']

var anagram_finder = function(list) {

  // WRITE SOLUTION HERE...

};

var output = anagram_finder(input_list);

console.log(output);
// => ['list', 'acme', 'slit', 'cat', 'came', 'tac', 'act']

NOTE: The output words can be in any order

--

This Gist is part of the Spring 2016 University of San Francisco MSAN 622 course: Introduction to Data and Information Visualization (taught by Jonathan Dinu). Course materials are openly available on the class website (with lectures broadcast on Youtube) for the curious autodidact.

var anagram_finder = function(input) {
var anagrams = []
for ( var i = 0; i < input.length; i++) {
var word = input[i];
var sorted = word.split("").sort().join("");
if (anagrams[sorted] != null) {
anagrams[sorted].push(word);
}
else {
anagrams[sorted] = [word];
}
}
return output
}
input_list = ['man', 'list', 'acme', 'talk', 'cat', 'beach', 'came', 'tac', 'naan', 'slit', 'act']
var output = anagram_finder(input_list);
console.log(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment