ch06
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
# oop/static.methods.py
|
||||
class StringUtil:
|
||||
|
||||
@staticmethod
|
||||
def is_palindrome(s, case_insensitive=True):
|
||||
# we allow only letters and numbers
|
||||
s = ''.join(c for c in s if c.isalnum()) # Study this!
|
||||
# For case insensitive comparison, we lower-case s
|
||||
if case_insensitive:
|
||||
s = s.lower()
|
||||
for c in range(len(s) // 2):
|
||||
if s[c] != s[-c -1]:
|
||||
return False
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def get_unique_words(sentence):
|
||||
return set(sentence.split())
|
||||
|
||||
print(StringUtil.is_palindrome(
|
||||
'Radar', case_insensitive=False)) # False: Case Sensitive
|
||||
print(StringUtil.is_palindrome('A nut for a jar of tuna')) # True
|
||||
print(StringUtil.is_palindrome('Never Odd, Or Even!')) # True
|
||||
print(StringUtil.is_palindrome(
|
||||
'In Girum Imus Nocte Et Consumimur Igni') # Latin! Show-off!
|
||||
) # True
|
||||
|
||||
print(StringUtil.get_unique_words(
|
||||
'I love palindromes. I really really love them!'))
|
||||
# {'them!', 'palindromes.', 'I', 'really', 'love'}
|
||||
|
||||
|
||||
"""
|
||||
$ python static.methods.py
|
||||
False
|
||||
True
|
||||
True
|
||||
True
|
||||
{'them!', 'palindromes.', 'I', 'really', 'love'}
|
||||
"""
|
||||
Reference in New Issue
Block a user