[Tutor] overlapping tuples

Narasimharao Nelluri narasimha928 at gmail.com
Thu Feb 27 23:03:43 EST 2020


Hi David ,
Thanks for your feedback. Yes here i am solving Home work from one of my
assignments.
please check below i added your comments.

NNELLURI-M-L13P:google_class nnelluri$ cat overlap.py
#!/usr/bin/env python3


def tuple_overlap(old_list,overlap = None):

  old_list.sort()
  if overlap is None:
    overlap = []
  for fir,sec in zip(old_list,old_list[1:]):

    if fir[1] >= sec[0] and fir[1] <=sec[1]:=======>Here i am validating
2nd element in first variablae is in-between seconds variable if it is
there is a overlap
      overlap.append(fir)

    if sec[0] >= fir[0] and sec[1] <= fir[1]:=====> Here i am checking if
first element in second variable is in-between second variable , there is a
oberlap
      overlap.append(sec)

  overlap = sorted(overlap)
  return overlap


overlaps = tuple_overlap( [(1,10),(15,20),(101,110)] )
print( "Overlap 1 =", overlaps )
assert overlaps == []

overlaps = tuple_overlap( [(1,20),(15,20),(101,110)])
print( "Overlap 2 =", overlaps )
assert overlaps ==[(1, 20), (15, 20)]

overlaps = tuple_overlap( [(1,10),(15,20),(1,10),(1,10),(101,110)])
print( "Overlap 3 =", overlaps )
assert overlaps == [(1, 10), (1, 10),(1,10)]


NNELLURI-M-L13P:google_class nnelluri$


Please let know if you know correct solution.

Thanks
Narasimha


On Wed, Feb 26, 2020 at 6:02 PM David L Neil via Tutor <tutor at python.org>
wrote:

> On 27/02/20 5:44 AM, Narasimharao Nelluri wrote:
> > I got stuck at below problem for couple of days, can you guys help tp
> solve
> > this problem.
>
> This seems like a 'homework' assignment. Please declare it as such, so
> that we know to help you along your Python learning path...
>
>
> >    if overlap is None:
> >      overlap = []
>
> Good technique!
> - although question need for a second parameter at all?
>
>
> >    for fir,sec in zip(old_list,old_list[1:]):
> >
> >      if fir[1] >= sec[0] and fir[1] <=sec[1]:
> >        overlap.append(fir)
> >
> >      if sec[0] >= fir[0] and sec[1] <= fir[1]:
> >        overlap.append(sec)
>
> Please write your understanding of the above, more-or-less in English as
> if you were instructing another person (pseudo-code).
>
> Second question (after you've tackled the above): are you preferring to
> use different words in the explanation from those used for variable
> names in the code? Room for code-improvement there?
>
>
> >    print(overlap)
>
> Recommend instead of printing 'inside', returning the result of the
> function. Then your main-line can change from - to -:
>
>
> > list1 = [(1,10),(15,20),(101,110)]
> > tuple_overlap(list1)
>
> overlaps = tuple_overlap( [(1,10),(15,20),(101,110)] )
> print( "Overlap 1 =", overlaps )
>
>
> Plus, free bonus! Should you ever wish to use tuple_overlap() within
> some other function, it will be ready to deliver...
>
>
> In fact (free ultra-bonus!) you have expected results in-mind (which you
> worked-out 'manually' before-hand - another good practice!), so you
> could ask Python to check/test for you:
>
> assert overlaps == [(1, 20), (15, 20)]  # second test
>
> Will leave it to you to note what happens when the assert-ion ('this is
> what I believe to be true') is correct, and what when it proves wrong...
>
> (there are whole Python libraries to accomplish 'automated testing',
> which you could research later - but for now, simple is sweet!)
> --
> Regards =dn
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list