1

My problem:

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.

I've already generated 100 random numbers between 0 and 1. However, I am not sure about my second function. I iterated my list and if any element was zero I returned the value. I also put the continue statement in order to break the loop if any value was diferent from zero.

import random

l=[]

def list():
    for j in range(0,100):
        x=random.randint(0,1)
        l.append(x)
    return list
print(list())

def largest_row_of_zeros(l):
    c=0
    for j in l:
        while j==0:
            c+=1
            continue
    return c
print(largest_row_of_zeros(l))

My compiler does not return any value at all.

This is what appears:

input
<function list at 0x7f6a170dcbf8>                                                                                                                                                     
^CTraceback (most recent call last):                                                                                                                                                  
  File "main.py", line 26, in <module>                                                                                                                                                
    print(largest_row_of_zeros(l))                                                                                                                                                    
  File "main.py", line 24, in largest_row_of_zeros                                                                                                                                    
    continue                                                                                                                                                                          
KeyboardInterrupt
3
  • 1
    Change your while to an if in the second function and then try it out. Commented May 3, 2019 at 13:33
  • you counts all zeros in list. You have to check if you have 1 and start couning new zeros. But you have to remeber previous c to compare with new c and get bigger value. Commented May 3, 2019 at 13:36
  • furas that is another important aspect Commented May 3, 2019 at 13:51

3 Answers 3

2

Your code is getting stuck is an infinite loop, and then when you press Ctrl+C to exit it, you get the KeyboardInterrupt error. This is because your while condition j==0 is an infinite loop, since the moment it sees a 0 in the loop, it gets stuck in the while True loop

Also you should not name your function as list which is bad practice, since list is a python reserved keyword.

I have fixed your code as follows

import random

#Function to get list of size 100 with 1s and 0s
def get_random_list():
    l = []
    #Append 0s and 1s randomly to list and return the list
    for j in range(0,100):
        x=random.randint(0,1)
        l.append(x)
    return l

#Function to get largest run of zeros
def largest_row_of_zeros(l):

    c=0
    max_c = 0
    #Keep track of max count seen so far
    for j in l:
        #If you encounter a 0, increment count
        if j == 0:
            c+=1
        #Else change max count and reset count
        else:
            if c > max_c:
                max_c = c
            c = 0
    return max_c

l = get_random_list()
print(largest_row_of_zeros(l))
Sign up to request clarification or add additional context in comments.

7 Comments

@ Devesh Kumar Singh why didnt you in part of incrementing the count when i==0 did not say..
if i ==0 c+= 1 max=c
Sorry I didn't get it! What increment I didn't do! Could you please explain it clearly ?
when you say if j==0 you increment the counter. But why dont write that and that max = counter because max is number of zeros the counter enconters up to that moment
So until j==0 is happening, we are counting continuos zeros, but when we encounter a non-zero, we save the number of zeros encounted so far is max_c, and reset the counter!
|
1

Change your while to an if in the second function as your while loop does not get executed at all.

import random

l=[]

def my_list():
    for j in range(0,100):
        x=random.randint(0,1)
        l.append(x)
    print (l)
    return l

def largest_row_of_zeros(l):

    c = 0
    zero_count = 0
    for j in l:
        if j==0:
            c+=1
        else:
            if c > zero_count:
                zero_count = c
            c = 0
    return zero_count

l = my_list()
print(largest_row_of_zeros(l))

OUTPUT:

[1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0]

3

ANOTHER OUTPUT:

[1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1]

4

The output will keep on generating random numbers since l is a list of randomly generated numbers so the number of 0's and 1's keep on changing.

In your program, you were counting the number of times zero occurred. But by amending the program now you will be able to find the longest number of zeros in a row.

Since list() is a python inbuilt function it is recommended to use variables that do not interfere with python inbuilt functions. So I changed list() to my_list(). The above program ensures that after coming across a zero it starts to increment the value of c by 1 but if it encounters a number larger than zero it resets the value of c to 0. All this while the function remembers the largest row of zeros every time the counter gets reset and prints out the highest value it recorded.

Hope this helps!

Comments

0

You're probably pressing Ctrl+C when your script executes - that sends a SIGTERM signal to close the program.

1 Comment

i already made a change. In the first function i had return list but what i wanted was l. Either way my program is returning c=227 and that is not correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.