#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Oct 17 09:39:49 2022 @author: cpresser """ # Take a moment to read your feedback in Moodle """ Write a function called isAlwaysIncreaing which takes a list as an argument. It should return True if the list has fewer than two items or if every value after the first is larger than the previous one. Otherwise it should return false. Make test lists for your function to test the various cases. """ def isAlwaysIncreasing(theList): #check the length condition first, because that is easy if len(theList) < 2: return True #loop through the indices. #compare each value with the one before it. for i in range(1, len(theList)): if theList[i] <= theList[i-1]: return False return True def isAlwaysIncreasing1(theList): if len(theList) < 2: return True #compare each value with the one after it. for i in range(len(theList)-1): if theList[i+1] <= theList[i]: return False return True # This version only returns once at the end def isAlwaysIncreasing2(theList): result = True #it is long enough to check. We could do this for the others too if len(theList) >= 2: for i in range(1, len(theList)): if theList[i] <= theList[i-1]: #once we set this, it should never change result = False return result # uses the values instead of the indices, not we have to skip # the first or the last def isAlwaysIncreasing3(theList): if len(theList) < 2: return True #rember the last one we looked at, give it a dummy value prev = 0 #remember if we are working on the first one or later first = True #go through each ITEM in the list. for n in theList: if first: # do nothing if this is the first one # but from now on it won't be first = False else: # not the first one, so compare to the previ if n <= prev: return False #update the previous prev = n return True list1 = [1,3,6,8,12,15,17,18] list2 = [1,3,6,8,12,11,17,18] list3 = [4] inc1 = isAlwaysIncreasing3(list1) print(f"Is the list {list1} increasing? {inc1}") print(f"Is the list {list2} increasing? {isAlwaysIncreasing3(list2)}") print(f"Is the list {list3} increasing? {isAlwaysIncreasing3(list3)}")