0

Suppose I have an array: list1 = [8, 5, 3, 1, 1, 10, 15, 9] Now if the element is less than its previous element, increase it till the previous element with one.

Here:

5 < 8 so 5 should become: 5 + 3 + 1 = 9 i.e (8+1)

3 < 5 so 3 should become: 3 + 2 + 1 = 6 i.e (5+1)

1 < 3 so 1 should become: 1 + 2 + 1 = 4 i.e (3+1)

Now I am able to get the difference between elements if its less than its previous element. But, how to use it in a final list to get an output like this:

finallist = [8, 9, 6, 4, 1, 10, 15, 16]

Also how can I get a final list value of 'k' list in my code? Right now it shows:

[2] [2, 4] [2, 4, 3] [2, 4, 3, 3] [2, 4, 3, 3, 7]

Source code:

list1 = [8, 5, 3, 1, 1, 10, 15, 9]

k = []

def comput(x):
    if i[x] < i[x-1]:
        num = (i[x-1] - i[x]) + 1
        k.append(num)
        print(k)
    return


for i in [list1]:
    for j in range(len(list1)):
        comput(j)

2 Answers 2

1

You can use a list comprehension for this. Basically, the following code will check if one is larger than the next. If it is, then it will convert it to the previous+1.

list1 = [8, 5, 3, 1, 1, 10, 15, 9]

k = [list1[0]] + [i if j<=i else j+1 for i,j in zip(list1[1:],list1[:-1])]
cost = [j-i for i,j in zip(list1,k)]
print(k)
print(cost)

Output:

[8, 9, 6, 4, 1, 10, 15, 16]
[0, 4, 3, 3, 0, 0, 0, 7]
Sign up to request clarification or add additional context in comments.

4 Comments

This is the final list. I also want the cost of increment too. Like 5 got incremented by 4. 3 got incremented by 3. So, how to get that increment values? I got [2, 4, 3, 3, 7] which is correct. How to get it here? Or even better: Can we get a cost list like: [0, 4,3,3,0,0,0,7,] ?
Man I just got i :D
@Tarak I updated my answer. If I helped you, if you could mark as solution / upvote, it would help a lot.
Just curious! If I wanted to make increment to my finallist how would I do it? Here final list we got is [8, 9, 6, 4, 1, 10, 15, 16] so applying same logic, can we make final list as [8, 9, 10, 11,12, 13,15,16] ??
0

The following code will create a new list with the required output

l1 = [8, 5, 3, 1, 1, 10, 15, 9]
l = [l1[0]]
c=[0]         # cost / difference list

for i in range(len(l1)-1):
    if l1[i+1] < l1[i]:
        l.append(l1[i]+1)
        c.append(l1[i]+1-l1[i+1])
    else:
        l.append(l1[i+1])
        c.append(0)
print(l)

Output

[8, 9, 6, 4, 1, 10, 15, 16]
[0, 4, 3, 3, 0,  0,  0,  7]

5 Comments

This is the final list. I also want the cost of increment too. Like 5 got incremented by 4. 3 got incremented by 3.
So, how to get that increment values? I got [2, 4, 3, 3, 7] which is correct. How to get it here? Or even better: Can we get a cost list like: [0, 4,3,3,0,0,0,7,2] ?
@TarakShah updated! used a separate list c for differences.
Just curious! If I wanted to make increment to my finallist how would I do it? Here final list we got is [8, 9, 6, 4, 1, 10, 15, 16] so applying same logic, can we make final list as [8, 9, 10, 11,12, 13,15,16] ??
simply you can change l1 to [8, 9, 6, 4, 1, 10, 15, 16]. If you want to do this operation twice on the initial l1 then, define a function and run it twice.

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.