Quick tip- Clearly communicate ranges in your functions' documentation using mathematical notations
2023/11/18
2023/11/18
The mathematicians have a very clear way of telling ranges.
wikipedia.org/wiki/Interval_(mathematics)
The notation is simple -
(a,b)
means all x where a<x<b
. {x∈R:a<x<b}
[a,b]
means all x where a<=x<=b
. {x∈R:a≤x≤b}
You can also mix and match ranges.
Examples
[0,3]
{x∈R:0<=x<=3}
{0,1,2,3}
[0,3)
{x∈R:0<=x<3}
{0,1,2}
(0,3]
{x∈R:0<x<=3}
{1,2,3}
(0,3)
{x∈R:0<x<3}
{1,2}
The python range function - range(start, stop, step)
[start, stop)
start <= val < stop
List slicing - list[start:stop:step]
[start,stop)
start <= val < stop
Turns out, most standard libraries of most languages follow the [start,stop)
convention.