"Are you asking for a challenge???!!"
Mar. 26th, 2004 02:36 pmFor all j00 l337 hackers out there...
Choose your favourite language; how many lines and how many minutes does it take you to write a program that takes in an arbitrary text file, find all the unique words in it, and spit them back?
(I had need to do this, just a few minutes ago, and I did it in like 20 very calm, relaxed lines of LISP, in just a coupla minutes... but I bet if you're good at Perl, you could do it in fewer lines and fewer minutes than that)
Post source :)
Choose your favourite language; how many lines and how many minutes does it take you to write a program that takes in an arbitrary text file, find all the unique words in it, and spit them back?
(I had need to do this, just a few minutes ago, and I did it in like 20 very calm, relaxed lines of LISP, in just a coupla minutes... but I bet if you're good at Perl, you could do it in fewer lines and fewer minutes than that)
Post source :)
(load "ci-utils.lisp") ;; for macros get-hash-keys and dolines (defvar *words* (make-hash-table :test #'equal)) (defun populate-words (filename) (dolines (line filename) (loop for word in (getwords line) do (setf (gethash word *words*) t)))) (defun getwords (line) (read-from-string (format nil "(~a)" (strip-period line)))) (defun strip-period (str) (string-trim "." str)) (defun list-of-words () (get-hash-keys *words*)) (defun print-all-words () (loop for word in (list-of-words) do (format t "~a~%" word)))
no subject
Date: 2004-03-26 02:34 pm (UTC)no subject
Date: 2004-03-26 02:55 pm (UTC)I'm trying to conceptualize how you'd do this in 1 line of Perl, because, you know, Everything Can Be Reduced To One Line Of Perl, but it might be a very long one...
no subject
Date: 2004-03-26 04:08 pm (UTC)#!/usr/bin/perl $file="test"; %words = {}; open(FILE, $file); while ($line = <FILE>) { pos($line) = 0; #non essential while ($line =~ /\G(\w+)\s*/g) { $words{$1} = 1; } } print scalar(keys %words) - 1;Java - 6 minutes - 28 lines
import java.util.*; import java.io.*; public class Challenge { public static void main(String[] args) { try { FileReader fr = new FileReader("test"); BufferedReader br = new BufferedReader(fr); String line; Hashtable table = new Hashtable(); int count = 0; while((line = br.readLine()) != null) { StringTokenizer tok = new StringTokenizer(line); while (tok.hasMoreTokens()) { String token = tok.nextToken().toString(); if (table.get(token) == null) { table.put(token, "!null"); count++; } } } System.out.println("count: " + count); } catch (Exception e) { e.printStackTrace(); } } }no subject
Date: 2004-03-26 06:33 pm (UTC)no subject
Date: 2004-03-26 06:38 pm (UTC)no subject
Date: 2004-03-26 06:35 pm (UTC)