[Tutor] overlapping tuples

David L Neil PyTutor at DancesWithMice.info
Fri Feb 28 15:31:26 EST 2020


Hi,


On 28/02/20 5:03 PM, Narasimharao Nelluri wrote:
> Hi David ,
> Thanks for your feedback. Yes here i am solving Home work from one of my 
> assignments.

Are you attempting one of the Google certificates on-line - I saw one (I 
cannot recall its exact name, but something about) computer support 
using Python?


>      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)

When writing 'pseudo-code' try to avoid using IT-terminology, eg 
"variable"; because it 'puts on blinkers' - channels our brains to think 
(only) along certain lines! I appreciate that in this case, it is made 
more difficult, because the assignment has (probably) been phrased as an 
academic exercise.

Can you think of example data/circumstances for such data and algorithm 
in 'real life'? Now you can use 'real words', even if pseudo-code is 
more logical than 'correct English grammar'. (in fact, apart from the 
fact that I may not understand you 'here'; there's no rule that 
disallows the expression of "pseudo-code" in any other language - 
whichever you prefer. If it helps you to produce better code, then 
that's the 'correct' answer!)


At the risk of stating the obvious, when solving a logic problem, it 
pays to be methodical, ie be equally-logical in one's approach. Please 
note how the current approach first examines the *second* element in the 
first tuple, and *later* moves to the first. Wouldn't it be more logical 
to work 'from left to right' and 'first to second'?
(whereas English is written left-to-right and top-to-bottom, please 
forgive me if your preferred language is otherwise - 'your logic' won't 
align with my view)

Perhaps even consider both elements of the first tuple against the first 
element of the second tuple, and subsequently against the second element 
of the second tuple...


Lets design some simple tests:
1 no overlap, (second tuple is completely 'below the range')
2 no overlap, 'above'
3 (second tuple's) first element is 'inside' but second is not
4 ... first element is 'outside' but second is 'in'
5 ... both elements are 'inside' the first tuple's range.

First question: are there other possible situations that I didn't notice?

Second question (and it's very important): how is "overlap" defined?

In other words, which of these five tests should result in True 
(overlap), and which will be considered False (no overlap)?

NB I see two understandings of the word. (You seem to use one, whereas I 
(without seeing the exact question, but checking against the sample 
data) prefer the other)


Consideration/a question of design-approach: Without understanding 
*exactly* what is required, how can we write a correct program/answer?


Was the sample data provided as part of the question, or did you make it 
up? If it was provided, does it help us understand "overlap"? If you 
made it up, did you consider *all* the (reasonable) possible cases?


Regardless, it is a good idea to make your own set of test data to check 
your own code - let's call that "Unit Testing". Once all is working to 
our satisfaction, we then to use the provided-data as part of delivering 
the assignment-answer - we can call that "Acceptance Testing".

Let's follow-on from the logic-tests (developed above) and create some 
sample data:
first tuple: ( 4, 7 )
second tuples:
1 ( 1, 3 )
2 ( 8, 9 )
3 ( 5, 9 )
4 ( 1, 6 )
5 ( 5, 6 )


Depending upon the answers, above (and any realisations (learning!)), by 
this point, the current-code might already be consigned to the 
'bit-bucket of cyber-space'. However, *before* you are tempted to start 
re-coding, let's continue 'on paper', by working-through the 
newly-developed test scenarios ("unit tests") against the first 
if-statement:

if fir[1] >= sec[0] and fir[1] <=sec[1]

1 7 >= 1 and 7 <= 3
2 7 >= 8 and 7 <= 9
3 7 >= 5 and 7 <= 9
4 7 >= 6 and 7 <= 6
5 7 >= 5 and 7 <= 6

Which of these is True, and how does that meet the "specification"? (ie 
the assignment's requirements)

Once again, ignoring any fresh thoughts about 'the solution', let's 
repeat the exercise for the second if-statement:

if sec[0] >= fir[0] and sec[1] <= fir[1]:

1 1 >= 4 and 3 <= 7
2 8 >= 4 and 9 <= 7
3 5 >= 4 and 9 <= 7
4 1 >= 4 and 6 <= 7
5 5 >= 4 and 6 <= 7

Again, please consider: which of these is True?


Remember the earlier suggestion of using assert? Could you now build 
five (or more) assert statements, one for each unit test?

Ahah!. So, that's how we carry-through all the preparatory work 'on 
paper' into work-on-the-machine!


Now, before charging head-long towards the 'finish line', let's take 
another step, even further back: where did the problem "my current
solution doest solve all the cases" actually arise?

a) understanding the assignment (in IT we call that a "specification")
b) understanding the meaning/definition of words in the spec
c) understanding/possessing sufficient skills in Python
d) understanding of Boolean logic/algebra (True/False, and, or, not...)
e) developing test-data
f) somewhere else along the way


I expect that you are primarily focused on completing your assignment, 
so if you're confident you'll be wanting to 'get it done' and 'hand it 
in'. Let us know how you get on...

Nevertheless, to succeed in the course, may I recommend that you try 
answering these questions in a reply 'here', and list-members will 
likely respond with helpful advice which will help you, not only to 
learn Python, but to become a better professional programmer...
-- 
Regards =dn


More information about the Tutor mailing list