2010 1.09

From XPUB & Lens-Based wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.


Permutations

bla bla

#!/usr/bin/python

import sys, codecs, string, random

sys.stdin = codecs.getreader("utf-8")(sys.stdin)
count = {}
for line in sys.stdin:
	for w in line.split():
		w = w.strip(string.punctuation).lower()
		if w:
			count[w] = count.get(w,0)+1

#for w,c in sorted(count.items()):
#	print w,c

words = count.keys()
#words.sort()
random.shuffle(words)
words = words[0:5]

import itertools

for ws in itertools.permutations(words):
	for w in ws:
		print w,
	print

Headline Permutation CGI!

#!/usr/bin/env python
#-*- coding:utf-8 -*-

print "Content-type: text/html"
print

import cgi
args = cgi.FieldStorage()
n = int(args.getvalue("n", "0"))

print """
<body>
"""

import feedparser
feed = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")

import itertools
words = feed.entries[n].title.split()
orderings = list(itertools.permutations(words))
for ws in orderings:
    print " ".join(ws)
    print "<br />"
    
#for e in feed.entries[:options.num]:
#    print "<p>"
#    print e.title.encode("utf-8")
#    print "</p>"
    
print """
</body>
"""

123