-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
1 deletion.
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
68 changes: 68 additions & 0 deletions
68
Challenges2022.playground/Pages/Challenge#12.xcplaygroundpage/Contents.swift
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/*: | ||
| ||
[< Previous](@previous) [Home](Introduction) [Next >](@next) | ||
# Challenge 12: Is it a palindrome? | ||
Create a function that receives a string and returns a boolean, true if the given string is a palindrome, false otherwise. | ||
* Palindrome is a word or phrase that it's the same if read from right to left or in inverse order. | ||
* Discard spaces, punctuation signs and acentuation. | ||
Escribe una función que reciba un texto y retorne verdadero o falso (Boolean) según sean o no palíndromos. | ||
* Un Palíndromo es una palabra o expresión que es igual si se lee de izquierda a derecha que de derecha a izquierda. | ||
* NO se tienen en cuenta los espacios, signos de puntuación y tildes. | ||
*/ | ||
|
||
import Foundation | ||
|
||
func isPalindrome(_ str: String) -> Bool { | ||
// process given string | ||
var processedString = str | ||
.lowercased() | ||
.replacingOccurrences(of: " ", with: "") | ||
.replacingOccurrences(of: ".", with: "") | ||
.replacingOccurrences(of: ",", with: "") | ||
.replacingOccurrences(of: ";", with: "") | ||
.replacingOccurrences(of: "á", with: "a") | ||
.replacingOccurrences(of: "é", with: "e") | ||
.replacingOccurrences(of: "í", with: "i") | ||
.replacingOccurrences(of: "ó", with: "o") | ||
.replacingOccurrences(of: "ú", with: "u") | ||
|
||
return String(processedString) == String(processedString.reversed()) | ||
} | ||
|
||
func isPalindromeRegExp(_ str: String) -> Bool { | ||
// process given string | ||
var processedString = str | ||
.lowercased() | ||
.folding(options: .diacriticInsensitive, locale: .current) // Removes acentuation | ||
.replacing(/[^a-z0-9]/, with: "") | ||
|
||
return String(processedString) == String(processedString.reversed()) | ||
} | ||
|
||
print(""" | ||
Test 1: | ||
Ana lleva al oso la avellana. | ||
Result: \(isPalindrome("Ana lleva al oso la avellana.")) | ||
Result RegExp: \(isPalindromeRegExp("Ana lleva al oso la avellana.")) | ||
""") | ||
print(""" | ||
Test 2: | ||
Adivina ya te opina, ya ni miles origina, ya ni cetro me domina, ya ni monarcas, a repaso ni mulato carreta, acaso nicotina, ya ni cita vecino, anima cocina, pedazo gallina, cedazo terso nos retoza de canilla goza, de pánico camina, ónice vaticina, ya ni tocino saca, a terracota luminosa pera, sacra nómina y ánimo de mortecina, ya ni giros elimina, ya ni poeta, ya ni vida | ||
Result: \(isPalindrome("Adivina ya te opina, ya ni miles origina, ya ni cetro me domina, ya ni monarcas, a repaso ni mulato carreta, acaso nicotina, ya ni cita vecino, anima cocina, pedazo gallina, cedazo terso nos retoza de canilla goza, de pánico camina, ónice vaticina, ya ni tocino saca, a terracota luminosa pera, sacra nómina y ánimo de mortecina, ya ni giros elimina, ya ni poeta, ya ni vida")) | ||
Result RegExp: \(isPalindromeRegExp("Adivina ya te opina, ya ni miles origina, ya ni cetro me domina, ya ni monarcas, a repaso ni mulato carreta, acaso nicotina, ya ni cita vecino, anima cocina, pedazo gallina, cedazo terso nos retoza de canilla goza, de pánico camina, ónice vaticina, ya ni tocino saca, a terracota luminosa pera, sacra nómina y ánimo de mortecina, ya ni giros elimina, ya ni poeta, ya ni vida")) | ||
""") | ||
print(""" | ||
Test 3: | ||
¿Qué os ha parecido el reto? | ||
Result: \(isPalindrome("¿Qué os ha parecido el reto?")) | ||
Result RegExp: \(isPalindromeRegExp("¿Qué os ha parecido el reto?")) | ||
""") | ||
//: [Next](@next) |
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
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