먼저 1과 0의 조합으로 사용자 입력 문자열을 가져옵니다. 그런 다음 1로 새 문자열을 만든 다음 연속 1이 있는 p 번호가 있는지 확인합니다. 존재하는 경우 FOUND를 표시하고 그렇지 않으면 NOTFOUND를 표시합니다.
예
Binary number ::1111001111 Enter consecutive 1’s :3 Consecutive 1's is Found
알고리즘
Step 1: input a string with the combination of 1’s, it’s stored in the variable X and 0’s and p is the consecutive 1’s in a binary number.
Step 2: form a new string of p 1’s.
newstring=”1”*p
Step 3: check if there is p 1’s at any position.
If newstring in X
Display “FOUND”
Else
Display “NOT FOUND”
End if
예시 코드
# To check if there is k consecutive 1's in a binary number
def binaryno_ones(n,p):
# form a new string of k 1's
newstr = "1"*p
# if there is k 1's at any position
if newstr in n:
print ("Consecutive 1's is Found")
else:
print (" Consecutive 1's is Not Found")
# driver code
n =input("Enter Binary number ::")
p = int(input("Enter consecutive 1's ::"))
binaryno_ones(n, p)
출력
Enter Binary number ::1111001111 Enter consecutive 1's ::3 Consecutive 1's is Found