# Problem Description:
The task is to determine if an element at a given index in a list of integers is valid based on specific rules. The rules for validation depend on the position of the element in the list:
1. For the first element, it is valid if the second element is less than the first.
2. For the last element, it is valid if the last element is greater than the second last.
3. For any other element, it is valid if the average of its left and right neighboring elements is less than the element itself.

# Visual Facts:
1. The image provides a general rule for validating elements in an array.
2. The rule for the first element (A[0]) is that it is valid if A[1] < A[0].
3. The rule for the last element (A[n]) is that it is valid if A[n-1] < A[n].
4. For any other element A[i], it is valid if (A[i-1] + A[i+1]) / 2 < A[i].
5. Example 2 and Example 3 illustrate the application of these rules to specific arrays.

# Visual Patterns:
1. The validation rule for the first element checks if the next element is smaller.
2. The validation rule for the last element checks if the previous element is smaller.
3. For elements in between, the validation rule involves calculating the average of the neighboring elements and comparing it to the element itself.
4. The validation process involves checking specific conditions and marking elements as "VALID" or "INVALID" based on the rules.

# Solution
from typing import List

def solution(nums: List[int], idx: int) -> bool:
    """
    Determines if the element at the given index in the list is valid based on the specified pattern.
    Parameters:
        nums (List[int]): the list of numbers.
        idx (int): Zero-based index of the element to check.

    Returns:
        bool: True if the element is valid, False otherwise.
    """
    if idx != 0 and idx!= len(nums)-1:
        return True if (nums[idx-1] + nums[idx + 1]) /2 < nums[idx] else False
    else:
        return True if (nums[idx-1] if idx >0 else nums[1]) < nums[idx] else False



# Test Script
def test():
    test_cases = [
        ([3,7], 0, False),
        ([3,7], 1, True),
        ([3,1,4,2,2,7], 4, False),
        ([3,1,4,2,2,7], 5, True),
        ([3,1,4,2,2,7], 0, True),
        ([3,1,4,2,2,7], 2, True),
        ([3, 7, 2, 4, 6, 5, 3],1, True),
        ([3, 7, 2, 4, 6, 5, 3],2, False),
        ([3, 7, 2, 4, 6, 5, 3],0, False),
        ([3, 7, 2, 4, 6, 5, 3],5, True),
    ]

    for nums, idx, expected_output in test_cases:
        try:
            output = solution(nums, idx)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`nums`: {nums}\n"
                f"`idx`: {idx}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

        if output != expected_output:
            error_message = (
                f"The following Test Case failed:\n"
                f"`nums`: {nums}\n"
                f"`idx`: {idx}\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()