[Tutor] overlapping tuples
David L Neil
PyTutor at DancesWithMice.info
Wed Feb 26 21:01:35 EST 2020
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
More information about the Tutor
mailing list