DEV Community

Day-4 Maximum Consecutive Ones

Mridu Bhatnagar on April 16, 2020

This problem is a part of LeetCode's Introduction to Data Structure Arrays - 101. This section covers the theoretical concepts related to Arrays fi...
Collapse
 
paddy3118 profile image
Paddy3118 • Edited

"find the length of the maximum runs/groups of ones in a list"

Groups means check groupby for me and I came up with the following

from itertools import groupby

def answer(lst):
    return len(max(tuple(g) for k, g in groupby(lst)
                            if k == 1))

print(answer([1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0]))

Which returns 3.

Collapse
 
cod3kid profile image
Muhamed Sufail

a = [0,1, 0,1, 1, 1, 0, 1, 1]
streak = 0
count = 0
for (i in a) {
if (a[i] == 1) {
count++;
streak = Math.max(streak, count)
}
else {
count = 0
}
}
console.log(streak)

This works too.. But its in JS

Collapse
 
paddy3118 profile image
Paddy3118

Python is a multi paradigm language. A class with only one method is a code smell, try using a simpler function.

Collapse
 
mridubhatnagar profile image
Mridu Bhatnagar

This is the default code skeleton on leetcode. You can have a look at it if you want.

Collapse
 
paddy3118 profile image
Paddy3118

Ah, then you just need to remember that it is a code smell, it just makes maintenence worse.