[Tutor] Replacing a value in a list

Alan Gauld alan.gauld at yahoo.co.uk
Fri Aug 13 08:50:22 EDT 2021


On 13/08/2021 08:14, nzbz xx wrote:

> This is what i got so far. The problem is that i don't know what to do when
> there are consecutive -999 in the list e.g.  [1, 10, -999, -999, -999]

This is a specification/design issue not a coding problem.
You eiyther:
1) ask the client what they want you to do (and update the specification
as required)
2) ask the architect for a design decision
3) make an executive decision for yourself and *document it in the code*

Some possibilities are:
1) use  the average value from either side to populate all the spaces
eg [1,2,3,-999,-999,-999,-999,8,9] becomes:

[1, 2, 3, 5.5, 5.5, 5.5, 5.5, 8, 9]

2) create a fill pattern using the average value to fill the mid points
then take the average of the end point and average to fill the next
points and so on. (this yields a smoother curve).
eg [1,2,3,-999,-999,-999,-999,8,9] becomes:

[1, 2, 3, 4.25, 5.5, 5.5, 6.75, 8, 9]

But it's a lot more complex to code.

But there are many other interpolation techniques, including
looking at the slopes of the curve on ether side. These 2 options
are just the easiest. Ideally, your client will know exactly
what they want!

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