in reply to Words in Words
Update: if I ommit the "just once" condition, the script finishes in 40 secs on my Mac Mini.#!/usr/bin/perl use feature 'say'; use warnings; use strict; my $file = '/etc/dictionaries-common/words'; open my $IN, '<', $file or die "$!"; my %words; while (my $word = <$IN>) { chomp $word; undef $words{$word}; } for my $word (keys %words) { my $length = length $word; my %found; # report each occurence just once for my $pos (0 .. $length - 1) { my $skip_itself = ! $pos; for my $len (1 .. $length - $pos - $skip_itself) { my $subword = substr($word, $pos, $len); next if exists $found{$subword}; if (exists $words{$subword}) { say "$subword in $word"; undef $found{$subword}; } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Words in Words
by sarchasm (Acolyte) on Sep 30, 2011 at 21:45 UTC | |
by choroba (Cardinal) on Sep 30, 2011 at 23:31 UTC | |
by sarchasm (Acolyte) on Sep 30, 2011 at 23:41 UTC | |
by choroba (Cardinal) on Sep 30, 2011 at 23:54 UTC |