Skip to content

Instantly share code, notes, and snippets.

@rowinf
Last active January 17, 2025 02:53
Show Gist options
  • Save rowinf/74315c2160cbb67ec9ea1184b55a7a97 to your computer and use it in GitHub Desktop.
Save rowinf/74315c2160cbb67ec9ea1184b55a7a97 to your computer and use it in GitHub Desktop.
/**
* generate unique lyrics so the syllables add up to a number
*/
const WORDS = [null, "gang", "gucci"]
function syllableLength(lyrics) {
return lyrics.reduce((total, word) => {
return total + WORDS.indexOf(word)
}, 0)
}
function nextWord(lines, lyrics, count) {
// return words if it meets the "base case" length count
const slength = syllableLength(lyrics)
if (syllableLength(lyrics) === count) {
lines.add(lyrics.join(" "))
return
} else if (syllableLength(lyrics) > count) {
// else if it exceeds - return words + word[1]
lyrics.pop()
lyrics.push(WORDS[1])
lines.add(lyrics.join(" "))
return
} else if (syllableLength(lyrics) > count) {
// ignore otherwise
return
}
// thinking like a binary tree helps
let left = [...lyrics, WORDS[1]]
let right = [...lyrics, WORDS[2]]
nextWord(lines, right, count)
nextWord(lines, left, count)
}
function generateLyrics(count) {
const lines = new Set() // Set for asserting uniqueness
nextWord(lines, [], count)
return lines
}
let input = Number(process.argv[2])
console.log([...generateLyrics(input)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment