1. • Presentation is available online at
– http://www.slideshare.net/jasonjnoble/tdd_rspec_palindrome
2. Test Driven Design
to learn Recursion and Palindromes
Jason Noble
instructor@jasonnoble.org
3. Test Driven Design
• TDD is a software development practice that
relies on the repetition of a very short
development cycle
– Write a failing test that defines a desired
improvement or new function
– Write the minimum code possible to make the
test pass
– Refactor the code to acceptable standards
4. Red Green Refactor
• Write a failing test (Red)
• Make the test pass (Green)
• Refactor tests or code (NOT both!)
5. Why use TDD?
• Helps you break problems down into small
manageable tasks
• Writing tests takes the fear out of
programming
• Writing tests helps you communicate what
your code SHOULD do/accomplish
6. Let's Talk about Recursion
• "In order to understand recursion, you must
first understand recursion." – Anonymous
• Let's ask Google
8. What is recursion?
• Recursion is simply:
– A simple base case (or base cases)
– A set of rules that reduce all other cases towards a
known base case
• Example:
– Your parents are your ancestors (base case)
– The ancestors of your ancestors are also your
ancestors (recursion step)
9. Another example
• Factorial
– 1! equals 1 (Base case)
– For any number n greater than 1, the factorial of
that number is simply n * n – 1
– Factorial of 2 is 2 * 1! = 2 * 1 (base case) = 2
– Factorial of 3 is 3 * 1! = 3 * 2 = 6
– Factorial of 4 is 4 * 3! = 4 * 6 = 24
– Factorial of 5 is 5 * 4! = 5 * 24 = 120
10. Palindrome
• A palindrome is defined as "A
word, phrase, number or other sequence of
symbols or elements, whose meaning may be
interpreted the same way in either forward or
reverse direction".
• dad == dad.reverse
• 10022001
• radar
11. Let's code it!
• git clone
git@github.com:jasonnoble/tdd_rspec_palind
rome.git
• What's in this repo?
12. Palindrome recursive definition
• A given string is a palindrome if the string read
forward is the same as the string read
backwards
• Is an empty string a palindrome?
• Is a single character a palindrome?