473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Is len O(n) or O(1) ?

Python uses arrays for lists right?

def quicksort(lista):
if lista == []:
lista
else:
return quicksort([x for x in lista[1:] if x < lista[0]]) +[lista[0]] + \
quicksort([x for x in lista[1:] if x >= lista[0]])

or

def quicksort(lista):
if len(lista) == 0
lista
else:
return quicksort([x for x in lista[1:] if x < lista[0]]) +[lista[0]] + \
quicksort([x for x in lista[1:] if x >= lista[0]])

wait first one raises TypeError unsupported operand types.
Sep 11 '08 #1
5 1300
On Thu, 11 Sep 2008 02:23:43 -0700, process wrote:
Python uses arrays for lists right?
`len()` on `list` objects is O(1).
def quicksort(lista):
if lista == []:
lista
else:
return quicksort([x for x in lista[1:] if x < lista[0]]) +[lista[0]] + \
quicksort([x for x in lista[1:] if x >= lista[0]])

or

def quicksort(lista):
if len(lista) == 0
lista
else:
return quicksort([x for x in lista[1:] if x < lista[0]]) +[lista[0]] + \
quicksort([x for x in lista[1:] if x >= lista[0]])

wait first one raises TypeError unsupported operand types.
Both do because `None` + `list` doesn't make sense. You should actually
``return`` the `lista` in the ``if`` branch instead of just lookup the
object for that name and then do nothing with it.

Shorter alternative:

if not lista:
return []

Empty lists are considered `False` in a boolean test.

Ciao,
Marc 'BlackJack' Rintsch
Sep 11 '08 #2
process wrote:
Python uses arrays for lists right?
len is O(1). for other operations, see

http://effbot.org/zone/python-list.htm#performance
def quicksort(lista):
if lista == []:
lista
better make that:

def quicksort(lista):
if not lista:
return lista
...

</F>

Sep 11 '08 #3
ok but if len is O(1) then it doesnt matter? compared to

if not lista:
return []

i mean.
Sep 11 '08 #4
process wrote:
ok but if len is O(1) then it doesnt matter? compared to

if not lista:
return []
what doesn't matter?

</F>

Sep 11 '08 #5
>ok but if len is O(1) then it doesnt matter? compared to
if not lista:
return []
>i mean.
Depends on how often the test is performed, e.g.:
>>def f(a):
... if not a: return []
...
>>dis.dis(f)
2 0 LOAD_FAST 0 (a)
3 JUMP_IF_TRUE 5 (to 11)
6 POP_TOP
7 BUILD_LIST 0
10 RETURN_VALUE
> 11 POP_TOP
12 LOAD_CONST 0 (None)
15 RETURN_VALUE
>>def g(a):
... if not len(a): return []
...
>>dis.dis(g)
2 0 LOAD_GLOBAL 0 (len)
3 LOAD_FAST 0 (a)
6 CALL_FUNCTION 1
9 JUMP_IF_TRUE 5 (to 17)
12 POP_TOP
13 BUILD_LIST 0
16 RETURN_VALUE
> 17 POP_TOP
18 LOAD_CONST 0 (None)
21 RETURN_VALUE

Even though len(a) is O(1) it's going to have a much larger constant
coefficient because of the lookup and call to len().

Skip
Sep 11 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Woof-Woof | last post by:
I have a problem with windows in my VB projects. My project consists of an MDI form with many child (MDIChild = TRUE) and non-child (MDIChild = FALSE) forms contained within. The problem I...
83
by: user | last post by:
Hello, Here is the program #include stdio int main(void) { const int num = 100; int *ip;
27
by: Brett | last post by:
If I want to easily swap the database I'm using, what is the best method for developing that tier in my application? I'll have basically a 4 tier app: 1. presentation 2. business logic 3. data...
3
by: Binary | last post by:
VC++ .NET 2003: Access violation with /O2 compiler switch; no AV with /O Hi I'm in the process of narrowing down a problem, and I have reduced the code involved to the following If someone could...
3
by: Kevin Cunningham | last post by:
I have a repeater with some labels in it (code below). For whatever reason the text for the label is not persisted in viewstate on the postback. Is there a trick to get this to work? Is there...
5
by: George | last post by:
How do I compare the values of two objects when Option Strict is On? One of the objects is the Tag property from some control (declared as object but holding a value, e.g. an Integer or a String...
13
by: academic | last post by:
ToolStripMenuItems do not have a RadioCheck property. Is this feature not available with the new StripMenu or is it just that I haven't found out how? Hard to believe they dropped such a nice...
6
by: lovecreatesbea... | last post by:
Why doesn't line# 21 create an object of type `C' class? I think the default construction function invocation `C()' creates a temporary nameless object, and `o1' will be built with the copy...
3
by: Joe Goldthwaite | last post by:
Hi everyone, I'm a developer who's been using python for a couple of years. I wrote a fairly large application using it but I was learning the language at the same time so it most of the code...
23
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.