Last active
October 20, 2019 09:00
-
-
Save Ryan1729/b9bfeb1d3a919ae09df1736a1477494c to your computer and use it in GitHub Desktop.
Generating interesting variants on a classic Dr. Seuss poem with a quick and dirty script
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
// | |
// SO copy-pasta | |
// | |
function perm(xs) { | |
let ret = []; | |
for (let i = 0; i < xs.length; i = i + 1) { | |
let rest = perm(xs.slice(0, i).concat(xs.slice(i + 1))); | |
if(!rest.length) { | |
ret.push([xs[i]]) | |
} else { | |
for(let j = 0; j < rest.length; j = j + 1) { | |
ret.push([xs[i]].concat(rest[j])) | |
} | |
} | |
} | |
return ret; | |
} | |
// fuck it, let's live dangerously and slow. | |
String.prototype.replaceAll = function(search, replacement) { | |
var target = this; | |
return target.replace(new RegExp(search, 'g'), replacement); | |
}; | |
// | |
// | |
// | |
const story = `My N0 is A0, my N1 are A1. | |
I have a N2 I like to hold. | |
My N3 is A2, my N4 is A3. | |
My N3 is A2, my N4 is A3. | |
I have a N2 I like to hold. | |
My N0 is A0, my N1 are A1. | |
And now my story is all told.` | |
const nouns = ["hat", "teeth", "bird", "shoe", "foot"] | |
const adjectives = ["old", "gold", "off", "cold"] | |
const nPerms = perm(nouns) | |
const aPerms = perm(adjectives) | |
let output = "" | |
let sep = "" | |
for (let i = 0; i < nPerms.length; i += 1) { | |
outer: | |
for (let j = 0; j < aPerms.length; j += 1) { | |
output += sep | |
const ns = nPerms[i] | |
const as = aPerms[j] | |
for (let k = 0; k < ns.length; k += 1) { | |
if (ns[k] === nouns[k]) continue outer | |
} | |
for (let k = 0; k < as.length; k += 1) { | |
if (as[k] === adjectives[k]) continue outer | |
} | |
let str = story | |
for (let k = 0; k < ns.length; k += 1) { | |
str = str.replaceAll("N" + k, ns[k]) | |
} | |
for (let k = 0; k < as.length; k += 1) { | |
str = str.replaceAll("A" + k, as[k]) | |
} | |
output += str | |
sep = "\n\n" | |
} | |
} | |
console.log(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment