1️⃣회문인지 검사하는 함수
# 회문인지 검사
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 i in input_string:
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if i != " ":
new_string += i.lower()
reverse_string = i.lower() + reverse_string
# 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
2️⃣문자열 A, B, C 를 입력받고, A가 B로 끝나는 경우, 그 한 부분만 C로 바꿔서 출력
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence
if sentence.endswith(old):
# Using i as the slicing index, combine the part
# of the sentence up to the matched string at the
# end with the new string
i = len(sentence) - len(old)
new_sentence = sentence[:i]
return new_sentence + new
# Return the original sentence if there is no match
return sentence
print(replace_ending("It's raining cats and cats", "cats", "dogs"))
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts"))
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april"))
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April"))
# Should display "The weather is nice in April"