Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

python - Count consecutive 1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Code Review and fix

First let's review your code:

for i in seq

The for loop will get you each element from the sequence. So this will be values of 0s and 1s.

Inside the loop, you are checking

if seq[i+1] == 1:

Since the values of i can be only 0 or 1, you are checking through the loop for values of seq[1] or seq[2] depending on the value of i in the list seq. Instead, you should iterate through the list for the length of seq - 1 times (since python list index starts from 0).

To do that, you can set the counter to 0 and for every iteration inside the for loop, increment the counter by 1 and use that to check seq. Python makes it easy by having enumerate(seq). This will give you a tuple (counter, value). That way you have direct access to the counter and can use that to check the counter + 1 value as well. Or you can iterate through the loop using range (0 to len of seq - 1).

Your modified code will be as follows:

def count11(seq):
    x = 0
    for i in range (len(seq)-1):
        if seq[i] == seq[i+1] == 1: #check if ith and i+1th position are both equal to 1
            x += 1
    return x

For a value of [0, 0, 1, 1, 1, 0], the result will be 2

Alternate solutions:

There are three types of consecutive checks we can do. I am going to give you code for all 3 types.

Type 1: If the value is [1, 1, 1, 0, 1, 1], there are two sequence of 1s in this list. So the result can be 2.

Type 2: An alternate way to look at this is to find any two elements and if they are both 1s, then count it as sequence. If that's the case, the result will be 3 (1, 1 for first and second, then 1, 1 for second and third, then 1, 1 for last two elements).

Type 3: Another check you can do is to find the highest sequence of 1s in a list. In this case, the highest will be 3.

In the below code,

consec_a function will result in 3 (check if the next element is also a 1). This solution is similar to what you have. I have made it a list comprehension and a single line.

consec_b function will result in 2 (check if there is a series of uninterrupted 1s)

consec_c function will check for 3 (find the max number of uninterrupted 1s)

def consec_a(sequ):
    return sum(sequ[i] == sequ[i+1] == 1 for i in range(len(sequ)-1))

def consec_b(sequ):
    seq_flag = False
    seq_count = 0
    for i in range (len(sequ)-1):
        if sequ[i] == sequ[i+1] == 1:
            if not seq_flag: seq_count += 1
            seq_flag = True
        else: seq_flag = False
    return seq_count

def consec_c(sequ):
    seq_flag = False
    seq_count = 1
    max_count = 0
    for i in range (len(sequ)-1):
        if sequ[i] == sequ[i+1] == 1:
            seq_count += 1
            if max_count < seq_count: max_count = seq_count
            seq_flag = True
        else:
            seq_flag = False
            seq_count = 1
    return max_count

print ('Results of consec_a function :')
print('[0, 0, 1, 1, 1, 0]', consec_a([0, 0, 1, 1, 1, 0]))

print('[0, 1, 1, 0, 1, 1]', consec_a([0, 1, 1, 0, 1, 1]))

print('[1, 1, 1, 0, 1, 1]', consec_a([1, 1, 1, 0, 1, 1]))

print('[1, 1, 1, 1, 1, 1]', consec_a([1, 1, 1, 1, 1, 1]))

print('[0, 0, 0, 0, 0, 0]', consec_a([0, 0, 0, 0, 0, 0]))

print ('
Results of consec_b function :')
print('[0, 0, 1, 1, 1, 0]', consec_b([0, 0, 1, 1, 1, 0]))

print('[0, 1, 1, 0, 1, 1]', consec_b([0, 1, 1, 0, 1, 1]))

print('[1, 1, 1, 0, 1, 1]', consec_b([1, 1, 1, 0, 1, 1]))

print('[1, 1, 1, 1, 1, 1]', consec_b([1, 1, 1, 1, 1, 1]))

print('[0, 0, 0, 0, 0, 0]', consec_b([0, 0, 0, 0, 0, 0]))

print ('
Results of consec_c function :')
print('[0, 0, 1, 1, 1, 0]', consec_c([0, 0, 1, 1, 1, 0]))

print('[0, 1, 1, 0, 1, 1]', consec_c([0, 1, 1, 0, 1, 1]))

print('[1, 1, 1, 0, 1, 1]', consec_c([1, 1, 1, 0, 1, 1]))

print('[1, 1, 1, 1, 1, 1]', consec_c([1, 1, 1, 1, 1, 1]))

print('[0, 0, 0, 0, 0, 0]', consec_c([0, 0, 0, 0, 0, 0]))

The output of this will be:

Results of consec_a function :
[0, 0, 1, 1, 1, 0] 2
[0, 1, 1, 0, 1, 1] 2
[1, 1, 1, 0, 1, 1] 3
[1, 1, 1, 1, 1, 1] 5
[0, 0, 0, 0, 0, 0] 0

Results of consec_b function :
[0, 0, 1, 1, 1, 0] 1
[0, 1, 1, 0, 1, 1] 2
[1, 1, 1, 0, 1, 1] 2
[1, 1, 1, 1, 1, 1] 1
[0, 0, 0, 0, 0, 0] 0

Results of consec_c function :
[0, 0, 1, 1, 1, 0] 3
[0, 1, 1, 0, 1, 1] 2
[1, 1, 1, 0, 1, 1] 3
[1, 1, 1, 1, 1, 1] 6
[0, 0, 0, 0, 0, 0] 0

Hopefully the code is easy to understand. If you need clarification, let me know.

I am using chaining to check for 1s.

if sequ[i] == sequ[i+1] == 1

This checks if all the values are equal to 1 and if yes, then the equation is set to True

sum(sequ[i] == sequ[i+1] == 1 .....)

This sums all the True values. As you know True = 1 and False = 0.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...