mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-22 16:31:15 -04:00
Update and rename array-check_mountain.py to check_mountain.py
This commit is contained in:
parent
37a25d4423
commit
5c0e0228e6
1 changed files with 2 additions and 9 deletions
36
arrays_and_strings/check_mountain.py
Normal file
36
arrays_and_strings/check_mountain.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
'''
|
||||
Given an array of integers arr, return true if and only if it is a valid mountain array.
|
||||
An array is a moutain array if and only if:
|
||||
|
||||
arr.length >= 3
|
||||
There exists some i with 0 < i < arr.length - 1 such that:
|
||||
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
|
||||
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
|
||||
'''
|
||||
|
||||
def valid_mountain_array(arr: list[int]) -> bool:
|
||||
|
||||
last_number, mountain_up = arr[0], True
|
||||
|
||||
for i, n in enumerate(arr[1:]):
|
||||
|
||||
if n > last_number:
|
||||
if mountain_up == False:
|
||||
return False
|
||||
|
||||
elif n < last_number:
|
||||
if i == 0:
|
||||
return False
|
||||
mountain_up = False
|
||||
|
||||
else:
|
||||
return False
|
||||
|
||||
last_number = n
|
||||
|
||||
return not mountain_up
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue