cemper93
  • Member for 10 years, 7 months
  • Last seen more than 1 year ago
13 answers
40 votes
5k views
Advanced Code Golf - Write a small HTTP server
12 votes

Python 2, 525 510 493 (bending the rules 483) chars from socket import* from os.path import* a=socket(2,1) a.bind(("",80)) a.listen(5) while 1: c,_=a.accept();i=c.recv(512).split("\n&...

View answer
14 answers
7 votes
942 views
400th Question Celebration/Challenge
Accepted answer
10 votes

Python, FOUR HUNDRED bits i=0 while 1:print" "*(i%8>3 and-i%4 or i%4),0;i+=1 At first I wanted to make a sine scroller, but there was no place for the import. Now you get a bugged triangle wave. ~~~...

View answer
9 answers
12 votes
2k views
Creative Programming
8 votes

Python Love def initely(there, were): if not None: atLeast = not "many" who.made("my heart") "beat as", you.do(_,_) return your.smile warm = "ly" try: toKeep = "it" + ...

View answer
158 answers
302 votes
111k views
Tips for golfing in Python
8 votes

Abuse the fact that in case of an expression yielding True boolean operators return the first value that decides about the outcome of the expression instead of a boolean: >>> False or 5 5 ...

View answer
4 answers
14 votes
690 views
A Mere Bagatelle
4 votes

Python 2, 147 132 130 chars import sys s=0 l=list(sys.stdin) for t in l[-1].split(): p=int(t)+1 for r in l[:-3]:p-=" /".find(r[p]) s+=min(p-1,20-p) print s

View answer
38 answers
20 votes
4k views
'Add' up the letters in a word
4 votes

Python 3, 95 chars with dictionary d=input().lower() print(d in open("d").read()and sum(['',ord(c)-96]['`'<c<'{']for c in d)or'f') The dictionary has to be in a file called d. Python ...

View answer
43 answers
44 votes
9k views
Reverse Polish notation
2 votes

Python 3, 126 132 chars s=[2,2] for c in input().split(): a,b=s[:2] try:s[:2]=[[a+b,b-a,a*b,a and b/a]["+-*/".index(c)]] except:s=[float(c)]+s print(s[0]) There have been better ...

View answer
15 answers
7 votes
473 views
Remove r-th powers
2 votes

Python 2, 84 83 80 chars r,n=input() r+=.0 i=c=1 while c<=n: p=i**(1/r) if p!=int(p):print i;c+=1 i+=1 Runs in about 3 seconds for r=2, n=1000000

View answer
17 answers
12 votes
3k views
Draw a BCD Binary clock
2 votes

Python 3, 136 chars import time d=input() for i in(0,1):print(''.join([" .':"[[c>>2,c&3][i]]for c in map(int,d.replace(':','')or time.strftime('%H%M%S'))]))

View answer
28 answers
15 votes
3k views
Implement division
1 votes

Python, 46 bytes Nobody had posted the boring subtraction solution, so I could not resist doing it. a,b=input() i=0 while a>=b:a-=b;i+=1 print i

View answer
117 answers
64 votes
21k views
99 Bottles Of Beer
1 votes

Python 2 (1129 1120 chars) import zlib,base64 print zlib.decompress(base64.b64decode(b"eF6dmk1u2mAABfec4jtAhJr/cIJeoBcA4apVCa6AKOrtiyJ1ldeZJ2/t8ZvdCOxvsxm7+XI5TOcxfx+7aTqN+TguP6bxvj0cbsbm0/...

View answer
138 answers
86 votes
12k views
Determine whether strings are anagrams
1 votes

Python 2, without sorting as that starts to get boring - 104 chars def f(a,b): a,b=list(a),list(b) while a: try:b.remove(a.pop()) except:return return len(a)==len(b)

View answer
31 answers
32 votes
16k views
Rand5() to Rand7()
1 votes

Python, 56 37 chars Another solution that may be wrong, in Python: rand7 = lambda: sum(rand5() for i in range(7)) % 7 + 1 This seems to be too simple, but when I try: counter = [0] * 7 for i in ...

View answer
4 answers
9 votes
2k views
N-Queens problem
0 votes

Python 2, 190 185 chars from itertools import* n=input() print len(filter(lambda x:all(1^(y in(z,z+i-j,z-i+j))for i,y in enumerate(x)for j,z in enumerate(x[:i]+(1e9,)+x[i+1:])),permutations(range(1,n+...

View answer