alexr_rwx: (my fandom writes your software)
Alex R ([personal profile] alexr_rwx) wrote2004-03-26 02:36 pm

"Are you asking for a challenge???!!"

For 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 :)


(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)))

[identity profile] falun.livejournal.com 2004-03-26 04:08 pm (UTC)(link)
perl -- 49 minutes (and i don't completely understand how the \G works, alhtough i do have an idea) - 13 lines
#!/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();
        }
    }
}

[identity profile] falun.livejournal.com 2004-03-26 06:33 pm (UTC)(link)
i just noticed you wanted it to read in an file and to spit them out -- oops
ext_110843: (hand)

[identity profile] oniugnip.livejournal.com 2004-03-26 06:38 pm (UTC)(link)
Yeah, but these are Very Close And Could Be Easily Modified To Do That.
ext_110843: (majestic)

[identity profile] oniugnip.livejournal.com 2004-03-26 06:35 pm (UTC)(link)
Rawk :)