Lowjacker
  • Member for 10 years, 8 months
  • Last seen more than 7 years ago
57 answers
155 votes
31k views
Tips for golfing in C
122 votes

Use bitwise XOR to check for inequality between integers: if(a^b) instead of if(a!=b) saves 1 character.

View answer
6 answers
77 votes
37k views
Hard code golf: Regex for divisibility by 7
31 votes

468 characters Ruby's regex flavor allows recursion (although it's sort of cheating), so it is straightforward to implement a DFA that recognizes numbers divisible by 7 using that. Each named group ...

View answer
53 answers
72 votes
27k views
Tips for golfing in Ruby
31 votes

Use abort to terminate the program and print a string to STDERR - shorter than puts followed by exit If you read a line with gets, you can then use ~/$/ to find its length (this doesn't count a ...

View answer
1 answers
17 votes
3k views
Regex validating regex
Accepted answer
26 votes

Ruby I tried to match the actual syntax of Ruby's regex flavor as much as possible, but there are a few quirks: it accepts a few lookbehinds that are actually invalid (like (?<=(?<!))), and it ...

View answer
156 answers
392 votes
268k views
Write a program that makes 2 + 2 = 5
24 votes

Ruby class Fixnum alias plus + def + (other) plus(other).succ end end puts 2 + 2 The side-effects of this are even worse than the Java version.

View answer
57 answers
155 votes
31k views
Tips for golfing in C
23 votes

Since usually EOF == -1, use the bitwise NOT operator to check for EOF: while(~(c=getchar())) or while(c=getchar()+1) and modify value of c at every place

View answer
5 answers
17 votes
2k views
Write Buggy Code
Accepted answer
15 votes

Ruby, 136 characters, 7 bugs, ratio = 0.051 o=gets[/\d+/].to_i $,='\s' a=gets.split(/ /).map{|w|w.gsub(/[^.,:;?!]/){(97+($&.ord-97-o)%26).chr}} print a.join,a.sort_by{|a|a[0]}.join [/\d+/]: ...

View answer
40 answers
38 votes
5k views
Print the alphabet four times
10 votes

Ruby 1.9, 88 87 84 80 puts b=[*?a..?z],:qwertyuiopasdfghjklzxcvbnmpyfgcrlaoeuidhtnsqjkxbmwvz,b.reverse

View answer
14 answers
15 votes
1k views
Calculate a user's privileges on CodeGolf.SE
8 votes

Ruby 1.9.3, 514 467 459 (507 460 452 + 7 for flags) Run with ruby -rzlib <program>. #coding:binary r=0 $<.each{|l|b=l.sub!('un','')?-1:1 r+=('cvuwdsaifo'[/.#{l[9]||break}/].ord-2)*b} puts"#{...

View answer
11 answers
21 votes
2k views
Interpret your lang, but not yourself?
Accepted answer
8 votes

Ruby, 63 b=$<.read t="b=$<.read\nt=%p\nb!=t%%t&&eval(b)" b!=t%t&&eval(b)

View answer
14 answers
7 votes
942 views
400th Question Celebration/Challenge
7 votes

Ruby 1.upto(side=gets.to_i){|i|puts (?**i).center side} This creates a wobbling tree, like the following: * ** *** **** ***** ****** *******

View answer
23 answers
17 votes
3k views
Indent a string using given parentheses
7 votes

Ruby, 106 101 96 95 s,e,i=$* i.scan(/[#{z=Regexp.quote s+e}]|[^#{z}]*/){|l|puts' '*(s[l]?~-$.+=1:e[l]?$.-=1:$.)+l} Input is provided via the command line.

View answer
13 answers
18 votes
1k views
Pirate Treasure
Accepted answer
7 votes

Ruby 1.9, 175 171 162 153 130 120 117 l=0 gets.scan(/(\w+) (\d+)/){|d,n|l+=n.to_i*?i.to_c**%w[E NE N NW W SW S SE].index(d).quo(2)} puts'%.3f, %.3f'%l.rect

View answer
10 answers
14 votes
3k views
Hangman wordgame golf
Accepted answer
6 votes

Ruby 1.9, 134 132 120 117 108 107 Word list provided in ARGV. The words and the entered letters must match in case. r=w=$*.sample t=10 loop{puts [*w.tr(r,?_).chars]*' ',t t>0&&r>''?w[l=...

View answer
11 answers
27 votes
2k views
Simulate a Minsky Register Machine (I)
6 votes

Ruby 1.9, 214 212 211 198 195 192 181 175 173 175 *s,k=*$< a,=s b=Hash.new 0 eval k.gsub /(\w+)=/,';b["\1"]=' loop{x,y,r,o,t,f=a.scan /".*?"|\S+/ l=(b[r]-=o<=>?,)<0?(b[r]=0;f):t l[?"]&...

View answer
7 answers
28 votes
3k views
Score a Cribbage Hand
5 votes

Ruby 1.9, 359 356 It's far too long - almost as much as the C solution. R='A23456789TJQK' y=gets f=y.scan /\w+/ o=f.map(&:chr).sort_by{|k|R.index k} s=0 2.upto(5){|i|o.combination(i){|j|t=0 j....

View answer
21 answers
53 votes
5k views
1P5: Nested boxes
5 votes

Ruby 1.9, 229 228 226 223 222 g=->n{(1..n).map{g[Integer l=gets.chop]rescue l}} w=->b{b.bytesize rescue b.map{|e|w[e]}.max+4} p=->b,c{r=c-2 [?.+?-*r+?.,*b.map{|i|p[i,c-4]}.flatten.map{|k|"| #...

View answer
11 answers
13 votes
886 views
The last digit in an exponentation
5 votes

Ruby, 97 93 72 71 67 61 60 Also handles the case where b == 0. #!ruby -nl ~/ / p eval"#$`%10*"*($'>?0?($'.to_i-1)%4+1:0)+?1 Guess it's actually worse to use a lookup table.

View answer
10 answers
14 votes
1k views
Create a Twitter Parser
4 votes

Ruby, 75 Can't beat Perl, but at least it marginally beats the other Ruby solution. Note that it prints the tweets in reverse order (the problem does not specify one). f=->t,i=1{t=~/\S.{,130}\S(?!...

View answer
9 answers
21 votes
2k views
Implement lazy lists, preferably in a language you don't know well
4 votes

Python Doesn't use generators to implement the list, just to implement the __iter__ method for use with for. class Node(object): def __init__(self, head, tail): self.__head__ = head ...

View answer
50 answers
20 votes
87k views
How to determine if a number is odd or even without mod -or- bitwise operations?
4 votes

Ruby n.odd? If you want to print out the result: f[n] = ->(n){puts n.odd?? 'odd' : 'even'}

View answer
28 answers
22 votes
7k views
Find whether a number is happy or not?
4 votes

Befunge, 109 Returns correct values for 1<=n<=109-1. v v < @,,,,,"Happy"< >"yppahnU",,,,,,,@ >&>:25*%:*\25*/:#^_$+++++++++:1-!#^_:4-!#^_10g11p

View answer
148 answers
71 votes
17k views
The alphabet in programming languages
4 votes

A is for AppleScript, 41 -> Score: 52 display alert"abcdefghijklmnopqrstuvwxyz"

View answer
17 answers
5 votes
15k views
Find all numbers between 2 and 100 co-prime to a given number
3 votes

Ruby, 52 46 (43 + 3 for " -p" option) Now does beat it J (barely). Caveat: doesn't exit after outputting (you can actually keep giving more inputs after the first). $_=(2..100).select{|w|w.gcd(eval$...

View answer
7 answers
9 votes
3k views
Count the number of cyclic words in an input
3 votes

Python (3) again The method I used was to calculate a rolling hash of each word starting at each character in the string; since it's a rolling hash, it takes O(n) (where n is the word length) time to ...

View answer
4 answers
14 votes
690 views
A Mere Bagatelle
3 votes

Ruby, 123 117 115 107 99 98 97 *b,_,_,n=*$< p eval n.split.map{|k|i=k.to_i+1 b.map{|l|i-='\ /'.index(l[i])-1} [i-1,20-i].min}*?+

View answer
9 answers
19 votes
2k views
Choose Your Own Adventure
3 votes

Ruby 1.9, 98 j=$<.map &:split f=->*p,c{c=='100'?abort(p*' '):j.map{|a,b|a==c&&!p.index(b)&&f[*p,b,b]}} f[?1,?1] Ungolfed: $lines = $<.map &:split def f (*path) ...

View answer
10 answers
23 votes
2k views
Meta Golf Challenge
3 votes

Ruby, 146 characters + 4 spaces b=$<.map &:split puts b.map{|u,l,c,v|[b.map{|_,n|n.size}.max-l.size+b.count{|_,n|n==l}**2+eval(c+?-+v),u]}.sort.map.with_index{|(s,u),i|[i+1,u,s]*' '}

View answer
4 answers
17 votes
2k views
Wordsearch Solver
Accepted answer
3 votes

Ruby 1.9, 214 210 206 182 177 173 172 166 s,G=$<.read.split$/*2 O=G.tr'^ ',' ' (s+$/+s.reverse).split.map{|w|[0,l=G=~/$/,l+1,l-1].map{|d|(k=G=~/#{[*w.chars].*?.*d}/m)&&w.size.times{|i|O[k+...

View answer
19 answers
35 votes
6k views
1P5: Iterated Prisoner's Dilemma
3 votes

"Probabimatic" Starts by cooperating, then picks whichever option gives it the highest expected value. Simple. #include <stdio.h> void counts(char* str, int* k, int* r, int* s, int* e) { *...

View answer