How to nest a list based on increasing sequences and ignore left overlapping ranges

2024/10/5 19:50:35

This is my input

mylist = [2, 7, 8, 11, 7, 9, 10, 15, 22, 30, 32]
  • from 2 to 11, it's increasing, so we need to grab the min max [2, 11]
  • from 7 to 10 it's increasing, but we need to ignore it because the range (7 to 10) is inluded in the first grabed list
  • from 15 to 32, it's increasing, so we need to grab the min max: [15, 32]

the final list should be : [[2, 11], [15, 32]]

I tried something like below but it does not :

final = []
mi = mylist[0]
ma = mylist[1]for i, j in zip(mylist, mylist[1:]):if i < j:ma = jelif i > j:mi = icontinueelif mi == ma:continuefinal.append([mi, ma])

update:

Let me add more scenarios :

  • for [5, 8, 10, 3, 4, 5, 7] we should get [[5,10]] because even if [3, 7] is overlapping with [5, 10], the start of [3, 7] is behind [5, 10]
  • for [5, 8, 10, 8, 9, 12] we should get [[5,12]] which is [5,10] ∪ [8, 12] because [8, 12] is overlapping with [5, 10] from the right (ahead of it)
  • for [1, 3, 5, 4, 3, 2, 1] we should get [[1, 5]] because from 4 to 1 it is a decreasing sequence, so we have to ignore it

I'm not necessarily looking for a Python code. I just need the algorithm or the correct way to approach this problem.

Answer

I don't think zipping the array makes sense. You could solve this problem by keeping track of min and max indices of the local sequences by iterating through the array

mylist = [2, 7, 8, 11, 7, 9, 10, 15, 22, 30, 32]final = []minIndex = -1 
maxIndex = -1 
maxValue = -1 * float('inf') for i in range(0, len(mylist)):if minIndex == -1 and mylist[i] > maxValue:maxValue = mylist[i]minIndex = imaxIndex = iif i > 0:if mylist[i - 1] >= mylist[i]:final.append([mylist[minIndex], mylist[maxIndex]])minIndex = -1maxIndex = -1else:maxIndex += 1maxValue = max(maxValue, mylist[i])if minIndex != -1:final.append([mylist[minIndex], mylist[min(maxIndex, len(mylist) - 1)]])print(final)
https://en.xdnf.cn/q/119814.html

Related Q&A

Float comparison (1.0 == 1.0) always false

Im using the following function in Python 2.7.3 and Kivy 1.8.0 to fade-in a Grid widget:def __init__(self, **kwargs):# ...Init parent class here...self.grid.opacity = 0.0Clock.schedule_interval(self.sh…

How to I extract objects? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 2…

sklearn.metrics.roc_curve only shows 5 fprs, tprs, thresholds [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 2 years ago.Improve…

I can not transform a file to a dictionary in python [duplicate]

This question already has answers here:ValueError: need more than 1 value to unpack python(4 answers)Closed 5 years ago.I am trying to transform a file to dictionary but having error.def txt_to_dict():…

Loan payment calculation

I am learning Python and am stuck. I am trying to find the loan payment amount. I currently have:def myMonthlyPayment(Principal, annual_r, n):years = nr = ( annual_r / 100 ) / 12MonthlyPayment = (Princ…

How can I implement this model?

Problem statement I have 3 classes (A, B, and C). I have 6 features: train_x = [[ 6.442 6.338 7.027 8.789 10.009 12.566][ 6.338 7.027 5.338 10.009 8.122 11.217][ 7.027 5.338 5.335 8.122 5.537…

How do I change a variable inside a variable?

Heres my code :hp1 = 100 health1 = you have, hp1hp1 = hp1 - 50 health1print hp1 print health1This is what it prints :50 (you have, 100)Why doesnt the hp1 change inside the health?

Why do I get NameError: name ... is not defined in python module?

filename:recom.py# Returns a distance-based similarity score for person1 and person2 def sim_distance(prefs,person1,person2): # Get the list of shared_itemssi={}for item in prefs[person1]:if item in pr…

How to form boxes from nearly touching lines

I want to detect corners from a image with boxes, although i created the chessboard edge lines with the EDlines algorithm. Now, I have some problems to join them to create perfect boxes. Could you help…

How to change app.py variable with HTML button?

How do I add or subtract 1 from the variable num ( in flask route) with an HTML button? So when I click the button it change the var to 1 and refresh the page to show the new value @app.route(/) def n…