Home arrow Blog arrow Misc arrow Twitter #twitcode Archive
Twitter #twitcode Archive | Print |
Written by Akiba   
Saturday, 05 September 2009

A strange phenomenon occurred where many people started posting very interesting software on Twitter. The attraction is that Twitter limits posts to 140 characters and so it's a challenge to see what type of functionality you can implement within that 140 character limit. I've been completely astounded by the quality, functionality, and creativity of these twitcodes and I've decided to archive them here. I hope you can enjoy them as well.


Linked list library
  • Author: Adam Dunkels
  • Twitter: adunk
  • Desc: A generic linked list library that can be used as a building block for data structures
  • twitcode: Linked list library in #twitcode (it works! :)): typedef struct i{struct i*n;}i;i*h;push(i*i){i->n=h;h=i;}i*pop(){i*t;t=h;h=h->n;return t;}
twIP IP stack
  • Author: Adam Dunkels
  • Twitter: adunk
  • Desc: A very constrained IP stack that can respond to pings. This actually made it on to Slashdot .
  • twitcode: s[70];*l=s;main(){for(;;){read(0,s,140);if((s[4]&-256)==256&s[10]==8){s[10]=0;s[11]+=8;l[4]^=l[3]^=l[4]^=l[3];write(1,s,140);}}} #twitcode
Tiny Webserver
  • Author: Raluca Musaloiu
  • Twitter: ralucam
  • Desc: A 128 character webserver written in Python
  • twitcode: s=socket.socket();s.bind(('',80));s.listen(9) while(1): c,a=s.accept();c.send(open(c.recv(99)[5:].split()[0]).read());c.close() #twitcode
CPU Emulator
  • Author: Joakim Ericcson
  • Twitter: JOAKIM
  • Desc: A CPU emulator capable of 8 operations: a +/- b, a=b/b=a, jump, jump if a=0, break, two gp registers (a,b), 99 words RAM
  • twitcode: int m[99],*p,a,b,o;l(){o=*p++;if(o&4)o&1?a=*p++:(m[*p++]=a);if(o&8)o&2?(o&1?a+=b:(a-=b)):(o&1?a=b:(b=a));if(o&64&a)p+=*p;if(o)l();}#twitcode

Fibonacci on Twitter CPU Emulator

  • Author: Akiba
  • Twitter: freaklabs
  • Desc: Brute force calculation of fibonacci using opcodes from Joakim's CPU Emulator
  • twitcode: Instructions to calc 1st 8 vals in fibonacci series using @joakim emulator: p[]={5,0,8,5,1,b,8,b,8,5,1,b,8,5,2,b,8,5,3,b,8,5,5,b} #twitcode

Pseudorandom number generator

  • Author: Akiba
  • Twitter: freaklabs
  • PSNR based on linear feedback shift register
  • twitcode: #include <stdio.h>main(){uint16_t a=0x1;uint16_t b;for (;;){b=((a>>0)^(a>>2)^(a>>3)^(a>>5))&0x1;a=(a>>1)|(b<<15);printf("%X\n",a);#twitcode
