#完全に回文でない時に回文を作成する関数 def palindrome(sentence): if sentence[-1] == sentence[-2]: sentence2 = sentence[::-1] sentence3 = sentence2[2:] new_sentence = sentence + sentence3 print(new_sentence) else: sentence2 = sentence[::-1] sentence3 = sentence2[1:] new_sentence = sentence + sentence3 print(new_sentence) #与えられた文字列がもうすでに完全に回文か調べる関数 def check_palindrome(sentence): for i in range(len(sentence)): if sentence[i] == sentence[-(i + 1)]: pass else: break else: print(sentence) #文字列が既に一部回文になりかかっているかどうか調べる関数 def check_palindrome2(sentence): for i in range(len(sentence) -2): if sentence[i] == sentence[i + 2] and sentence[i] == sentence[-(i + 1)]: break elif sentence[i] == sentence[i + 2]: if sentence[i - 1] == sentence[i + 3]: check_sentence = sentence[:i + 2] make_palindrome = check_sentence[::-1] make_palindrome2 = make_palindrome[1:] print(check_sentence + make_palindrome2) print("a") break else: pass elif sentence[i] == sentence[i+1]: if sentence[i-1] == sentence[i + 2] and i != 1: check_sentence = sentence[:i + 2] make_palindrome = check_sentence[::-1] make_palindrome2 = make_palindrome[2:] print(check_sentence + make_palindrome2) print("b") break else: pass else: palindrome(sentence) sentence = input("") if len(sentence) >= 2: check_palindrome2(sentence) check_palindrome(sentence) else: print(sentence)