Thursday, March 12, 2015

Anagram creator / Recursion

Earlier, our teacher chalenged us to write a program that outputs a list of anagrams given an input string. A bit later, he said "On second thought, it might be too hard, considering we haven't learned the skills needed yet."

WELL, I DID IT ANYWAYS.


This program uses some serious recursion to return a list of anagrams. For example, "yes" prints out "yes", "yse", "eys", "esy", "sye", and "sey". If the boolean useAllCharacters is set to false, it will print out the string at each step, meaning it will also print anagrams using only some of the characters. For example, "hi" will print "h", "hi", "i", and "ih".

The recursion function works like this- given a string and a list of characters, it will go through the list and each time, call itself with the original string plus a character from the list and the list of characters without the character added to the string. If the list is empty however, it will simply print the completed string, and end without calling itself. More visually demonstrated-


1 comment:

  1. Nice Job Che! Clearly you are a talented programmer. I would now focus on being able to explain my code to other people...if I were you.

    ReplyDelete