Last active
August 29, 2015 13:57
-
-
Save dbernar1/9425754 to your computer and use it in GitHub Desktop.
An implementation of a solution to a "kata" at http://www.codewars.com/dojo/katas/5245a9138ca049e9a10007b8/train/javascript
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 countAdjacentPairs(search_string) { | |
var words_that_appeared_in_adjacent_pairs = []; | |
sets_of_two_adjacent_words_in( search_string ).forEach( function( two_adjacent_words ) { | |
var first_of_the_two_words = two_adjacent_words[ 0 ]; | |
if ( | |
two_adjacent_words.are_a_pair() | |
&& first_of_the_two_words.is_not_one_of( words_that_appeared_in_adjacent_pairs ) | |
) { | |
// which one reads more naturally? | |
//words_that_appeared_in_adjacent_pairs.add_word( first_of_the_two_words ); | |
add_to( words_that_appeared_in_adjacent_pairs, first_of_the_two_words ); | |
} | |
} ); | |
return words_that_appeared_in_adjacent_pairs.length; | |
} |
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
"use strict"; | |
Array.prototype.are_a_pair = function () { | |
var first_word_in_lowercase = this[ 0 ].toLowerCase(), | |
second_word_in_lowercase = this[ 1 ].toLowerCase(); | |
return first_word_in_lowercase === second_word_in_lowercase; | |
}; | |
add_to = function( collection, word ) { | |
collection.push( word.toLowerCase() ); | |
}; | |
Array.prototype.contains = function( element_to_check_for ) { | |
return -1 !== this.indexOf( element_to_check_for.toLowerCase() ); | |
}; | |
String.prototype.is_one_of = function( collection_to_check_in ) { | |
return collection_to_check_in.contains( this ); | |
}; | |
String.prototype.is_not_one_of = function( collection_to_check_in ) { | |
return ! this.is_one_of( collection_to_check_in ); | |
}; | |
function sets_of_two_adjacent_words_in( search_string ) { | |
var words_in_search_string = search_string.split( ' ' ), | |
sets = [], | |
num_words_in_search_string = words_in_search_string.length; | |
if ( num_words_in_search_string > 1 ) { | |
for( var i = 0; i < num_words_in_search_string - 1; i++ ) { | |
sets.push( [ words_in_search_string[ i ], words_in_search_string[ i+1 ] ] ); | |
} | |
} | |
return sets; | |
} |
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
Test.assertEquals( [ 'cat', 'dog' ].are_a_pair(), false, "'cat' and 'dog' are not a pair" ); | |
Test.assertEquals( [ 'dog', 'dog' ].are_a_pair(), true, "'dog' and 'dog' are a pair" ); | |
Test.assertEquals( [ 'dog', 'dOg' ].are_a_pair(), true, "'dog' and 'dOg' are a pair" ); | |
Test.assertEquals( 'dog'.is_not_one_of( [ 'cat' ] ), true, "'dog' is not one of [ 'cat' ]" ); | |
Test.assertEquals( 'dog'.is_one_of( [ 'dog' ] ), true, "'dog' is one of [ 'dog' ]" ); | |
Test.assertEquals( ['dog'].contains( 'dog' ), true, "[ 'dog' ] contains 'dog'" ); | |
Test.assertEquals( ['dog'].contains( 'cat' ), false, "[ 'dog' ] does not contain 'cat'" ); | |
Test.assertEquals( countAdjacentPairs('cat cat dog dog bird bird'), 3, 'countAdjacentPairs() counts adjacent pairs in provided string' ); | |
Test.assertEquals( countAdjacentPairs(''), 0, 'An empty string contains 0 adjacent pairs' ); | |
Test.assertEquals( countAdjacentPairs('dog cat dog'), 0, 'Only adjacent pairs are counted' ); | |
Test.assertEquals( countAdjacentPairs('dog dog dog'), 1, 'Pairs that repeat are only counted once' ); | |
Test.assertEquals( countAdjacentPairs('cat CaT dog dOg'), 2, 'Words are pairs even if the case of the letters is different' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment