Is strictly ascending?
Pattern for checking if an array is strictly ascending
Problem statement
You are given an array of integers a. A new array b is generated by rearranging the elements of in the following way:
- b[0] is equal to a[0]
- b[1] is equal to the last element of a
- b[2] is equal to a[1]
- b[3] is equal to the second-last element of a
- b[4] is equal to a[2]
- b[5] is equal to the third-last element of a and so on. Your task is to determine whether the new array b is sorted in strictly ascending order or not.
Here is how the process of generating the new array b works:
Examples
Example1:
For a = [1, 3, 5, 6, 4, 2], the output should be solution(a) = true
The new array b will look like [1, 2, 3, 4, 5, 6], which is in strictly ascending order, so the answer is true
Example2:
For a = [1, 4, 5, 6, 3] the output should be solution(a) = false
The new array b will look like [1, 3, 4, 6, 5] which is not in strictly ascending order, so the answer is false .
Solution
def solution(a):
def strictly_ascending(array):
for i in range(len(array)-1):
if array[i] >= array[i+1]:
return False
return True
i = 0
j = len(a)-1
array = list()
mid = len(a)//2
while i < mid:
array.extend([a[i],a[j]])
i+=1
j-=1
if len(a)%2 != 0:
array.append(a[mid])
return strictly_ascending(array)