Cody

# Problem 15. Find the longest sequence of 1's in a binary sequence.

Solution 2995951

Submitted on 26 Sep 2020 by Chris Winnard
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
x = '0'; y_correct = 0; assert(isequal(lengthOnes(x),y_correct))

x = '0'; y_correct = 0; assert(isequal(lengthOnes(x),y_correct)) TotLength = 1 k = 0 check = 0 y = 0

2   Pass
x = '1'; y_correct = 1; assert(isequal(lengthOnes(x),y_correct))

x = '1'; y_correct = 1; assert(isequal(lengthOnes(x),y_correct)) TotLength = 1 k = 0 check = 1 CountLength = 0 j = 1 CountLength = 1 check = 0 k = 1 MatrixOfCounts = 1 y = 1

3   Pass
x = '01'; y_correct = 1; assert(isequal(lengthOnes(x),y_correct))

x = '01'; y_correct = 1; assert(isequal(lengthOnes(x),y_correct)) TotLength = 2 k = 0 check = 0 check = 1 CountLength = 0 j = 2 CountLength = 1 check = 0 k = 1 MatrixOfCounts = 1 0 y = 1

4   Pass
x = '10'; y_correct = 1; assert(isequal(lengthOnes(x),y_correct))

x = '10'; y_correct = 1; assert(isequal(lengthOnes(x),y_correct)) TotLength = 2 k = 0 check = 1 CountLength = 0 j = 1 CountLength = 1 i = 2 check = 0 k = 1 MatrixOfCounts = 1 0 check = 0 y = 1

5   Pass
x = '00'; y_correct = 0; assert(isequal(lengthOnes(x),y_correct))

x = '00'; y_correct = 0; assert(isequal(lengthOnes(x),y_correct)) TotLength = 2 k = 0 check = 0 check = 0 y = 0

6   Pass
x = '11'; y_correct = 2; assert(isequal(lengthOnes(x),y_correct))

x = '11'; y_correct = 2; assert(isequal(lengthOnes(x),y_correct)) TotLength = 2 k = 0 check = 1 CountLength = 0 j = 1 CountLength = 1 i = 2 check = 1 CountLength = 2 check = 0 k = 1 MatrixOfCounts = 2 0 check = 1 CountLength = 0 j = 2 CountLength = 1 check = 0 k = 2 MatrixOfCounts = 2 1 y = 2

7   Pass
x = '1111111111'; y_correct = 10; assert(isequal(lengthOnes(x),y_correct))

x = '1111111111'; y_correct = 10; assert(isequal(lengthOnes(x),y_correct)) TotLength = 10 k = 0 check = 1 CountLength = 0 j = 1 CountLength = 1 i = 2 check = 1 CountLength = 2 i = 3 check = 1 CountLength = 3 i = 4 check = 1 CountLength = 4 i = 5 check = 1 CountLength = 5 i = 6 check = 1 CountLength = 6 i = 7 check = 1 CountLength = 7 i = 8 check = 1 CountLength = 8 i = 9 check = 1 CountLength = 9 i = 10 check = 1 CountLength = 10 check = 0 k = 1 MatrixOfCounts = 10 0 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 2 CountLength = 1 i = 3 check = 1 CountLength = 2 i = 4 check = 1 CountLength = 3 i = 5 check = 1 CountLength = 4 i = 6 check = 1 CountLength = 5 i = 7 check = 1 CountLength = 6 i = 8 check = 1 CountLength = 7 i = 9 check = 1 CountLength = 8 i = 10 check = 1 CountLength = 9 check = 0 k = 2 MatrixOfCounts = 10 9 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 3 CountLength = 1 i = 4 check = 1 CountLength = 2 i = 5 check = 1 CountLength = 3 i = 6 check = 1 CountLength = 4 i = 7 check = 1 CountLength = 5 i = 8 check = 1 CountLength = 6 i = 9 check = 1 CountLength = 7 i = 10 check = 1 CountLength = 8 check = 0 k = 3 MatrixOfCounts = 10 9 8 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 4 CountLength = 1 i = 5 check = 1 CountLength = 2 i = 6 check = 1 CountLength = 3 i = 7 check = 1 CountLength = 4 i = 8 check = 1 CountLength = 5 i = 9 check = 1 CountLength = 6 i = 10 check = 1 CountLength = 7 check = 0 k = 4 MatrixOfCounts = 10 9 8 7 0 0 0 0 0 0 check = 1 CountLength = 0 j = 5 CountLength = 1 i = 6 check = 1 CountLength = 2 i = 7 check = 1 CountLength = 3 i = 8 check = 1 CountLength = 4 i = 9 check = 1 CountLength = 5 i = 10 check = 1 CountLength = 6 check = 0 k = 5 MatrixOfCounts = 10 9 8 7 6 0 0 0 0 0 check = 1 CountLength = 0 j = 6 CountLength = 1 i = 7 check = 1 CountLength = 2 i = 8 check = 1 CountLength = 3 i = 9 check = 1 CountLength = 4 i = 10 check = 1 CountLength = 5 check = 0 k = 6 MatrixOfCounts = 10 9 8 7 6 5 0 0 0 0 check = 1 CountLength = 0 j = 7 CountLength = 1 i = 8 check = 1 CountLength = 2 i = 9 check = 1 CountLength = 3 i = 10 check = 1 CountLength = 4 check = 0 k = 7 MatrixOfCounts = 10 9 8 7 6 5 4 0 0 0 check = 1 CountLength = 0 j = 8 CountLength = 1 i = 9 check = 1 CountLength = 2 i = 10 check = 1 CountLength = 3 check = 0 k = 8 MatrixOfCounts = 10 9 8 7 6 5 4 3 0 0 check = 1 CountLength = 0 j = 9 CountLength = 1 i = 10 check = 1 CountLength = 2 check = 0 k = 9 MatrixOfCounts = 10 9 8 7 6 5 4 3 2 0 check = 1 CountLength = 0 j = 10 CountLength = 1 check = 0 k = 10 MatrixOfCounts = 10 9 8 7 6 5 4 3 2 1 y = 10

