-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update computer-science/algorithms/palindromes.md
- Loading branch information
Jorge Cuadra
committed
Jan 17, 2016
1 parent
60cb37e
commit 3abd370
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,55 @@ | ||
# Palindromes | ||
|
||
> A *palindrome* is a word that it is spelled the same forward than backward. For example "*ROTOR*" | ||
## Pseudo-code | ||
|
||
1. If string has no letters or just one letter then it is a palindrome. | ||
2. Otherwise, compare first and last letters of the string. If they differ it is not a palindrome. | ||
3. Keep doing the same until you end up with one letter or 0 | ||
|
||
## Implementation | ||
|
||
```javascript | ||
// Returns the first character of the string str | ||
var firstCharacter = function(str) { | ||
return str.slice(0, 1); | ||
}; | ||
|
||
// Returns the last character of a string str | ||
var lastCharacter = function(str) { | ||
return str.slice(-1); | ||
}; | ||
|
||
// Returns the string that results from removing the first | ||
// and last characters from str | ||
var middleCharacters = function(str) { | ||
return str.slice(1, -1); | ||
}; | ||
|
||
var isPalindrome = function(str) { | ||
// base case #1 | ||
if (str.length <= 1) { | ||
return true; | ||
} | ||
// base case #2 | ||
if (firstCharacter(str) !== lastCharacter(str)) { | ||
return false; | ||
} | ||
// recursive case | ||
return (firstCharacter(str) === lastCharacter(str)) && isPalindrome(middleCharacters(str)); | ||
}; | ||
|
||
var checkPalindrome = function(str) { | ||
println("Is this word a palindrome? " + str); | ||
println(isPalindrome(str)); | ||
}; | ||
|
||
checkPalindrome("a"); | ||
Program.assertEqual(isPalindrome("a"), true); | ||
checkPalindrome("motor"); | ||
Program.assertEqual(isPalindrome("motor"), false); | ||
checkPalindrome("rotor"); | ||
Program.assertEqual(isPalindrome("rotor"), true); | ||
|
||
``` |