Home > Software design >  For loop will not add to counter
For loop will not add to counter

Time:12-19

I'm trying to write a function that will take a list of 1's and 0's an as argument, and will return the number of consecutive 1's in the list. E.g. a list [1, 1, 1, 1] should return 4, and a list [1,0,1,1] should return 2.

I've written the following function:

Note: the argument "result" is a list of 1's and 0's.

def count_consecutive_ones(result):
  
    for i in range (0, len(result)):
        num_ones = int(0)
        print(str(num_ones)   "is the num of ones")
        if create_binary(i) == 1.0:
            num_ones  = num_ones
            print(num_ones)
    return(num_ones)

new_result = count_consecutive_ones(result)
print(new_result)

When I input [1.0, 1.0, 1.0, 1] as the result list, I receive:

0is the num of ones
0is the num of ones
[1]
0is the num of ones
[0]
[1.0, 0]
0is the num of ones
[1]
[1.0, 1]
0

In other words, it looks like num_ones does not increase as the for loop iterates.

Why not?

I'm new to Python / programming so thank you for your patience!

I've tried changing the inputs of "result", but this hasn't gotten me very far. I'd appreciate any advice!

CodePudding user response:

This is because at the top of the for loop, I believe you are resetting the value of num_ones to 0. Therefore, every time, you are receiving a value of 0 which is being printed out. Consider moving this assignment upward so it is not continually reassigned back to 0 at each iteration of the for loop. Or, create another variable as well.

CodePudding user response:

I created this sample hope it helps:

binaries = [1, 1, 0, 1]

def count_consecutive_ones(binaries):
    current_consecutive_ones = 0
    max_consecutive_ones = 0
    for i in range(len(binaries)):
        if binaries[i] == 1:
            current_consecutive_ones  = 1
        else:
            current_consecutive_ones = 0
        if current_consecutive_ones > max_consecutive_ones:
            max_consecutive_ones = current_consecutive_ones

    return (max_consecutive_ones)

result = count_consecutive_ones(binaries)
print(f"the longest series of consecutive ones was {result}") 

CodePudding user response:

So you actually need additional variables to solve this problem...

  1. variable to hold consecutive 1s
  2. variable to hold maximun onsecutive 1s

def count_consecutive_ones(result):

max_consecutive_ones = 0
consecutive_ones = 0

for i in range(0, len(result)):
    num_ones = int(result[i])
    if num_ones == 0:
        consecutive_ones = 0
    else:
        consecutive_ones  = 1
        max_consecutive_ones = max(max_consecutive_ones, consecutive_ones)

    print(str(consecutive_ones)   "is the num of ones")

return(max_consecutive_ones)

CodePudding user response:

The other answer has explained what you did wrong, here are some solutions to the problem:

First here is my way of doing it:

result = [1, 1, 1, 0, 1, 1, 1, 1, 1]
def count_consecutive_ones(result):
  result = [int(i) for i in result]
  consecutives = []
  consecutive = 0
  prev = 0
  for i in result:
    if i == 1 and prev == 1:
      consecutive  = 1
    elif i == 1:
      prev = 1
      consecutive  = 1
    else:
      consecutives.append(consecutive)
      consecutive = 0
      prev = 0
  consecutives.append(consecutive)
  return max(consecutives)
    

new_result = count_consecutive_ones(result)
print(new_result)

Output:

5

All this does is loop through result and every time there is a 1 add 1 to the consecutive count. When a 0 comes up the count is reset and added to a list of counts. The function returns the max of this list.

You could also use .split:

result = [1, 1, 1, 0, 1, 1, 1, 1, 1]
def count_consecutive_ones(result):
  result = [int(i) for i in result]
  result_str = ''.join([str(i) for i in result])
  result_str_split = result_str.split('0')
  return len(max(result_str_split))
new_result = count_consecutive_ones(result)
print(new_result)

Output:

5

This just uses the python str method .split() which returns a list object where each item is the characters that occur between each character that the str is split by.

Thus '11101'.split(str(0)) returns ['111', '1']

  • Related