[Python] permutation

[code lang=”python”]
#!/usr/bin/python
def perm(a,b=[]):
if not len(a):
print ”.join(b)
for i in range(len(a)):
b.append(a.pop(i))
perm(a,b)
a.insert(i,b.pop())
perm(list("abcd"))
[/code]

abcd
abdc
acbd
acdb
adbc
adcb
bacd
badc
bcad
bcda
bdac
bdca
cabd
cadb
cbad
cbda
cdab
cdba
dabc
dacb
dbac
dbca
dcab
dcba

[Python]image crawler for deviant art

#!/usr/bin/python

import urllib, re, random
from os.path import isfile
#from time import sleep
#sleep(15)

keyword="white"
page=urllib.urlopen("http://browse.deviantart.com/?order=24&q="+keyword).read()

items=re.findall('super_img="(http://.*?\.(jpg|jpeg|png))"',page)
imgs=[]
for item in items:
	item=item[0]
	#print item
	imgs.append(item)

"""
img=random.choice(imgs)
print img
content=urllib.urlopen(img).read()
fileext=img[img.rfind("."):]
filename="/home/kjwon15/scripts/img/wallpaper" #+fileext
file(filename,"w").write(content)
"""

savedfile=0
for img in imgs:
	filename=img[img.rfind("/")+1:]
	if isfile("/home/kjwon15/pictures/"+filename):
		print "file exist: "+filename
	else:
		print "save file "+filename
		contents=urllib.urlopen(img).read()
		file("/home/kjwon15/pictures/"+filename,"w").write(contents)
		savedfile+=1

print "saved "+str(savedfile)+"files"