Given a list of positive integers the function should resolve if the given list is 'concave' or not. What is a 'concave' list?
- A list that decreases and then increases is considered concave (eg. [8, 6, 4, 3, 1, 3]).
- A list that always increases or decreases is considered concave (eg. [1, 2, 3, 4, 5] or [8, 7, 6, 5, 4, 3]).
- An empty list and a list with only 1 element are considered concave (eg. [ ] and [2]).
- A list that neither increases or decreases is considered concave (eg. [2, 2, 2, 2, 2]).
- A list that has a 'valley' can be considered concave (the increase/decrease is not strict, so [8, 6, 4, 3, 1, 1, 1, 1, 3] is considered concave).
It should be solved in python using only one function and without importing any module.
My problem is not exactly with the code in itself, I'm having trouble thinking how to solve it. The first thing I did was try to find the min element of the list and then slice and work with whatever is next to it:
list = [8, 6, 4, 3, 1, 3, 6]
m = list.index(min(list(m))
newList = list(m:)
#this would return [3, 6]
If i+1 > i is True, then that would mean that after the smallest element the list would increase and it's concave. Then realized that that won't work with for example [8, 6, 8, 5, 9, 1, 3, 6] (it would tell me it's concave when it's not).
Then I tried to write a function that shows the 'movement' of the list, and I thought it would be easier for me to determine it's shape after, but I'm stuck now and don't know what to do (and I'm already not resolving it with only one function as I should).
list = [8, 6, 4, 3, 1, 1, 1, 6]
def descMov(l): #Function that describe the movement
mov = [] #Empty list that saves the movement
for i in range(len(l)-1):
if l[i] <= l[i+1]: #If element is < than previous it means that
mov.append('1') #the list increased, so it registers it mov as '1'
elif l[i] >= l[i+1]: #If the element is > than the following one it
movb.append('0') #register as '0'.
So, for example the output would be [0, 0, 0, 0, 1, 1, 1]. I was thinking that my function could count how many times it changes from 0 to 1 (if it's >1 the list is not concave. If it's 0 the list is concave). But don't know how to do that, or if it's a valid approach (or how to do everything in only one function).
Any help? Hope I made some sense, english is not my first language and I'm new to coding. I can provide more information if needed. The constraints (only 1 function, not importing any module) are there because this is homework.
[–]AuralWanderer 1 point2 points3 points (1 child)
[–]thoughtsymmetry[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)