mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-23 08:51:31 -04:00
add some array playing
This commit is contained in:
parent
5732cfcc61
commit
5c55b81641
7 changed files with 263 additions and 0 deletions
39
arrays_and_strings/array-check_mountain.py
Normal file
39
arrays_and_strings/array-check_mountain.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
'''
|
||||
Given an array of integers arr, return true if and only if it is a valid mountain array.
|
||||
|
||||
Recall that arr is a mountain 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
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
arr = [0,3,2,1]
|
||||
print(valid_mountain_array(arr))
|
Loading…
Add table
Add a link
Reference in a new issue