Question

In an array, a run occurs when 2 or more consecutive elements match. For example in the array 2, 3, 3, 3, 3, 5, 5 6 there is a run of threes, and a run of fives. The length of the run of threes is 4 (because there are 4 threes in a row). Write a method that returns the length of the longest run. If no run occurs, return 0. maxRun(12, 3, 3, 3, 3, 5, 5, 6]) 4 max Run([1, 1, 1 2, 2, 2]) 3 maxRun([4, 51) 0

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def maxRun(lst):
l = len(lst)
count = 1
maxCount = 0
for i in range(l-1):
if lst[i] == lst[i+1]:
count = count + 1
else:
if maxCount < count:
maxCount = count
count = 1
if maxCount == 1:
return maxCount-1
return maxCount

print(maxRun([2,3,3,3,3,5,5,6]))
print(maxRun([1,1,1,2,2,2]))
print(maxRun([4,5]))

Output:

sh-4.3$ python3 main.py                                                                                                                                                                                                                                                

4                                                                                                                                                                                                                                                                      

3                                                                                                                                                                                                                                                                      

0

xecute > Share Code main.py x - def maxRun(1st): 1 len(1st) count1 maxCount0 for i in range (1-1): if 1st[i]1st[i+1]: countco

Add a comment
Know the answer?
Add Answer to:
In an array, a "run" occurs when 2 or more consecutive elements match. For example in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • F a as a sequence of adjacent array elements such that each value in the run array was of size 10...

    f a as a sequence of adjacent array elements such that each value in the run array was of size 10 9. We define a "run" of elements o (except for the first) is one greater than the previous and looked like: value. For example, say the 3 2 16 9 7 8 9 2 a: position 0 3 We have three runs in this array: (1) between 9,10,1, 12) and, (3) between positions 7 and 8, (15, 16) positions...

  • Let S be a sequence of n distinct integers stored in an array as array elements...

    Let S be a sequence of n distinct integers stored in an array as array elements S[1], S[2], · · · , S[n]. Use the technique of dynamic programming to find the length of a longest ascending subsequence of entries in S. For example, if the entries of S are 11, 17, 5, 8, 6, 4, 7, 12, 3, then one longest ascending subsequence is 5, 6, 7, 12. Specifically: (a) define a proper function and find the recurrence for...

  • Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1...

    Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1 ] (with starting index start and length len) is called a flat if all elements of that subarray are equal. Furthermore, such a subarray is called a plateau if it is flat and each of the elements ints[start -1] and ints[start + len] that immediately proceed/succeed the subarray are either nonexistent (i.e., out of array’s index range) or are strictly smaller than the elements...

  • public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the...

    public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the elements of the two arrays. The two arrays do not have to be the same length. For example, given a = [1, 2, 3] and b = [4, 5, 6, 7, 8], the method returns c = [1, 4, 2, 5, 3, 6, 7, 8] Parameters: a - given array b - given array Returns: the array which interleaves the elements of the two...

  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

  • Part-1: find the longest block (subsequence of elements with same value) in an array. Part-2: find...

    Part-1: find the longest block (subsequence of elements with same value) in an array. Part-2: find all subsequences in an array of int that add up to a given sum. */ #include <iostream> #include <cstdlib> using namespace std; void printSeq(int* a, int s, int e); // -------------------------------------------- functions to be implemented by you void longestBlock(const int* a, int n, int& s, int& e) { // s = start index of the block // e = end index of the block...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • in C language Write a program to find the element of an array, which occurs maximum...

    in C language Write a program to find the element of an array, which occurs maximum number of times and its count. The program should accept the array elements as input from the user and print out the most occurring element along with its count. Hint: array size is 5. For example: Enter array elements (array size 5): 22321 Output: Max occurring element: 2. count: 3

  • Create in C# Write a method called MaximumDiffrence that accepts an integer array as a parameter...

    Create in C# Write a method called MaximumDiffrence that accepts an integer array as a parameter and return the maximum difference between adjacent values in the array, where the gap is defined as the absolute value of the difference between the 2 adjacent values. Example: if the array contains {5, 7, 4, 9, 6, 12, 8} so The first gap is (5,7)=-2 and the absolute value is 2 The second gap is (7,4)=3 Third gap is (4,9) = -5 its...

  • GetNumber and Getarray parts are working but I need help figuring out part 7_4 and 7_5...

    GetNumber and Getarray parts are working but I need help figuring out part 7_4 and 7_5 (I do not need Printarray). My professor provided the sections in which we need to fill in with code to make them run and pass the test files provides by the professor in Visual Studio. GetNumber Reuse the GetNumber0 method that you have been refining in the previous two labs Get Array Using your GetNumber0 method, create a method that will read in an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT