Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 9th, 2006, 6:45 PM   #21
splinter9x
Hobbyist Programmer
 
splinter9x's Avatar
 
Join Date: Jun 2006
Posts: 137
Rep Power: 0 splinter9x is an unknown quantity at this point
It was a joke but I guess by the way people posted back about it, it didn't turn out that way. Calm down and don't be like 25% of the rest of the community here and jump on someones back becouse of a post that THE READER took the wrong way.
__________________
Visit my Blog
I support WINDOWS...
splinter9x is offline   Reply With Quote
Old Jun 9th, 2006, 6:50 PM   #22
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Thanks for the tips, Cerulean.

The final function I ended up writing executed almost instantly when given a 12-digit number, as opposed to the previous function, which took over a minute.

In case anyone's interested, this problem came from a USACO challege; now I have to re-write it in C++ or Java in order to submit it. (I wrote in Python to get experience with the language.) If anyone knows of a site with Python-specific challenges, I would be very interested...

Last edited by titaniumdecoy; Jun 9th, 2006 at 7:04 PM.
titaniumdecoy is offline   Reply With Quote
Old Jun 9th, 2006, 10:36 PM   #23
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

Just to warn you, both filter() and map() will go away with Python3. As Arevos pointed out, they can be easily replaced with list comprehensions:
# write your own using list comprehension:
def map(f, s):
    return [f(x) for x in s]

def filter(f, s):
    return [x for x in s if f(s)]
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jun 10th, 2006, 7:52 AM   #24
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
I find it quite silly how Python3 decided to add things like "any()" and "all()" when they are even easier to make yourself then the functions they are taking away ("filter()", "map()").
Sane is offline   Reply With Quote
Old Jun 10th, 2006, 10:04 AM   #25
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Quote:
Originally Posted by Sane
I find it quite silly how Python3 decided to add things like "any()" and "all()" when they are even easier to make yourself then the functions they are taking away ("filter()", "map()").
Depends what you are working on.

I think filter() and map() were introduced to Python before list comprehension came around. List comprehension is probably more efficient, particularly if you use the corresponding generator.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jun 12th, 2006, 12:46 PM   #26
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
Originally Posted by Dietrich
I think filter() and map() were introduced to Python before list comprehension came around. List comprehension is probably more efficient, particularly if you use the corresponding generator.
They were indeed. List comprehensions are more efficient if you're not calling a function in them. For example:
l = [104, 101, 108, 108, 111]
map(chr, l) # faster than [chr(x) for x in l] as the actual looping for the map function is done in C

[(x > 105) for x in l] # faster than map(lambda x: x > 105, l) because calling a function is fairly costly in Python.
We are talking very fractionally here, though.

Quote:
Originally Posted by Sane
I find it quite silly how Python3 decided to add things like "any()" and "all()" when they are even easier to make yourself then the functions they are taking away ("filter()", "map()").
Both are trivial one liners with list comps.
def map(func, l):
    return [func(x) for x in l]
def filter(func, l):
    return [x for x in l if func(x)]
reduce() is a fair bit harder to implement.
Cerulean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC