-
-
Save AnisahTiaraPratiwi/7cb4451ceeed9607e19ffd8756051579 to your computer and use it in GitHub Desktop.
def is_palindrome(input_string): | |
# We'll create two strings, to compare them | |
new_string = "" | |
reverse_string = "" | |
# Traverse through each letter of the input string | |
for string in input_string.lower(): | |
# Add any non-blank letters to the | |
# end of one string, and to the front | |
# of the other string. | |
if string.replace(" ",""): | |
new_string = string + new_string | |
reverse_string = string + reverse_string | |
# Compare the strings | |
if new_string[::-1]==reverse_string: | |
return True | |
return False | |
print(is_palindrome("Never Odd or Even")) # Should be True | |
print(is_palindrome("abc")) # Should be False | |
print(is_palindrome("kayak")) # Should be True |
##I have tried like this
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for message in input_string.lower():
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if message.strip():
new_string = message + new_string
reverse_string = reverse_string + message
# Compare the strings
if new_string == reverse_string:
return True
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
@moizuddin78 the script worked.
Can you please explain the above code?
May be this code will be easier to understand
`def is_palindrome(input_string):
# We'll create two strings, to compare them
stadard_string = input_string.lower().replace(" ","")
if stadard_string==stadard_string[::-1]:
return True
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True`
I have tried it as follows:
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = "".join(input_string).lower().replace(" ", "")
reverse_string = "".join(input_string[::-1]).lower().replace(" ", "")
ok = True
# Traverse through each letter of the input string
for s in range(len(new_string)):
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if new_string[s] != reverse_string[s-len(reverse_string)]:
ok = False
break
# Compare the strings
if ok:
return True
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
def is_palindrome(input_string):
new_string= input_string.replace(" ", "").upper()
reverse_string= new_string[::-1]
if new_string==reverse_string:
return True
else:
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
I erased other codes on this quiz. My brain is not braining on that.
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for letter in input_string.lower():
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if input_string.strip():
new_string = letter.strip() + new_string
reverse_string = reverse_string + letter.strip()
# Compare the strings
if new_string == reverse_string :
return True
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
def palindrome(s):
s2='' # new string with only alphabet characters
for index, letter in enumerate(s): #enumerate built-in function allows us to loop through strings with pair val index and value
if letter.isalpha() == True: #isalpha method to check if letter is in the alphabet or not
s2 = s2 + s[index]
else: continue
if s2[::-1] == s2 : #reversing by using string slicing
return True
else:
return False
I tried this process:
def is_palindrome(input_string):
new_string = ""
reverse_string = ""
for letter in input_string.lower():
if letter != " ":
new_string = new_string + letter
reverse_string = letter + reverse_string
if reverse_string == new_string :
return True
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
This one worked for me
👇🏻
def is_palindrome(input_string):
# Two variables are initialized as string date types using empty
# quotes: "reverse_string" to hold the "input_string" in reverse
# order and "new_string" to hold the "input_string" minus the
# spaces between words, if any are found.
new_string = ""
reverse_string = ""
# Complete the for loop to iterate through each letter of the
# "input_string"
for letter in input_string:
# The if-statement checks if the "letter" is not a space.
if letter != " ":
# If True, add the "letter" to the end of "new_string" and
# to the front of "reverse_string". If False (if a space
# is detected), no action is needed. Exit the if-block.
new_string = new_string + letter.lower()
reverse_string = letter.lower() + reverse_string
# Complete the if-statement to compare the "new_string" to the
# "reverse_string". Remember that Python is case-sensitive when
# creating the string comparison code.
if new_string == reverse_string:
# If True, the "input_string" contains a palindrome.
return True
# Otherwise, return False.
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
If a valid email is known to have a mix of characters and numbers and then ends with @edibrige.ng, e.g nnebaba123@edubridge.ng.
Write a functon named is_valid_email that accepts an email as an input argument and then check if the email is valid based on the condition above
You can replace line 10 -- if string.replace(" ",""):
with -- if string.strip():
to remove any whitespaces