# Khan/khan-exercises

Fetching contributors…
Cannot retrieve contributors at this time
99 lines (95 sloc) 5.84 KB
 Permutations
randFromArray(["APE", "DEN", "CLAW", "FEAT", "IRON", "OPAL", "SONG", "RULES", "ANGER", "SLIME", "FANCY", "TIGER"]) shuffle(WORD).join("") WORD.length _.map(_.range(LENGTH), function(l){ return "_ "; }).join("") factorial(LENGTH)

How many unique ways are there to arrange the letters in the word WORD?

Let's try building the re-arrangements (or permutations) letter by letter. The word is LENGTH letters long:

BLANKS

For the first blank, we have LENGTH choices of letters.

After we put in the first letter, let's say it's \text{PERM[0]}, we have LENGTH - 1 blanks left.

\text{PERM[0]} BLANKS.slice(2)

For the second blank, we only have LENGTH - 1 choices of letters left. So far, there are LENGTH \cdot LENGTH - 1 unique choices we can make.

We can continue in this fashion to put in a third letter, and so on. At each step, we have one fewer unique choice to make, until we get to the last letter, and there's only one we can put in.

So, the total number of unique re-arrangements must be _.map(_.range(LENGTH), function(i){ return LENGTH - i;}).join("\\cdot"). Another way of writing this is LENGTH!, or LENGTH factorial, which is ANSWER.

randRange(4, 6) ["Bloopin", "Gloopin", "Prancer", "Lancer", "Quentin", "Balthazar", "Ezekiel", "Jebediah", "Rudy"] shuffle(ALL_NAMES).slice(0, NUM_NAMES) randRange(3, NUM_NAMES - 1) factorial(NUM_NAMES) / factorial(NUM_NAMES - SLOTS)

You have NUM_NAMES reindeer, toSentence(NAMES), and you want to have SLOTS fly your sleigh. You always have your reindeer fly in a single-file line.

How many different ways can you arrange your reindeer?