Program 14
Write a program that generates 100 random integers that are either 0 or 1. Then find the longest run of zeros, the largest number of zeros in a row. For instance, the longest run of zeros in [1,0,1,1,0,0,0,0,1,0,0] is 4.
import random
l=[]
for num in range(20):
l.append(random.randint(0,1))
print("ListElements are:",l)
c = 0
max_count = 0
for i in range(0,20):
if (l[i]== 0):
c += 1
if(i==len(l)-1):
if(c>max_count):
max_count=c
else:
if c > max_count:
max_count = c
c = 0
print("Longest Run of Zeros in a row is:",max_count)
OUTPUT:
====RESTART: D:\old\r19 lab-python\14.py ================
ListElements are: [0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0]
Longest Run of Zeros in a row is: 2
>>>
=====RESTART: D:\old\r19 lab-python\14.py ============
ListElements are: [1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0]
Longest Run of Zeros in a row is: 2
>>>
==== RESTART: D:\old\r19 lab-python\14.py ==============
ListElements are: [0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0]
Longest Run of Zeros in a row is: 3
>>>
==== RESTART: D:\old\r19 lab-python\14.py ===========
ListElements are: [0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1]
Longest Run of Zeros in a row is: 3
>>>
====== RESTART: D:\old\r19 lab-python\14.py ==========
ListElements are: [1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0]
Longest Run of Zeros in a row is: 3
>>>
======= RESTART: D:\old\r19 lab-python\14.py ===========
ListElements are: [1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0]
Longest Run of Zeros in a row is: 5
No comments:
Post a Comment