Codegolf/1

From NoName e.V.
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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

Sack-C-ment modified by jchome

#include <stdio.h>
#include <search.h>
#define A 234937
main(){FILE *f=fopen("web2", "r");char *b=malloc(A*26);int c=0;ENTRY e,*d;hc
reate(A);while(fgets(b+(26*c++),26,f)){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<A;c++){e.key=b+(26*c);if(d=hsearch(e,FIND))print
f("%s->%s\n",d->data,d->key);}}


`--> wc foo.c                   
4      14     392 foo.c

urs: Haskell

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

import qualified Data.Set as S
import Data.Char
r=map(\x->chr(ord 'a'+((ord x-ord 'a'+13)`mod`26)))
f [] w=[]
f (x:y) w |x`S.member`w=x:f y w
          |otherwise=f y (S.insert (r x) w)
main = do
    cts <- getContents
    putStr$concat$concatMap(\x->[x,"->",(r x),"\n"])$f(words$map toLower cts) S.empty

299 Zeichen. 154 Zeilen Ausgabe.


Nachtrag

Jiska: Bash

Langsam aber 89 Zeichen kurz:

for i in `cat $1`;do x=`grep ^$(echo $i|tr A-Za-z N-ZA-Mn-za-m)$ $1`&&echo "$x->$i";done