Created
June 19, 2012 00:08
-
-
Save pzaich/2951576 to your computer and use it in GitHub Desktop.
Another Mark_twain recursive example
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
| class String | |
| def mark_twain | |
| self.mark_twain_helper([]) | |
| end | |
| def mark_twain_helper(drafts) | |
| words = self.split(' ') | |
| if words.length > 1 | |
| drafts << "Draft: #{words[0..words.length].join(" ")}." | |
| words[0..words.length-2].join(" ").mark_twain_helper(drafts) | |
| else | |
| drafts << "Final draft: #{words[0]}." | |
| drafts | |
| end | |
| end | |
| end | |
| "hey how are you.".mark_twain | |
| drafts = [] | |
| words = [hey, how, are, you.] | |
| drafts = ["Draft: hey how are you."] | |
| #----2nd Time----- | |
| "hey how are".mark_twain | |
| words = [hey, how, are] | |
| drafts = ["Draft: hey how are you.", "Draft: hey how are."] | |
| #----3rd Time---- | |
| "hey how".mark_twain | |
| words = [hey, how] | |
| drafts = ["Draft: hey how are you.", "Draft: hey how are.", "Draft: Hey how."] | |
| #----4th Time---- | |
| "hey".mark_twain | |
| words = [hey] | |
| drafts = ["Draft: hey how are you.", "Draft: hey how are.", "Draft: Hey how.", "Final: Hey."] | |
| => ["Draft: hey how are you.", "Draft: hey how are.", "Draft: Hey how.", "Final: Hey."] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment