Alexandru
  • Member for 11 years
  • Last seen more than 4 years ago
Tips for golfing in Python
30 votes

A one line function can be done with lambda: def c(a): if a < 3: return a+10 else: return a-5 can be converted to (note missing space 3and and 10or) c=lambda a:a<3and a+10or a-5

View answer
Print 1 to 1000 in C++ without semi-colons
29 votes

#error "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 ...

View answer
Interpret brainf***
13 votes

Python 275 248 255 I decided to give it a try. import sys i=0 b=[0]*30000 t='' for e in open(sys.argv[1]).read(): t+=' '*i+['i+=1','i-=1','b[i]+=1','b[i]-=1','sys.stdout.write(chr(b[i]))','b[i]=ord(...

View answer
Implement PCRE in your language.
Accepted answer
10 votes

Python Since implementing full PCRE is too much I implemented only an essential subset of it. Supports |.\.\w\W\s+*(). Input regexp must be correct. Examples: $ python regexp.py ^\s*(\w+)$ ...

View answer
196 algorithm code golf
10 votes

Python 2, 55 bytes Following JPvdMerwe suggestion: n=input() while`n`!=`n`[::-1]:n+=int(`n`[::-1]) print n Python 2, 62: n=raw_input() while n!=n[::-1]:n=`int(n)+int(n[::-1])` print n

View answer
The Luhn algorithm for verifying credit card numbers, etc
10 votes

Python 3, 77 bytes c=lambda a:sum(sum(divmod(int(a[-e-1])<<e%2,10))for e in range(len(a)))%10==0

View answer
Print largest integer you can with the fewest characters
10 votes

C 86 (including NL) main(i){ char b[8<<14]; memset(b,'9',8<<14); for(i=0;i<8<<15;i++) write(1,b,8<<14); } Prints 34_359_973_368 digits on my i7 620M. Challenge this score!...

View answer
99 Bottles Of Beer
10 votes

Curl 19 characters (requires internet connection) curl -L j.mp/eGv9K5

View answer
Contest: fastest way to sort a big array of Gaussian-distributed data
8 votes

Here is another sequential solution: #include <stdio.h> #include <stdlib.h> #include <algorithm> #include <ctime> typedef unsigned long long ull; int size; double *dbuf, *...

View answer
Finding "sub-palindromes".
7 votes

Python 124 r=raw_input() l=range(len(r)) print', '.join(set('"'+r[i:j+1]+'"'for i in l for j in l if i<j and r[i:j+1]==r[i:j+1][::-1]))

View answer
Sum of primes between given range
6 votes

C, 117 bytes main(a,b,s,j){ s=0,scanf("%d%d",&a,&b); for(a+=a==1;a<=b;a++) for(s+=a,j=2;j<a;) s-=a%j++?0:(j=a); printf("%d",s); }

View answer
Detect loops in a linked list
6 votes

C, non-golfed The idea is to have to pointers p1 and p2 that traverse the list. p2 moves twice as fast as p1. If p2 reaches p1 at some point then there is a loop. Here is the C code (not tested, ...

View answer
Palindromic palindrome checker
6 votes

C #include <stdio.h> // #include <string.h> // int main() { // char str[1024]; // fgets(str, sizeof(str), ...

View answer
Rand5() to Rand7()
6 votes

In Python: def Rand7(): while True: x = (Rand5() - 1) * 5 + (Rand5() - 1) if x < 21: return x/3 + 1

View answer
What's my name? Produce the name of the language indirectly
5 votes

cat 3 Create a file with the following content (source code) cat And run it like (execute the file): $ cat filename

View answer
Golf you a quine for great good!
4 votes

Python, 104 bytes Uses eval and repr a = "print 'a','=', repr(eval('a'))\nprint eval('a'),\n" print 'a','=', repr(eval('a')) print eval('a'),

View answer
Another amicable number problem
4 votes

Python, 67 a=lambda x:sum(i for i in range(1,x)if x%i<1) b=lambda x:x==a(a(x))

View answer
Implement Bogosort
4 votes

Python 61 Sorts in place. import random def f(l): while l!=sorted(l):random.shuffle(l)

View answer
IPv4 Integer Conversion Function
3 votes

Python 56 45 c=lambda x:eval('((('+x.replace('.','<<8)+'))

View answer
Shortest Floor Function
3 votes

C 126 (including NL) Doesn't use any built-in conversion such as (int)x. r(float x) { int p=*(int*)&x, f=p&8388607|1<<23, e=((p>>23)&255)-150; if(e>0)f*=1<<e; if(e&...

View answer
Find the Factorial!
2 votes

Python, 29 bytes f=lambda x:x and x*f(x-1)or 1

View answer
Contest: fastest way to sort a big array of Gaussian-distributed data
2 votes

Here is another sequential solution. This one uses the fact that the elements are normal distributed, and the I think the idea is generally applicable to get sorting close to linear time. The ...

View answer
Calculate 500 digits of pi
2 votes

Python3 136 Uses Madhava's formula. from decimal import * D=Decimal getcontext().prec=600 p=D(3).sqrt()*sum(D(2-k%2*4)/3**k/(2*k+1)for k in range(1100)) print(str(p)[:502]) Python3 164 Uses this ...

View answer
Fibonacci function or sequence
2 votes

Python a,b,n=0,1,10 while n:a,b,n=b,a+b,n-1;print b

View answer
Fibonacci function or sequence
2 votes

Python, 36 f=lambda x:x>1and f(x-1)+f(x-2)or x

View answer
Indent a string using given parentheses
1 votes

Python3, 184 182 chars import sys _,p,q,t=sys.argv i,f,x=0,1,print for e in t: if e in p:f or x();x(' '*i+e);i+=2;f=1 elif e in q:f or x();i-=2;f=1;x(' '*i+e) else:not f or x(' '*i,end='');f=x(e,...

View answer
Check if number is a sum of consecutive numbers or not
1 votes

C, 67 As mellamokb suggested all numbers, except powers of two, can be written as sum of positive consecutive numbers: main(int i,char**a){ printf((i=atoi(a[1]))&i-1?"true\n":"false\n"); }

View answer
Calculate 500 digits of pi
1 votes

bc -l (22 = 5 command line + 17 program) scale=500 4*a(1)

View answer
Compute modular inverse
Accepted answer
1 votes

Python 95 89 c is your function. Returns 0 if there is no inverse (i.e. when x is even). p=lambda x,y,m:y and p(x,y/2,m)**2*x**(y&1)%m or 1 c=lambda x,n:[0,p(x,2**n-1,2**n)][x%2]

View answer
Fibonacci function or sequence
1 votes

Bash 100 This is a very slow, but hey no performance penalty. First line needed. #!/bin/bash if [ $1 -lt 2 ]; then echo $1; exit; fi expr `$0 \`expr $1 - 1\`` + `$0 \`expr $1 - 2\``

View answer