Codegolf

From NoName e.V.
Revision as of 14:30, 23 September 2007 by 78.51.101.220 (talk) (Schönere (aber noch längere) Version des haskell-programms)
Jump to navigation Jump to search

Challenge #1

<SdK> gibts wörter, die nach rot13 n anderes existierendes wort ergeben?

Testdatei: http://qu.cx/~mxf/web2.gz == /usr/share/dict/web2
Gepiped durch wc -l sollte 154 rausfallen, da einige Wörter doppelt vorkommen. 
Falls Duplikate gefiltert werden, sollten es 132 sein.
Ausgabe in Form von "iraq->vend\n"

mxf: Perl

Fuer 154 Zeilen:

perl -nle'$w{$r=$_=lc}++;y&a-z&n-za-m&;$w{$_}&&print"$r->$_"' /usr/share/dict/web2

50+2 Zeichen

Fuer 132 Zeilen:

perl -nle'$w{$r=$_=lc}=1;y&a-z&n-za-m&;$w{$_}-->0&&print"$r->$_"' /usr/share/dict/web2

54+2 Zeichen

Das ganze in PHP

 php -r'foreach(file($argv[1],2)as$k){$f[$k=strtolower($k)]=0;if(isset($f[$r=str_rot13($k)]))echo$r."->$k\n";}' /usr/share/dict/web2 

154 Zeilen/102 Zeichen

urs: Perl

perl -nle'($$_=$_=lc)=~y^a-z^n-za-m^;print"$_->$$_"if$$$_' /usr/share/dict/web2

47+2 Zeichen. Gibt 154 Zeilen output.

PhilFry: Ruby

ruby -nle'y||={};$_.downcase!;y[$_]=0;r=$_.tr("a-z","n-za-m");y[r]&&(p $_+"->"+r)' < /usr/share/dict/web2

71 Zeichen

Ch3ka: php

<?$f=file($argv[1]);while($a[]=strtolower(next($f))){}while($b[]=str_rot13(next($a))){$c=end($b);if(in_array($c,$a))echo$c;}?>

122 Chrs

watz: PHP

<?require "File.php";foreach(explode("\n",strtolower(File::readAll($argv[1]))) as $f)$i[$f]=str_rot13($f);foreach($i as $a=>$b){if(array_key_exists($b,$i)){unset($i[$a]);echo"$a->$b\n";}}?>

190 Zeichen .. dafür schneller als Ch3ka .. Sortiert doppelte Worte auch raus --> 132 Zeilen+1 (1 Zeile leer weil eine Leerzeile im Dictionary) Man kann aber noch ein paar Zeichen rauskürzen und evlt. das mit dem File einlesen schöner machen.

Obige Zeile als PHP-Skript speichern und Aufruf mit php <php-skript> <dict>

Alternativ 186 Zeichen mit Aufruf:

php -r 'require "File.php";foreach(explode("\n",strtolower(File::readAll($argv[1]))) as $f)$i[$f]=str_rot13($f);foreach($i as $a=>$b){if(array_key_exists($b,$i)){unset($i[$a]);echo"$a->$b\n";}}' <dict>

Kungi: Python

import sys
e=dict([(a,0)for a in open(sys.argv[1]).read().lower().split()])
for w in e:
 b=w.encode('rot13')
 if b in e:
  print w+" -> "+b
kungi@BeerBook: wc golf.py                                                                                                                  
      6      19     140 golf.py

k-zed: common lisp

(with-open-file (s "/usr/share/dict/web2")
  (let ((d (make-hash-table :test #'equal)))
    (loop for l = (read-line s nil) until (not l)
          do (setf (gethash (string-downcase l) d) t))
    (maphash
      (lambda (k v)
        (let ((p (map 'string (lambda (c) (code-char (+ (mod (- (char-code c) 84) 26) 97))) k)))
          (when (gethash p d) (format t "~A -> ~A~%" k p)))) d)))

CentronX: eicar & ein Inder

Man lasse einen Inder das ganze in eicar eintippen und dann vergleichen.

Sack-C-ment: C natürlich

#include <stdio.h>
#include <stdlib.h>
#include <search.h>

int main() {
	FILE *fd = fopen("/tmp/web2", "r"); char *b = malloc(234937 * 26); int c = 0; ENTRY e,*d; hcreate(234937);
	while (fgets(b+(26*c++), 26, fd)) { char *r=strdup(b+(26*(c-1))); e.key=r; while (*(++r) != '\0' && *r != '\n') *r = (*r+13 > 'z' ? *r-13 : *r+13); e.data = b+(26*(c-1)); hsearch(e, ENTER); }
	for (c = 0; c < 234937; c++) { e.key = b+(26*c); if (d=hsearch(e, FIND)) printf("print it! %s / %s\n", d->data, d->key); }
}

Compile / Setup:

sort /usr/share/dict/web2 /tmp/web2
gcc -o golf golf.c && ./golf

Exakt 500 Zeichen

urs: Haskell

(Nein, eigentlich nicht auf länge optimiert. Aber wenn wir schon seltsame Programmiersprachen nehmen...)

import qualified Data.Set as Set
import Data.Char 

rot13 = map (\x->chr(ord 'a'+((ord x-ord 'a'+13)`mod`26)))
foo [] w = []
foo (x:xs) w | x `Set.member` w = x : foo xs w
             | otherwise        = foo xs (Set.insert r w)
             where r = rot13 x

main = do
    cts <- getContents
    let ws = words $ map toLower cts
    let rot13_words = foo ws Set.empty
    putStr $ concat $ concatMap (\x -> [x,"->",(rot13 x),"\n"]) rot13_words 

471 Zeichen. 154 Zeilen Ausgabe.