[Tutor] creating a range above & below a given number

Kent Johnson kent37 at tds.net
Tue Jun 9 03:29:15 CEST 2009


On Mon, Jun 8, 2009 at 12:56 PM, Gonzalo
Garcia-Perate<gonzillaaa at gmail.com> wrote:
> Kent, Emile thank you both. You're absolutely right, I was going for
> range because I thought it made the code more readable/more explicit.
> I hadn't taken into account the performance hit of creating the list
> and iterating through it. I'm not sure it was more readable either.
>
> the function now reads:
> def within_range_final(self, n, n2, threshold=5):
>    return n2-threshold <= n <= n2+threshold+1

You probably don't want the +1. What should be the result of
  self.within_range_final(7, 5, 1)
?

You might think a bit about testing your functions, since you have
posted several that don't do what you think they do. It's important to
test common cases, edge cases, and failure cases. For example you
might test all the combinations
1, 5, 1 - far outlier
3, 5, 1 - edge case
4, 5, 1 - edge case
5, 5, 1 - average case
6, 5, 1 - edge case
7, 5, 1 - edge case
10, 5, 1 - far outlier

Kent


More information about the Tutor mailing list