tungwaiyip.info

 

home

about me

links

my software

Media

Yucatán Photos

St Lucia Photos

Photo Album

Videos

Blog

< June 2005 >
SuMoTuWeThFrSa
    1 2 3 4
5 6 7 8 91011
12131415161718
19202122232425
2627282930  

past articles »

Click for San Francisco, California Forecast

San Francisco, USA

 

Python's half open index notation

Beginner programmers often wonder about Python's sequence indexing and slicing notation. Array index starts from 0. Slicing uses half open notations, where L[a:b] is a subsequence with index x where a <= x < b.

Why is the endpoint excluded? Isn't it more intuitive if array index starts from 1 and the endpoint is included, so that a 3 elements array is referenced as L[1:3] with items L[1], L[2], L[3]?

It turns out this notation is an elegant and deliberate design and it has some excellent properties.

We write programs to operate on arrays, to find their length, traverse the subsequences, split them or join them. The half open notation always show a simple pattern. But the inclusive notation often requires adding 1 or substracting 1 to the indexes in many operations. Thus it is more vulnerable to off-by-one-error. This article One True Way of array indexing discuss this at length. I have reproduce its example (with corrections) below:

Operation Half open Inclusive
length of a slice L[a:b] b-a (b-a+1)
first n characters of L L[:n] L[1:n]
last n characters of L L[-n:] L[len(L)-n+1:]
The identity slice L == L[0:len(L)] == L[:] L[1:len(L)]
The empty slice L[a:a] is empty for any a. perhaps L[a:a-1]?
A slice of length n, from point a L[a:a+n] L[a:a+n-1]
Split L[a:b] at index c L[a:b] == L[a:c]+L[c:b] L[a:c-1]+L[c:b]

Another important property is an empty sequence can be expressed by L[a:a], while there is no natural way to express an empty sequence with the inclusive notation. But do we really need to care about a special case? Absolutely! In fact failure to account for empty input is one of the most common error. Just like zero is a fundamental concept in mathematics, always think how you program can handle null input. An inferior approach is to represent empty sequence by None or null pointer. This creates a special case so that a variable need to be tested before dereferencing. Failure to do so contributes to unexpected exceptions. It is an elegant design that L[a:b] can also represent sequences with 0 length.

C++'s STL also choose this notation to represent a range. According to the literature this is crucial because "algorithms that operate on n things frequently require n+1 positions. Linear search, for example (find) must be able to return some value to indicate that the search was unsuccessful." I have seen so many people flunked link list or data structure exercises because they have trouble dealing with the end of a list. Often a good solution is shift the focus beyond the n concrete objects to the n+1 positions around them. I hope this help to make sense of the half open notation.

2005.06.16 [, ] - comments (0)

 

 

Comments (0)

past articles »

 

BBC News

 

Putin blames US for Georgia role

 

Obama launches historic campaign

 

US GDP rebounds with 3.3% growth

 

Indian PM says flood 'a calamity'

 

Strengthening Gustav hits Jamaica

 

Headless corpses found in Mexico

 

US jury acquits US ex-marine

 

'Lost towns' discovered in Amazon

Fri, 29 August, 2008, 05:05 GMT 06:05 UK

more »

 

Slashdot News for nerds, stuff that matters

 

SSD Won't Make Sense In Laptops For Two Years (2008-08-29T02:03:00+00:00)

 

Comcast To Cap Data Transfers At 250 GB In October (2008-08-29T00:10:00+00:00)

 

Hashing Email Addresses For Web Considered Harmful (2008-08-28T23:01:00+00:00)

 

Black Screens For Unauthorized Copies of Windows (2008-08-28T22:10:00+00:00)

 

Cost-Effective Server Room Air Conditioning? (2008-08-28T21:21:00+00:00)

 

Bell Labs Kills Fundamental Physics Research (2008-08-28T20:19:00+00:00)

 

Will W3C Accept DRM For Webfonts? (2008-08-28T19:25:00+00:00)

 

Lenovo Requires NDA For Windows License Refund (2008-08-28T18:36:00+00:00)

more »

 

SF Gate

 

As Gustav nears, Gulf Coast puts faith in planning (28 Aug 2008)

 

McCain makes decision on running mate (28 Aug 2008)

 

Putin: US orchestrated conflict in Georgia (28 Aug 2008)

 

Walkway collapses in San Diego, injuring 16 (28 Aug 2008)

 

Urinating on building leads to big trouble for parolee (28 Aug 2008)

 

One of wanted suspects arrested in Mountain View double-slaying (28 Aug 2008)

 

51 Haitians die in Hurricane Gustav (28 Aug 2008)

 

Clintons are powerful weapon in Obama arsenal (28 Aug 2008)

 

Boeing sends machinists final offer (28 Aug 2008)

 

Comcast to make monthly Internet use cap official (28 Aug 2008)

 

Iran, Nigeria to share peaceful nuclear technology (28 Aug 2008)

 

Post-convention 'bounce' averages 10 points (28 Aug 2008)

 

Plane crashes into home in Las Vegas (28 Aug 2008)

 

Walkway collapses in San Diego, injuring 16 (28 Aug 2008)

more »

 

Asia Times Online

 

Maliki picks a date with destiny (28 Aug 2008)

 

Tehran exploits US-Russian tensions (28 Aug 2008)

 

COMMENT : Punishing Russia could prove costly (28 Aug 2008)

 

The last act for Thailand's PAD (28 Aug 2008)

 

India's nuclear deal headed for fiasco (28 Aug 2008)

 

Afghan violence hits home in Japan (28 Aug 2008)

 

If it ain't broke, don't fix it (28 Aug 2008)

 

Secret pacts spoil Philippine peace (28 Aug 2008)

 

China's excess liquidity trap (28 Aug 2008)

 

Central banks need a Basel lll (28 Aug 2008)

 

And the prudent shall survive (28 Aug 2008)

 

THE MOGAMBO GURU : A-holes at the J-Hole (28 Aug 2008)

more »

 


Site feed Updated: 2008-Aug-28 21:15