8   Pass
x = '100101011111010011111'; y_correct = 5; assert(isequal(lengthOnes(x),y_correct))

x = '100101011111010011111'; y_correct = 5; assert(isequal(lengthOnes(x),y_correct)) TotLength = 21 k = 0 check = 1 CountLength = 0 j = 1 CountLength = 1 i = 2 check = 0 k = 1 MatrixOfCounts = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 0 check = 1 CountLength = 0 j = 4 CountLength = 1 i = 5 check = 0 k = 2 MatrixOfCounts = 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 6 CountLength = 1 i = 7 check = 0 k = 3 MatrixOfCounts = 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 8 CountLength = 1 i = 9 check = 1 CountLength = 2 i = 10 check = 1 CountLength = 3 i = 11 check = 1 CountLength = 4 i = 12 check = 1 CountLength = 5 i = 13 check = 0 k = 4 MatrixOfCounts = 1 1 1 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 9 CountLength = 1 i = 10 check = 1 CountLength = 2 i = 11 check = 1 CountLength = 3 i = 12 check = 1 CountLength = 4 i = 13 check = 0 k = 5 MatrixOfCounts = 1 1 1 5 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 10 CountLength = 1 i = 11 check = 1 CountLength = 2 i = 12 check = 1 CountLength = 3 i = 13 check = 0 k = 6 MatrixOfCounts = 1 1 1 5 4 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 11 CountLength = 1 i = 12 check = 1 CountLength = 2 i = 13 check = 0 k = 7 MatrixOfCounts = 1 1 1 5 4 3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 12 CountLength = 1 i = 13 check = 0 k = 8 MatrixOfCounts = 1 1 1 5 4 3 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 14 CountLength = 1 i = 15 check = 0 k = 9 MatrixOfCounts = 1 1 1 5 4 3 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 0 check = 1 CountLength = 0 j = 17 CountLength = 1 i = 18 check = 1 CountLength = 2 i = 19 check = 1 CountLength = 3 i = 20 check = 1 CountLength = 4 i = 21 check = 1 CountLength = 5 check = 0 k = 10 MatrixOfCounts = 1 1 1 5 4 3 2 1 1 5 0 0 0 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 18 CountLength = 1 i = 19 check = 1 CountLength = 2 i = 20 check = 1 CountLength = 3 i = 21 check = 1 CountLength = 4 check = 0 k = 11 MatrixOfCounts = 1 1 1 5 4 3 2 1 1 5 4 0 0 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 19 CountLength = 1 i = 20 check = 1 CountLength = 2 i = 21 check = 1 CountLength = 3 check = 0 k = 12 MatrixOfCounts = 1 1 1 5 4 3 2 1 1 5 4 3 0 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 20 CountLength = 1 i = 21 check = 1 CountLength = 2 check = 0 k = 13 MatrixOfCounts = 1 1 1 5 4 3 2 1 1 5 4 3 2 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 21 CountLength = 1 check = 0 k = 14 MatrixOfCounts = 1 1 1 5 4 3 2 1 1 5 4 3 2 1 0 0 0 0 0 0 0 y = 5

