There is a simple task, here is the literal condition:
Anna wrote a beautiful string generator. She thinks the line is beautiful if it reads equally from left to right and from right to left. For example, rrr and anna are pretty strings, but abc and adba are not.
But she made a mistake in the code and the generator does not output pretty lines, and the lines that can be made beautiful if you remove from each exactly one character. At least she thinks so.
She asks you to help determine if her assumption is correct.
Input data format
The first line contains a string s consisting of small Latin letters (4≤ | s | ≤10 ^ 3, where | s | is the string length).
Print YES if you can remove one character from the string so that it became beautiful; otherwise -NO.
Sample Input 1: abca Sample Output 1: YES
Sample Input 2: abcd Sample Output 2: NO
My solution (also commonplace):
def f (): s= '' .join (input (). split ()) n= len (s) v= 0 for i in range (int (n /2)): if s [i]!= s [n-1-i]: v += 1 if v>1: return ('NO') if v== 0: return ('NO') if v <2: return ('YES') print (f ())
BUT, to my surprise, code fails the tests . The catch is that I don't know the input for all tests. And looking at the problem and the code, I can't understand the reason. Am I missing something? ..
Answer # 1
def check (beautiful_string): middle= len (beautiful_string) /2 is_odd= False if (len (beautiful_string)% 2): is_odd= True pop_count= 0 index= 0 reverse_index= -1 while (index <= middle): if (beautiful_string [index]!= beautiful_string [reverse_index]): if (pop_count>0): return 'NO' index += 1 if (beautiful_string [index]== beautiful_string [reverse_index]): pop_count += 1 middle -= 1 continue index -= 1 reverse_index -= 1 if (beautiful_string [index]== beautiful_string [reverse_index]): pop_count += 1 continue return 'NO' index += 1 reverse_index -= 1 if (pop_count== 1): return 'YES' if (pop_count== 0) and (is_odd): return 'YES' return 'NO'
Answer # 2
def foo (text): for i in range (len (text)): items= list (text) items.pop (i) if items== items [:: -1]: return 'YES' return 'NO' print (foo ("abca")) # YES print (foo ("abcd")) # NO print (foo ("abab")) # YES print (foo ('banana')) # YES
- python : How to make a program that will repeat the text as many times as the user asked?
- Logical error in Python code
- python : Binding handlers with different parameters for an array of controls
- python : Recursive quicksort doesn't work
- python : How to implement constant movement of the character while holding down the key?
- python : How does sort_values work for multiple columns in Pandas?
- Python venv virtual environment does not isolate anything
- python : Code completion in django templates in PyCharm Community
- python : How to upload a connected Tor proxy to Vps Selenium Firefox
- python : Question on loops and lists