0

Write a function accordian(l) that takes as input a list of integer l and returns True if the absolute difference between each adjacent pair of elements strictly increases.

def accordian(l):

    for i in range(len(l)):
     diff = []
     for i in range(len(diff)):

             diff = l[i] - l[i+1]
             if diff[i] < diff[i+1]:
                     return True
             else:
                     return False

print(accordian([1,3,7,2,9]))

Output: "None"

Shallum
  • 19
  • 3
  • 3
    `diff` is empty, giving you nothing to iterate over, meaning your returns are still not reached. – Sayse Aug 14 '19 at 13:09
  • You have to replace `for i in range(len(diff)):` with `for i in range(len(l)):` otherwise your function will never get to a `return`. Also I guess it should be `diff[i] = l[i] - l[i+1]` instead of `diff = l[i] - l[i+1]`. – Nils Aug 14 '19 at 13:10

2 Answers2

1

Your code doesn't work because the inner loop never runs, since the length of diff is 0, however a range of 0 doesn't proceed, your code will work if you add a value to diff, I can't give a code since I don't fully understand what you wanna do.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

You can try:

def accordian(data):
    diff = []
    for index, value in enumerate(data):
        if index == 0:
            continue
        diff.append(data[index] - data[index - 1])

    flag = True
    for index, single_element in enumerate(diff):
        if index == 0:
            continue
        if diff[index] <= diff[index - 1]:
            flag = False
            break
    return flag


print(accordian([1, 3, 7, 2, 9]))
print(accordian([1, 3, 7, 13, 21]))
print(accordian([1, 3, 5, 7, 9]))

Output:

False
True
False
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61