Skip to content

Instantly share code, notes, and snippets.

@brisher777
Last active March 2, 2016 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brisher777/bd750c16c0082d819f49 to your computer and use it in GitHub Desktop.
Save brisher777/bd750c16c0082d819f49 to your computer and use it in GitHub Desktop.
Exercise 1
def is_palindrome(test_string):
mod_phrase = ""
if type(test_string) == int or test_string == mod_phrase:
return False
else:
for char in test_string[:]:
if char not in " ":
mod_phrase += char.lower()
if mod_phrase[:] == mod_phrase[::-1]:
return True
else:
return False
def is_palindrome(test_string):
#Converts test_string into a string object and returns in lowercase
test_string = str(test_string).lower()
#Splits the string by spaces and stores each word in a list
words = test_string.split(" ")
#A for loop places all the words together in a single string with no spaces.
newString = ""
for word in words:
newString += word
#Uses if statements to check if palindrome or empty string
if newString == "":
return False
elif newString == newString[::-1]:
return True
#Returns False if the if statements are not True
return False
def is_palindrome(test_string):
test_string = str(test_string)
test_string = test_string.lower()
test_string = test_string.replace(' ', '')
if len(test_string) > 0:
if test_string == ''.join(reversed(test_string)):
return True
if test_string.isalpha() == False:
return False
else:
return False
else:
return False
def is_palindrome(test_string):
test_string = str(test_string)
test_string = test_string.lower()
test_string = test_string.replace(' ', '')
if (len(test_string) > 0 and test_string == ''.join(reversed(test_string))):
return True
else:
return False
def is_palindrome(test_string):
test_string = str(test_string)
test_string = test_string.lower()
test_string = test_string.replace(' ', '')
if len(test_string) > 0:
if test_string == ''.join(reversed(test_string)):
return True
if test_string.isalpha() == False:
return False
else:
return False
else:
return False
def is_palindrome(test_string):
in_letters = []
if type(test_string) != str or test_string == "":
return False
else:
for c in test_string:
if c.isalnum():
in_letters.append(c.lower()) # testing ignores whitespace and case
return in_letters[:] == in_letters[::-1]
def is_palindrome(test_string):
t = test_string #Change parameter name cause it's too long for laziness
_t = [] #Will be expanded form of t
t_ = [] #Will be _t backwards
if(not(type(t) is str)): #Check if String
return False
if(len(t) == 0): #Check if Empty
return False
t = t.upper() #Make it uppercase
for i in t: #Set list _t to an expaded form of the input
if(i != " "):
_t.append(i)
i = len(_t)-1
while(i>=0):# set t_ to _t backwards
t_.append(_t[i])
i-=1
if(t_ == _t):#Check if forwards and backwards form are the same
return True
else:
return False
def is_palindrome(test_string):
# checking if we have string here
if not isinstance(test_string, str):
return False
# calculating len and checking for not empty string
test_string_len = len(test_string)
if test_string_len == 0:
return False
# string of len 1 is a palindrome
if test_string_len == 1:
return True
# since we don't care about spaces and lower or upper case
# creating new string without spaces and in lowercase
lowcase_no_space = test_string.lower().replace(" ", "")
# checking if string len is odd or not
if test_string_len % 2:
split = test_string_len // 2 + 1
else:
split = test_string_len / 2
# from start to split and from end to -split -1 with step -1
# tacocat
# tacocat[:4] = taco
# tacocat[:-4-1:-1] = taco
if lowcase_no_space[:split] == lowcase_no_space[:-split - 1:-1]:
return True
# there is nothing left to check so return false, just in case
return False
def is_palindrome(test_string):
if isinstance(test_string, str):
test_string = test_string.lower() # Transform Uppercase in LowerCase
test_string = test_string.replace(" ", "")
length = len(test_string)
if length == 0:
return False # Empty string is not palindrome
else: half = int(length/2) #Take the floor of the half
for i in range(half):
if test_string[i] != test_string[length - (1+i)]: #Test each couple
return False
return True
else: return False #Not a string
def is_palindrome(s):
import re
if type(s) != str :
return False
elif len(s) == 0 :
return False
else:
s = s.lower()
s = re.sub('[\s+]', '', s)
rev = s[::-1]
if rev == s:
return True
else:
return False
def is_palindrome(test_string):
if type(test_string) is not str:
return False
elif len(test_string) == 0:
return False
else:
test_string = test_string.lower().replace(' ', '')
if test_string == test_string[::-1]:
return True
else:
return False
def is_palindrome(word):
if type(word) != str:
return False
elif word == "":
return False
else: # lowercase, get rid of spaces
lowered = word.lower()
forward_list = lowered.split()
forward_word = ""
for i in range(len(forward_list)):
forward_word += forward_list[i]
# reverse characters and put them in list
reverse_list = []
for i in range(len(forward_word)):
reverse_list.insert(0, forward_word[i])
# make reverse word
reverse_word = ""
for i in range(len(reverse_list)):
reverse_word += reverse_list[i]
# check if palindrome
if forward_word == reverse_word:
return True
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment