Re: grabbing random words

2006-09-23 Thread Frode
Jay wrote:
> How would I be able to grab random words from an internet source.  I'd
> like to grab a random word from a comprehensive internet dictionary.
> What would be the best source and the best way to go about this?

Here's a source that gives you a random word:
http://www.zokutou.co.uk/randomword/

Frode

-- 
http://mail.python.org/mailman/listinfo/python-list


Out of Office AutoReply: warning

2005-01-03 Thread Frode Gulbrandsen
Hei

Jeg har sluttet i Adirekta. Ta gjerne kontakt med en av mine kollegaer for 
hjelp. Jan Ole Nystuen: 22 03 69 60 / 917 70 366, [EMAIL PROTECTED] eller Lene 
Andersen: 22 03 69 56 / 481 17 787, [EMAIL PROTECTED]

Mvh

Frode
--
http://mail.python.org/mailman/listinfo/python-list


Fast generation of permutations

2006-01-25 Thread Frode Øijord

Hi all,
given a sequence of n elements i need to generate all possible 
permutations of length k <= n.


I found an elegant way to do this recursively:

def comb(items, n):
if n==0: yield []
else:
for i in xrange(len(items)):
for cc in comb(items[i+1:],n-1):
yield [items[i]]+cc

However, this is way too slow for my needs. I try to use this to 
generate all possible 5 card poker hands, but this takes around 17 
seconds on an Athlon 2200. That's a 2 orders of magnitude too slow for 
my needs.


I am familiar with writing Python extensions in C++, but I will not do 
this until I am confident that it is the only way to get the speed I need.


Any of you excellent sirs have any suggestions on how I can speed this up?

Please find attached an example script that executes and times the poker 
hand generation.



--
Frode, SIM

"Any fool can write code that a computer can understand.
 Good programmers write code that humans can understand"
import sys
from timeit import Timer

def comb(items, n):
if n==0: yield []
else:
for i in xrange(len(items)):
for cc in comb(items[i+1:],n-1):
yield [items[i]]+cc


def test():
cards = range(52)
for hand in comb(cards, 5):
"do something with the hand"

def main(argv):
t = Timer("test()", "from __main__ import test")
print t.timeit(1)


if __name__=="__main__":
sys.exit(main(sys.argv[1:]))
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Fast generation of permutations

2006-01-25 Thread Frode Øijord
Jack Diederich wrote:

> You might want to look at a specific purpose library for poker hands:
> http://pokersource.sourceforge.net/

Nah, evaluating the poker hands is the FUN part! I want to do that myself :)

> If you really want to do combinations a C extension has already 
> been written (by me).
> 
> http://probstat.sourceforge.net/
> 
> import probstat
> cards = range(52)
> for (hand) in probstat.Combination(card, 5):
>   pass
> 
> Takes 1.3 seconds on my laptop instead of 17 seconds for the pure
> python version which is only one order of magnitude faster.

This is *exactly* what i wanted! I just installed it and the hand 
generation is down to around 1.2 seconds now, and that I can live with 
:) Now I just have to reduce the running time of the actual hand 
evaluation with an order of magnitude... ;)

Thanks!

-- 
Frode, SIM

"Any fool can write code that a computer can understand.
  Good programmers write code that humans can understand"
-- 
http://mail.python.org/mailman/listinfo/python-list