9   Pass
x = '01010101010101010101010101'; y_correct = 1; assert(isequal(lengthOnes(x),y_correct))

x = '01010101010101010101010101'; y_correct = 1; assert(isequal(lengthOnes(x),y_correct)) TotLength = 26 k = 0 check = 0 check = 1 CountLength = 0 j = 2 CountLength = 1 i = 3 check = 0 k = 1 MatrixOfCounts = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 4 CountLength = 1 i = 5 check = 0 k = 2 MatrixOfCounts = 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 6 CountLength = 1 i = 7 check = 0 k = 3 MatrixOfCounts = 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 8 CountLength = 1 i = 9 check = 0 k = 4 MatrixOfCounts = 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 10 CountLength = 1 i = 11 check = 0 k = 5 MatrixOfCounts = 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 12 CountLength = 1 i = 13 check = 0 k = 6 MatrixOfCounts = 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 14 CountLength = 1 i = 15 check = 0 k = 7 MatrixOfCounts = 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 16 CountLength = 1 i = 17 check = 0 k = 8 MatrixOfCounts = 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 18 CountLength = 1 i = 19 check = 0 k = 9 MatrixOfCounts = 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 20 CountLength = 1 i = 21 check = 0 k = 10 MatrixOfCounts = 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 22 CountLength = 1 i = 23 check = 0 k = 11 MatrixOfCounts = 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 24 CountLength = 1 i = 25 check = 0 k = 12 MatrixOfCounts = 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 26 CountLength = 1 check = 0 k = 13 MatrixOfCounts = 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 y = 1

10   Pass
x = '0101010111000101110001011100010100001110110100000000110001001000001110001000111010101001101100001111'; y_correct = 4; assert(isequal(lengthOnes(x),y_correct))

x = '0101010111000101110001011100010100001110110100000000110001001000001110001000111010101001101100001111'; y_correct = 4; assert(isequal(lengthOnes(x),y_correct)) TotLength = 100 k = 0 check = 0 check = 1 CountLength = 0 j = 2 CountLength = 1 i = 3 check = 0 k = 1 MatrixOfCounts = Columns 1 through 30 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 91 through 100 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 4 CountLength = 1 i = 5 check = 0 k = 2 MatrixOfCounts = Columns 1 through 30 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 91 through 100 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 6 CountLength = 1 i = 7 check = 0 k = 3 MatrixOfCounts = Columns 1 through 30 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 91 through 100 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 8 CountLength = 1 i = 9 check = 1 CountLength = 2 i = 10 check = 1 CountLength = 3 i = 11 check = 0 k = 4 MatrixOfCounts = Columns 1 through 30 1 1 1 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 91 through 100 0 0 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 9 CountLength = 1 i = 10 check = 1 CountLength = 2 i = 11 check = 0 k = 5 MatrixOfCounts = Columns 1 through 30 1 1 1 3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 91 through 100 0 0 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 10 CountLength = 1 i = 11 check = 0 k = 6 MatrixOfCounts = Columns 1 through 30 1 1 1 3 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 91 through 100 0 0 0 0 0 0 0 0 0 0 check = 0 check = 0 check = 0 check = 1 CountLength = 0 j = 14 CountLength = 1 i = 15 check = 0 k = 7 MatrixOfCounts = Columns 1 through 30 1 1 1 3 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 91 through 100 0 0 0 0 0 0 0 0 0 0 check = 0 check = 1 CountLength = 0 j = 16 CountLength = 1 i = 17 check = 1 CountLength = 2 i = 18 check = 1 CountLength = 3 i = 19 check = 0 k = 8 MatrixOfCounts = Columns 1 through 30 1 1 1 3 2 1 1 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 91 through 100 0 0 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 17 CountLength = 1 i = 18 check = 1 CountLength = 2 i = 19 check = 0 k = 9 MatrixOfCounts = Columns 1 through 30 1 1 1 3 2 1 1 3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 91 through 100 0 0 0 0 0 0 0 0 0 0 check = 1 CountLength = 0 j = 18 CountLength = 1 i = 19 check = 0 k = 10 MatrixOfCounts = Columns 1 through 30 1 1 1 3 2 1 1 3 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 91 through 100 0 0 0 0 0 0 0 0 0 0 check = 0 check = 0 check = 0 check = 1 CountLength = 0 j = 22 CountLength = 1 i = 23 check = 0 k = 11 MatrixOfCounts = Columns 1 through 30 1 1 1 3 2 1 1 3 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 31 through 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 61 through 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!