Codegolf

From NoName e.V.
Revision as of 02:04, 19 September 2007 by 78.51.68.80 (talk) (→‎watz: php: So .. noch ein bissl dies und das ...)
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

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>

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