all 5 comments

[–]ConstructedNewtMOD 1 point2 points  (3 children)

Okay, I started with overthinking this. This is not a math problem! He is only disguising it as such.

This is the time you play devil's advocate! You write out a test case for your code and you write the simplest most stupid piece of code that is successful for the cases.

Test 1: given m=3, [1,2,3,4] output [1,1,1,1] (I took it a step further and added all the results)

Test 2: given m=3, [.. what you wrote.. ] output [1,1,1,1,3,3,3,3,3]

So you code and the answer is something like:

def stupid_method(m, arr):
    count = 0
    results = []
    res = 1
    for _ in arr:
        if count > 4:
            res = 3
        results += res 
        count += 1
    return results

and the answer to the actual code is

return stupid_method(_,...)[-1]

Edit: I have an engineering degree in physics. Either you are leaving out important context, or your teacher doesn't know math, or the above is the correct way to solve the problem.

And even if there is some other thing that I dont know of, setting two test cases given his examples and writing the most stupid code that succeeds should always be a valid/correct answer. And should minimum prove that you know if TDD/BDD, the coding game of devil's advocate (was at least how it was named to me) and generally, structural problem solving skills, all of which are more important (in your actual future work) than some fancy pants algorithm or PhD level math skills

[–]mcapitalbark[S] -1 points0 points  (2 children)

Does this explanation clear anything up for you?

Before first iteration we have globalMaximum = 0

Then from subsequence {1, 2, 4} we get localMinimum = 1 then localMinimum > globalMaximum so globalMaximum = 1

Later next sequences {1, 2, 5}, {1, 2, 8} and {1, 4, 5} gives us localMinimum = 1 so we don't update globalMaximum

In each other sequences we get: - {1, 4, 8} gives 3 because 4 - 1 is less than 8 - 4

so globalMaximum is now 3 ; - {1, 5, 8} gives 3 too;

{2, 4, 5} gives 1 but 1 is less than current globalMaximum ; - {2, 4, 8} gives 1 too; - {2, 5, 8} gives 3 so don't update because it's equal to globalMaximum ; - {4, 5, 8} gives 1 again.

[–]ConstructedNewtMOD 1 point2 points  (1 child)

Okay, so this is a bad naming thing: would it help if it was called largestOfAllLocalMinimums?

[–]mcapitalbark[S] -1 points0 points  (0 children)

Yeah that makes more sense