JBernardo
  • Member for 10 years, 6 months
  • Last seen this week
Tips for golfing in Python
70 votes

A nice way to convert an iterable to list on Python 3: imagine you have some iterable, like i = (1,2,3,4) i = range(4) i = (x**2 for x in range(5)) But you need a list: x=list(i) #the default way ...

View answer
The alphabet in programming languages
15 votes

P is for Python 2, 30 chars -> Score: 36 I did it, after 8 years I realized there is a shorter way in python 2! print bytearray(range(97,123)) Previous code that was as big as trivial print: print'...

View answer
Meta Golf Challenge
11 votes

Python, 184 That's why I love spaces. import sys x = sys.stdin.read( ).split() z = x [ 1 : : 4 ] for i , ( j , k ) in enumerate ( sorted ( zip ( [ int(i) ...

View answer
400th Question Celebration/Challenge
8 votes

Brainf*ck [-]>++++[<+++++++++++++>-]<.----..---------------. And the output will be: 400!

View answer
Implement division
6 votes

Python, 70 Something crazy I just thought (using comma separated input): from cmath import* x,y=input() print round(tan(polar(y+x*1j)[1]).real) If you accept small float precision errors, the round ...

View answer
Scream very loudly
4 votes

Python 2, 19 bytes This might be cheating as it can only work on a terminal, since each space added in between "A" is removed with a backspace char. while 1:print'\bA',

View answer
Resolve quadratic equation
4 votes

Python 3, 79 chars a,b,c=eval(input()) d=(b*b-4*a*c)**.5/2/a x=-b/2/a print('r1=',x+d,'\nr2=',x-d) Python's imaginary unity is j and not i. I used Python 3 because the power operator works also on ...

View answer
Draw an ASCII chess board!
3 votes

Python 3, 59 bytes print('rnbqkbnr','p'*8,*['.'*8]*4,'P'*8,'RNBQKBNR',sep='\n') Interesting that with better unpacking and print function, Python 3 can be much better than Python 2 for golf This ...

View answer
Undulant numbers
3 votes

Python, 134 129 chars def f(x):d=[cmp(*i)for i in zip(`x`,`x`[1:])]if x>9 else[0];n=d[0]>0;return all(i<0 for i in d[n::2])&all(i>0 for i in d[n<1::2]) Ungolfed: def f(x): if ...

View answer
The alphabet in programming languages
2 votes

P is for Python 3, 32 chars -> Score: 38 print('%c'*26%(*range(97,123),)) I'm on fire. This is based in my 2011 answer which is before Python 3.5 added starred tuple unpacking, making it smaller ...

View answer
Soundex function
2 votes

Perl, 110 sub f{$_="$_[0]000";/./;$t=$&;s/(?<=.)[HW]//g;y/A-Z/:123:12_:22455:12623:1_2:2/s;s/(?<=.)\D//g;/.(...)/;$t.$1} I'm using Howard's solution with my translate table (y/A-Z/table/s ...

View answer
Soundex function
2 votes

Python, 274 285 241 235 225 200 190 183 179 174 166 161 - Fixed last clause (H or W as consonant separators). Ashcraft now has the right result. - Made the dict smaller - Formating is smaller (doesn'...

View answer
Negative Regexp
Accepted answer
2 votes

Python, 49 chars import re f=lambda x:'(^(?!%s$).*$)'%re.escape(x) The answer matches anything but the input. It gives an output using conditional groups so is suited for the problem: regular ...

View answer
Which side has more?
1 votes

Python 3.8, 32 bytes lambda s:((r:=s[::-1])!=s)+(r<s) Try it online! It is just one byte shorter than other Python 3.x lambda s:(s[::-1]!=s)+(s[::-1]<s)

View answer
JAxH obfuscated method, not text
1 votes

Python My JAPH in Python class Foo: def __init__(s, t='Just another Python hacker'): def g(): return [100,101,102,32,102, 40,120,41,58,112,114, ...

View answer
Output the spoken names of numbers used on planet Flapus
1 votes

Python 3 - 181 177 bytes (Ponara'bosti) a='Ku Po Bo Co Vru Fo Sti Kri zla nara qkel lopee enat ham kty cola'.split() *y,x=map(int,input()) u=x>0 v=y>[] print(a[x]+a[x+8]*u**v+"'"*(u&v)+''....

View answer
There Was an Old Lady
1 votes

Python, 484 Ok, I did it but was pretty boring... The last sentence is always with "fly" so some chars were removed... f,s,b,c,d=o='fly spider bird cat dog'.split() x='There was an old lady who ...

View answer
Unicode character classification
1 votes

Python 3, 1369 chars (not bytes) a='...

View answer
Markov Chain Beatbox Generator
0 votes

Just a reference implementation in Python: from random import random def find(num, pdf): ''' Find position of number in CDF from PDF (must sum 100%) ''' cdf = (sum(pdf[:i+1]) for i in range(...

View answer
Sum of primes between given range
0 votes

Python, 133 A little bit of sorcery: x,y=map(int,raw_input().split()) y+=1 a=range(y) print sum(i for i in[[i for a[::i]in[([0]*y)[::i]]][0]for i in a[2:]if a[i]]if i>=x)

View answer