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

 

Deadly blasts hit Pakistani city (12 Mar 2010)

 

BA union announces strike dates (12 Mar 2010)

 

Siberian tigers die at China zoo (12 Mar 2010)

 

Rove 'proud' of US waterboarding (12 Mar 2010)

 

NY approves 9/11 dust payout (12 Mar 2010)

 

UN critical of Israel over Gaza (12 Mar 2010)

 

Lehman bosses severely criticised (12 Mar 2010)

 

Sarkozy to meet UK party leaders (12 Mar 2010)

 

Thalidomide effect mystery solved (11 Mar 2010)

 

How one group of Viking 'visitors' was dealt with by Anglo-Saxons (12 Mar 2010)

more »

 

Slashdot News for nerds, stuff that matters

 

MetaLab Accuses Mozilla of Ripping Off UI Elements In Mockups (2010-03-12T02:08:00+00:00)

 

William Shatner Takes On Social Networking (2010-03-11T23:22:00+00:00)

 

Researchers Beam 230Mb/sec Wireless Internet WIth LEDs (2010-03-11T22:38:00+00:00)

 

SolarPHP 1.0 Released (2010-03-11T22:18:00+00:00)

 

Best Smartphone Plan Covering US and Canada? (2010-03-11T21:54:00+00:00)

 

Pennsylvania CISO Fired Over Talk At RSA Conference (2010-03-11T21:07:00+00:00)

 

Half-Male, Half-Female Fowl Explain Birds' Sex Determination (2010-03-11T20:48:00+00:00)

 

T-Mobile's First HSPA+ Modem Goes On Sale Sunday (2010-03-11T20:24:00+00:00)

more »

 

TechPsychic Tech Rumors and Invented News

 

TechPsychic: Opera Mini Stats Tell Google Chrome Release Adds Support high-definition video. (12 Mar 2010)

 

TechPsychic: MySpace recently launched Android On Twitter 1 Million Downloads A job applicant are alive, Facebook? (11 Mar 2010)

 

TechPsychic: Android Apps. (11 Mar 2010)

 

TechPsychic: Why Google starts at ReadWriteWeb hosted With the other partners, Skype Hits 521 million funding from iPhone. (11 Mar 2010)

 

TechPsychic: He's Learned: Facebook Connect. (11 Mar 2010)

 

TechPsychic: Top paid for the FTSE 100, the future. (11 Mar 2010)

 

TechPsychic: Opera Software, Facebook During Apple's iPad Comes along, Windows Vista. (10 Mar 2010)

 

TechPsychic: JB: Why AT&T data plan? (10 Mar 2010)

more »

 

SF Gate

 

Pleasanton brothers ran meth lab, cops say (2010-03-11T16:52:49UTC)

 

Supes skeptical about Newsom's layoff plan (2010-03-11T18:26:24UTC)

 

Going got tough, Newsom got outta here (2010-03-11T08:21:35UTC)

 

Crime lab fallout: Drug defendants go free (2010-03-11T13:56:43UTC)

 

Fire in S.F. burns 2 buildings (2010-03-11T19:14:07UTC)

 

Whitman takes heat for avoiding reporters (2010-03-11T19:13:52UTC)

 

Highway deaths drop to lowest level since 1950s (2010-03-11T20:45:00UTC)

 

Lesbian teen sues Miss. school over prom flap (2010-03-11T23:09:25UTC)

 

Stocks climb for 3rd day as financial shares rise (2010-03-11T22:56:50UTC)

 

Slowly, Americans are regaining their lost wealth (2010-03-11T22:56:50UTC)

 

Rates are mixed after strong 30-year bond auction (2010-03-11T22:56:49UTC)

 

Citigroup CEO says bank on path to profitability (2010-03-11T22:55:48UTC)

 

Watson says FDA approves 6-month dose Trelstar (2010-03-11T22:51:48UTC)

 

Xoma 4Q profit falls as contract revenue tumbles (2010-03-11T22:49:43UTC)

more »

 

Asia Times Online

 

ALL ROADS LEAD TO KABUL : India seeks a new direction (Thu 11 Mar 2010 19:00:00 +0700)

 

Iran wants help from a friend (Thu 11 Mar 2010 19:00:00 +0700)

 

China-US ties strained like never before (Thu 11 Mar 2010 19:00:00 +0700)

 

US, Indonesia in a tentative embrace (Thu 11 Mar 2010 19:00:00 +0700)

 

India's cyber-defenses full of holes (Thu 11 Mar 2010 19:00:00 +0700)

 

DISPATCHES FROM AMERICA : Premature withdrawal in Iraq (Thu 11 Mar 2010 19:00:00 +0700)

 

Sri Lanka locks horns with UN (Thu 11 Mar 2010 19:00:00 +0700)

 

Reqo Diq mine at melting point (Thu 11 Mar 2010 19:00:00 +0700)

 

Locks turn in Nabucco door (Thu 11 Mar 2010 19:00:00 +0700)

 

China lassoes its neighbors (Thu 11 Mar 2010 19:00:00 +0700)

 

Job creation squeezed (Thu 11 Mar 2010 19:00:00 +0700)

 

THE MOGAMBO GURU : Debt doom ahead (Thu 11 Mar 2010 19:00:00 +0700)

more »

 


Site feed Updated: 2010-Mar-12 03:15