DEV Community

Discussion on: Day-9 Valid Mountain Array

Collapse
arsho profile image
Ahmedur Rahman Shovon • Edited on

This problem can be found in Leetcode: leetcode.com/problems/valid-mounta...

My approach with test case:

class Solution:
    def validMountainArray(self, arr: List[int]) -> bool:
        peak_found = False
        for i in range(1, len(arr)):
            if arr[i] == arr[i-1]:
                return False
            if arr[i] < arr[i-1]:
                if peak_found == False:
                    if i == 1:
                        return False
                    else:
                        peak_found = True
            else:
                if peak_found == True:
                    return False
        return peak_found

if __name__ == "__main__":

    solution = Solution()

    test_case = [0, 2, 3, 4, 5, 2, 1, 0]
    accepted_result = True
    result = solution.validMountainArray(test_case)
    assert result == accepted_result, (test_case, accepted_result, result)

    test_case = [0, 2, 3, 3, 5, 2, 1, 0]
    accepted_result = False
    result = solution.validMountainArray(test_case)
    assert result == accepted_result, (test_case, accepted_result, result)

    test_case = [0, 1, 2, 3, 2, 1]
    accepted_result = True
    result = solution.validMountainArray(test_case)
    assert result == accepted_result, (test_case, accepted_result, result)

    test_case = [0, 2, 3, 4, 5, 2, 1, 5, 4, 1]
    accepted_result = False
    result = solution.validMountainArray(test_case)
    assert result == accepted_result, (test_case, accepted_result, result)

    test_case = [0, 2, 3, 4, 5, 2, 1, 3, 2, 1]
    accepted_result = False
    result = solution.validMountainArray(test_case)
    assert result == accepted_result, (test_case, accepted_result, result)

    test_case = [0, 2, 1]
    accepted_result = True
    result = solution.validMountainArray(test_case)
    assert result == accepted_result, (test_case, accepted_result, result)

    test_case = [0, 2, 3]
    accepted_result = False
    result = solution.validMountainArray(test_case)
    assert result == accepted_result, (test_case, accepted_result, result)

    test_case = [5, 2, 1]
    accepted_result = False
    result = solution.validMountainArray(test_case)
    assert result == accepted_result, (test_case, accepted_result, result)

Enter fullscreen mode Exit fullscreen mode
Collapse
glucu profile image
Gabriel

Aren't the last two test cases wrong? The expected answer on leetcode returns false and not true. I am trying to solve this now!

Collapse
arsho profile image
Ahmedur Rahman Shovon

@gabriel , yes you are right. I did not notice about the leetcode problem. I have updated the old answer.

Thank you for finding the bug.

  • Shovon