0
Binary Digits
Please someone help me understand the logic behind this question. You are given a number A which contains only digits 0's and 1's. Your task is to make all digits same by just flipping one digit (i.e. 0 to 1 or 1 to 0 ) only. If it is possible to make all the digits same by just flipping one digit then print 'YES' else print 'NO'. Input Format: The first line contains a number made up of 0's and 1's. Output Format: Print 'YES' or 'NO' accordingly without quotes. Example: Input: 101 Output: YES Explanation: If you flip the middle digit from 0 to 1 then all the digits will become same. Hence output is YES.
6 ответов
+ 3
You can just input number as a string and then count '0's and '1's
Based on number of '0's and '1's you can find the answer
Sometimes you don't need to use digits as digits
+ 2
#python 2.7.15
s = str(input ())
ones, zeroes = 0, 0
for i in range(len(s)):
ones += s[i]=='1'
zeroes += s[i]=='0'
if abs(ones-zeroes) != 1:
print ("NO")
else:
print("YES")
+ 1
Actually, all is explained in the task
But anyway
You are given a string of 0s and 1s
The task is to find, if there's only one 0 in the string of 1s or only one 1 in the string of 0s
Maybe the answer "YES" can be if the whole string is 1s or 0s
But it is said "by flipping ONE digit"
Note that "flipping" in this case can be seen as: a = not a
0
Дмитрий Мазыло do I need to convert the input number to list? because we cannot edit elements of the string.
0
Дмитрий Мазыло I have written the code as instructed by you. it runs well but for some cases it does not. for example, it works for inputs like 10 and 1011. but it shows wrong output for 0011. can u please help me.
0
From where shall we learn binary digits