Skip to content

Instantly share code, notes, and snippets.

@epan
Created May 30, 2017 17:06
Show Gist options
  • Save epan/89c5f86f6dbda59a38edd758d178d811 to your computer and use it in GitHub Desktop.
Save epan/89c5f86f6dbda59a38edd758d178d811 to your computer and use it in GitHub Desktop.
Double Vowels
// THE PROBLEM
// ----------------------------------
// https://gist.github.com/tim-hr/3bae7341ca7e566748c13726964d8535
// I: 1 array of 1 char per index
// O: 1 array like the Inut but with each vowel duplicated
// C: No add'l data structures. O(n)
// E: None yet
// PLAN
// ----------------------------------
// For each index, check if vowel
// If the char is a vowel,
// Insert the same char before itself
// Return array
// I don't think this is linear time complexity :(
function doubleVowels (array) {
array.forEach((char, index) => {
if (isVowel(char)) {
array = duplicateBefore(index, array)
}
})
return array
}
function duplicateBefore (index, array) {
array.splice(index, 0, array[index]);
return array
}
function isVowel (char) {
return ['a', 'e', 'i', 'o', 'u'].includes(char)
}
// TEST HELPERS
// ----------------------------------
function arrayComparison (target, source, result, description) {
const actual = target
const expected = source
_.isEqual(expected, actual) === result
? console.log(`PASS: ${description}`)
: console.error(`FAIL: ${description}. Expected ${expected} but got ${actual}`)
}
function willDetectVowel (letter, expected, description) {
const actual = isVowel(letter)
isVowel(expected) === isVowel(actual)
? console.log(`PASS: ${description}`)
: console.error(`FAIL: ${description}. Expected ${expected} but got ${actual}`)
}
function willDoubleVowels (input, expected, result, description) {
const actual = doubleVowels(input)
_.isEqual(expected, actual) === result
? console.log(`PASS: ${description}`)
: console.error(`FAIL: ${description}. Expected ${expected} but got ${actual}`)
}
// TESTS
// ----------------------------------
willDetectVowel('a', true, `It should find 'a' to be a vowel`)
willDetectVowel('b', false, `It should notfind 'b' to be a vowel`)
arrayComparison([1], [1], true, 'It should detect one and one are equal')
arrayComparison([1], [2], false, 'It should detect one and two are not equal')
willDoubleVowels(['a'], ['a', 'a'], true, `It should double an array of 1 vowel`)
willDoubleVowels(['a', 'b'], ['a', 'a', 'b'], false, `It should double an array with vowel and consonant`)
willDoubleVowels(['b'], ['b'], true, `It should return same array if no vowels`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment