Alexandru
• Member for 11 years
• Last seen more than 4 years ago
• Amsterdam, MO

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

#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 ...

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(...

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+)$ ...

Python 2, 55 bytes Following JPvdMerwe suggestion: n=input() whilen!=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

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

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!...

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

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

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]))

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); }

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, ...

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

cat 3 Create a file with the following content (source code) cat And run it like (execute the file): $cat filename View answer 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 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 4 votes Python 61 Sorts in place. import random def f(l): while l!=sorted(l):random.shuffle(l) View answer 3 votes Python 56 45 c=lambda x:eval('((('+x.replace('.','<<8)+')) View answer 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 2 votes Python, 29 bytes f=lambda x:x and x*f(x-1)or 1 View answer 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 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 2 votes Python a,b,n=0,1,10 while n:a,b,n=b,a+b,n-1;print b View answer 2 votes Python, 36 f=lambda x:x>1and f(x-1)+f(x-2)or x View answer 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 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 1 votes bc -l (22 = 5 command line + 17 program) scale=500 4*a(1) View answer 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 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\