Twitter post buffer stack 
  • Author: Akiba
  • Twitter: freaklabs
  • Desc: LIFO buffer stack for holding twitter posts, ie: in case you want to queue a batch of twitter posts for transmission. It was built using Adam Dunkel's linked list library twitcode :)
  • twitcode: typedef struct{i*n;char p[140];}t;t w[99];char a=0;in(char*d){memcpy(w[a].p,d,140);push(&w[a++]);*out(){t*b=pop();a--;return b->p;}#twitcode
Binary Search
  • Author: Raluca Musaloiu
  • Twitter: ralucam
  • Desc: Binary search
  • twitcode: def bsearch(l,k): p,r=0,len(l) while p<r: q=(p+r)/2 if k<l[q]:r=q elif k>l[q]:p=q+1 else:return q return -1 #twitcode

Quicksort

  • Author: Razvan Musaloiu
  • Twitter: razvanm
  • Desc: Quicksort algorithm
  • twitcode:  #twitcode def qsort(l): if not l: return l return qsort([e for e in l if e < l[0]]) + [l[0]] + qsort([e for e in l if e > l[0]])

Square Root Calculator

  • Author: #endif
  • Twitter: poundifdef
  • Desc: Calculate the square root of  a number
  • twitcode: #include<stdio.h> double sqrt(double x){double r=x/2;while((r-x/r)>.001)r=(r+(x/r))/2;return r;}main(){printf("%f\n",sqrt(150));} #twitcode

Largest Prime Factor

  • Author: Samuel Baldwin
  • Twitter: recursiveforest
  • Desc: Largest prime factor 
  • twitcode: $a=pop;$i=1;while(++$i<$a){unless($a%$i){$a/=$i;$i=1;}}print $a; #twitcode largest prime factor, idea from projecteuler.net

Hangman Game

  • Author: Joaquin Cabezas
  • Twitter: joaquincabezas
  • Desc: Hangman game 2.0 shows position, accept letter repetition, maximum 4 fails, 5 letter word
  • twitcode: int z,l,f=4;char b[5]="*****",a,*c="thang";main(){while(f){a=getchar();for(l=z=0;z<5;z++)c[z]==a?b[z]=a:l++;puts(b);getchar();if(l>4)f--;}}

ALLCAPS

  • Author: Joaquin Cabezas
  • Twitter: joaquincabezas
  • Desc: Convert string to all caps
  • twitcode: main(int a,char**b){int c;if(a==2){for(c=0;c<strlen(b[1]);c++)if(b[1][c]>96&&b[1][c]<123)b[1][c]-=32;puts(b[1]);}} #twitcode ALLCAPS

RC4 Stream Cipher:

  • Author: Joachim Strömbergson
  • Twitter: Kryptoblog
  • Desc: RC4 Stream Cipher
  • twitcode:  char S[256];char i=0;char j=0;char id, jd, kd;char main(){id=S[i];j=j+id%256;jd=S[j];S[i]=jd;S[j]=id;kd=S[(id+jd%256)];i++;return kd;}

Unix grep utility

  • Author: Adam Dunkels
  • Twitter: adunk
  • Desc: Implementation of Unix grep utility
  • twitcode: i;main(int c,char**v){char b[999];if(c>1)while(gets(b))for(i=0;i<strlen(b);i++)if(!strncmp(v[1],&b[i],strlen(v[1]))){puts(b);break;}}

Minimal Unix cat utility

  • Author: Adam Dunkels
  • Twitter: adunk
  • Desc: Implementation of minimal Unix cat utility
  • twitcode: #include <stdio.h>i,l,b;main(int c,char**v){for(i=1;i<c;i++){FILE *f;if(f=fopen(v[i],"r"))while(l=fread(&b,1,1,f))write(1,&b,l);}}

Unix style shell

  • Author: Adam Dunkels
  • Twitter: adunk
  • Desc: Unix style shell - (requires full paths to commands though)
  • twitcode: p;main(i){char b[99],*a[9];p=0;gets(b);a[0]=b;for(i=0;i<99;++i)if(b[i]==' ')b[i]=0,a[++p]=b+i+1;a[p+1]=0;execv(a[0],a);}

Modified Unix style shell 

  • Author: Frederik Hermans
  • Twitter: frederikhermans
  • Desc: Extended @adunk's shell, now it loops and prints '?' on error
  • twitcode: char b[99],*a[9];p,i;main(){a[0]=gets(b);for(;b[i];i++)b[i]==' '?b[i]=0,a[++p]=b+i+1:0;a[p+1]=0;fork()?i=p=0,main():execv(b,a)?puts("?"):0;}
First 2854 digits of pi
  • Author: Frederik Hermans
  • Twitter: frederikhermans
  • Desc: First 2854 digits of pi using spigot algorithm
  • twitcode: c=9999,f[9999],g,a=1e4,d,e;main(b){for(;(b=c-=14)>0;){while(--b)g?d=(d/g)*b+(e?a*f[b]:2e7):0,g=2*b-1,f[b]=d%g;printf("%04d",e+d/a);e=d%a;}}

Email sender

  • Author: Raluca Musaloiu
  • Twitter: ralucam
  • Desc: Email sender implementation:  Usage: <server><from><to><content>, with the subject on 1st line of content
  • twitcode: s=socket();v=argv[1:];s.connect((v[0],25)) for i in['MAIL FROM:'+v[1],'RCPT TO:'+v[2],'DATA','Subject:%s\n.'%v[3]]:s.recv(99);s.send(i+'\n')

CRC 16 calculator

  • Author: Adam Dunkels
  • Twitter: adunk
  • Desc: CITT CRC16 calculator using CITT CRC16 polynomial ^16 + ^12 + ^5 + 1
  • twitcode: c(char *d,int l){unsigned short a=0;int i;for(i=0;i<l;++i){a^=*d;a=(a>>8)|(a<<8);a^=(a&-256)<<4;a^=(a>>8)>>4;a^=(a&-256)>>5;++d;}return a;}

Prime factorization

  • Author: Samuel Baldwin
  • Twitter: recursiveforest
  • Desc: Prime factorization of some number (I assume)
  • twitcode: $b=$a=pop;while($a>1){while(++$i<$a){unless($a%$i){$a/=$i;$i=1;}}print "$a\n";$i=1;$b=$a=$b/$a;} #twitcode prime factorisation (no bugs)

Min-Heapify

  • Author: Razvan Musaloiu
  • Twitter: razvanm
  • Desc: The heapify function can be thought of as building a heap from the bottom up, successively shifting downward to establish the heap property. More info on wikipedia for heapsorting .
  • twitcode 1: minheapify = heap def buildminheap(v): l = len(v) for i in range(l/2, -1, -1): minheapify(v, i)
  • twitcode 2: def heap(v,k): l=len(v) i=2*k+1 j=i+1 i=[k,i][i<l and v[i]<v[k]] j=[i,j][j<l and v[j]<v[i]] if j!=k: v[j],v[k]=v[k],v[j] heap(v,j)

Unix Echo Server

  • Author: Frederik Hermans
  • Twitter: frederikhermans
  • Desc: Unix echo server in C. Listening at localhost:1031. Beware, doesn't close its sockets or print errors!
  • twitcode: char a[99]={2,0,4,7,127,0,0,1};main(s,t){s=socket(2,1,0);bind(s,a,16);listen(s,0);t=accept(s,0,0);bzero(a,99);read(t,a,99);write(t,a,99);} 
Julia set
  • Author:Yalsi
  • Twitter: yalsi
  • Desc: This twitcode generates a Julia set .
  • twitcode: float i,j,t;k,n;a(){t=i*i-j*j-1;j=2*i*j-.2;i=t;k++>99?0:a();}main(){for(;n++<1800;)j=n/999.-1,i=n%80/40.,k=0,a(),putchar(2>i*i+j*j?35:32);}

Base Converter

  • Author:Akiba
  • Twitter: freaklabs
  • Desc: Convert base-10 number into any other base up to base-9. Usage: c(value, base). Warning: prints out number in new base backwards due to iteration.
  • twitcode: #include<stdio.h> c(uint32_t v,uint8_t s){uint32_t a, b;do{a=v/s;b=v%s;printf("%d",b);v=a;} while ((v=a)>s);printf("%d", a);} #twitcode
Hits: 15230
Trackback(0)
Comments (0)Add Comment

Write comment

busy
  No Comments.

Discuss...
< Prev   Next >