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"

2
  • 3
    diff is empty, giving you nothing to iterate over, meaning your returns are still not reached. Commented Aug 14, 2019 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]. Commented Aug 14, 2019 at 13:10

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Answer is still getting None
Sorry, there were some minor changes, could you check the code again?
Thanks, everyone! Btw question was Write a function expanding(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.
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

1 Comment

@Shallum : As this answer solved your problem. Please accept the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.