Code Golf: Beehive

Perl, 99 characters @P=map{$/.substr$”.’__/ \\’x99,$_,$W||=1+3*pop}0,(3,6)x pop; chop$P[0-$W%2];print” __”x($W/6),@P Last edit: Saved one character replacing -($W%2) with 0-$W%2 (thanks A. Rex) Explanation: For width W and height H, the output is 2+2 * H lines long and 3 * W+1 characters wide, with a lot of repetition in the middle of the output. For convenience, we … Read more

Code Golf: All +-*/ Combinations for 3 integers

Perl 130 chars So long as external libraries are permitted: use Algorithm::Permute”permute”; permute{for$x(@a=qw(+ – / *)){for$y(@a){$_=”@ARGV”;s/ /$x/;s/ /$y/;printf” $_ = %.0f”,eval}}}@ARGV 2nd newline is significant. Without a module, and assuming that all three inputs are distinct, here’s another solution: @n=& ARGV; @o=( q[+], “-“, q{/}, ‘*’ );; for$ {a}(@ n){ for $b(@n){for$c(@ {n}){ for $x( … Read more

Code Golf: Leibniz formula for Pi

J, 14 chars 4*-/%>:+:i.1e6 Explanation 1e6 is number 1 followed by 6 zeroes (1000000). i.y generates the first y non negative numbers. +: is a function that doubles each element in the list argument. >: is a function that increments by one each element in the list argument. So, the expression >:+:i.1e6 generates the first … Read more

Code Golf: Countdown Number Game

Python Number of characters: 548 482 425 421 416 413 408 from operator import * n=len def C(N,T): R=range(1<<n(N));M=[{}for i in R];p=1 for i in range(n(N)):M[1<<i][1.*N[i]]=”%d”%N[i] while p: p=0 for i in R: for j in R: m=M[i|j];l=n(m) if not i&j:m.update((f(x,y),”(“+s+o+t+”)”)for(y,t)in M[j].items()if y for(x,s)in M[i].items() for(o,f)in zip(‘+-*/’,(add,sub,mul,div))) p|=l<n(m) return min((abs(x-T),e)for t in M for(x,e)in t.items())[1] … Read more

Code Golf – Banner Generation

J, 133 135 79 83 84 88 characters (utf-8 encoding) ;/5 3$”1(‘ ‘,.s){~”1#:3 u:(ucp’翇篭篯礧歮禧禤祯寭璗牯宭䤧彭忭筯篤筿殭秏璒孯孪寿咕寏犧’){~0>.64-~a.i.s=: Usage: ;/5 3$”1(‘ ‘,.s){~”1#:3 u:(ucp’翇篭篯礧歮禧禤祯寭璗牯宭䤧彭忭筯篤筿殭秏璒孯孪寿咕寏犧’){~0>.64-~a.i.s=:’ABCDEFGHIJKLMNOPQRSTUVWXYZ !’ ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐ │AAA│BBB│CCC│DD │EEE│FFF│GGG│H H│III│JJJ│K K│L │M M│N N│OOO│PPP│QQQ│RR │SSS│TTT│U U│V V│W W│X X│Y Y│ZZZ│ │!!!│ │A A│B B│C │D D│E │F │G │H H│ I │ J│K K│L │MMM│NNN│O O│P P│Q Q│R R│S │ T … Read more

Code Golf: Spider webs

Perl, 164 chars 195 184 171 167 164 print@o=((map{$z=_ x($x=1+$N-$_);$”x$x.”https://stackoverflow.com/”x$_.”\\$z|$z/”.’\ ‘x$_.$/}0..($N=<>)), “_/”x++$N.” “.’\_’x$N.$/); y’/\\’\/’,@o||y#_# #,$t++||y#_ # _#,print while$_=pop@o First statement prints out the top half of the spider web. Second statement uses transliteration operations to create a reflection of the top half. This next one weighs in closer to 314 chars (of productive code), but … Read more

Code Golf: Seven Segments

Perl, 134 characters All linebreaks may be removed. @s=unpack”C*”,~”P\xdbLI\xc3a`[\@AB\xe0t\xc8df”; $_=<>;for$s(6,3,0){print map$s[hex$&]&1<<$_+$s?$_%2?”_”:”|”:” “, 0..2while/./g;print$/} Explanation @s stores the bits for each segment. The entries are in order from 0 to F (thanks to hex()) and the bits map to segments in this order: 6 7 x 3 4 5 0 1 2 with 0 being the … Read more