[Tutor] A program that can check if all elements of the list are mutually disjoint
Alan Gauld
alan.gauld at yahoo.co.uk
Sat Jun 5 14:06:07 EDT 2021
On 05/06/2021 14:09, Manprit Singh wrote:
> Dear sir,
> Consider a list :
> ls = ["amba", "Joy", "Preet"]
>
> The elements of the list are mutually disjoint, as no two elements of the
> list have anything in common.
Ok, That is a very specific definition. As such you should put
it in a predicate function ( ie. a test that returns a boolean
result) You can then compare two items for success/failure of
your test.
> I have written the code but it seems quite unreadable. First of all need
> to know if it is correct or not,
There are several meanings of correct in programming:
1) Does it perform the function it is supposed to perform?
2) Does it adhere to community/project coding standards
3) Is it code that could actually be used in production
(error handling, edge cases etc) Perhaps better
described as "finished".
Assuming you mean (1). How will you determine that?
You can approach it in two ways:
a) prove the code is correct using formal methods
b) prove the code is correct by comprehensive testing.
I'd recommend the second approach unless you have done
a course on formal code proving...
So can you define a set of tests and test data that will cover all the
possible variations(not all cases, that could be infinite!) but all
types of scenario. Include bad data, and edge cases as well as pass
and fail scenarios.
Put your code into a function and write the tests to check
the function. If all tests give the expected result you
have a high likelihood of it being correct.
> can be done in a better way or not.
When you move into solving very specific problems 'better' becomes a
relative trm. There are unlikely to be definitive answers. Best will
need to take account of the purpose of the code (library, one-off test,
part of a bigger suite etc) as well as non-functional constraints such
as run-time, space requirements, maintainability, logging/debugging
needs etc.
> ls = ["amba", "Joy", "Preet"]
> for idx, ele in enumerate(ls):
> if idx < len(ls)-1:> if not all(set(ele).isdisjoint(ch)for ch in ls[idx+1:]):
This is where a predicate helper function would clarify
things a lot:
for idx, word in enumerate(ls):
if isDisjoint(word, ls[idx+1:]
And the function looks something like(untested!)
def isDisjoint(aWord,aCollection)
for other in aCollection:
if not set(other).isdisjoint(set(aWord)):
return false
else: return True
That is completely untested but I think it should work and
seems more readable to me. Its probably not as fast, but
you didn't specify any speed constraints.
You may not need all of the explicit set() conversions.
Now you need to test it...
I notice Peter has now responded with ideas on how to do that.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list