[Tutor] Filling an array - with a twist

Alan Gauld alan.gauld at yahoo.co.uk
Wed Nov 11 06:51:27 EST 2020


On 11/11/2020 10:10, Phil wrote:

>> def validate(item, row, index, theList)
> 
> Thank you Alan for your suggestions. I'll continue to muddle on, 
> however, I was hoping that this problem might be common and have a 
> clever solution.

The problem is that it is not a general problem. There are too
many possible rules for what constitutes "valid". You could
write a general function to construct an MxN array that
takes a validator as an argument. But you still need to
write the validator function yourself.

It is possible that numpy has some such array builder function,
I don't ever use numpy so don't know.

> So far, I search each row for and error and correct it with a random 
> number. Of course this can create another error in the same row or a new 
> error in a column. Testing and correcting  could continue for a long time.

Searching and correcting is going to be much slower than checking each
item is valid before inserting. You have to iterate over the entire
array twice for a start: once to create it and once to check it. Much
better to test each item for validity before inserting and then, if not
valid, generate a new one.

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