Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions 02. Algorithms/08. Strings/Palindrome String/palindrome_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# The function to find if the string is a palindrome or not
def is_palindrome(s):
# Check if string is equal to its reverse.
s = s.lower()
return s == s[::-1]

# The driver code
if __name__ == '__main__':
s = str(raw_input("Ask user for something: "))
IS_PALINDROME = is_palindrome(s)

if IS_PALINDROME:
print("Yes, the input is a palindrome")
else:
print("No, the input in not a palindrome")
15 changes: 15 additions & 0 deletions 02. Algorithms/08. Strings/Reverse a String/reverse_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
The idea:
1. s = s.split(' ') -> splits the string with the space delim
2. [x for x in s.split(' ') if x != ""] -> filters leading, trailing or extra spaces (if any)
3. rev_s = list(reversed(s)) -> returns a reversed list
4. " ".join(rev_s) -> create a string again from the rev_s with the space delim
"""
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""

return " ".join(list(reversed([x for x in s.split(' ') if x != ""])))