Created
June 9, 2020 15:49
-
-
Save rjlutz/c86689f79eb1ad9379ac57bbcb51a37b to your computer and use it in GitHub Desktop.
Palindrome and Flutter Stuff
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
void main() { | |
print('Should all be true:'); | |
print(Palindrome.check('radar')); | |
print(Palindrome.check('noon')); | |
print(Palindrome.check('')); | |
print(Palindrome.check('123321')); | |
print(Palindrome.check('12,32,1')); | |
print(Palindrome.check('1 2 3 2 1')); | |
print(Palindrome.check('1 2 3 2 1')); | |
print(Palindrome.check('Rise to vote, sir!')); | |
print(Palindrome.check('aA')); | |
print(Palindrome.check('?aA')); | |
print(Palindrome.check(',aA')); | |
print(Palindrome.check(':aA')); | |
print(''); | |
print('Should all be false:'); | |
print(Palindrome.check('radarx')); | |
print(Palindrome.check('ABC')); | |
print(Palindrome.check('?ABC')); | |
print(Palindrome.check('...ABC')); | |
} | |
// lambda to mimic Java's Character.isLetterOrDigit() | |
bool isLetterOrDigit(String s, int idx) => s[idx].contains(RegExp(r'[\da-zA-Z]')); | |
class Palindrome { | |
static bool check(String s) { | |
// noon radar radarx | |
// 0123 01234 012345 | |
// modify the string, such that spces and punctuation are removed | |
int mid = s.length ~/ 2; | |
int start = 0; | |
int end = s.length - 1; | |
while (start < mid) { | |
while (!isLetterOrDigit(s, start)) start++; | |
while (!isLetterOrDigit(s,end))end--; | |
if (s[start].toLowerCase() != s[end].toLowerCase()) | |
return false; | |
start++; | |
end--; | |
} | |
return true; | |
} | |
} |
beingentangled
commented
Apr 16, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment