• Presentation is available online at – http://www.slideshare.net/jasonjnoble/tdd_rspec _palindrome
• Presentation is available online at – http://www.slideshare.net/jasonjnoble/tdd_rspec_palindrome
Test Driven Designto learn Recursion and Palindromes
Jason [email protected]
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
Red Green Refactor
• Write a failing test (Red)
• Make the test pass (Green)
• Refactor tests or code (NOT both!)
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
Let's Talk about Recursion
• "In order to understand recursion, you must first understand recursion." – Anonymous
• Let's ask Google
What is recursion?
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)
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
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
Let's code it!
• git clone [email protected]:jasonnoble/tdd_rspec_palindrome.git
• What's in this repo?
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?