summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjluehrs2 <jluehrs2@uiuc.edu>2008-02-02 05:17:50 -0500
committerjluehrs2 <jluehrs2@uiuc.edu>2008-02-02 05:17:50 -0500
commitc462ccb8de508c001004462f1a9e2be8e9dc74cd (patch)
tree6a2c71d4ed147a00f2558661ab539bdd502a0a20
parent39f2c4429dd46dc12d167ea43126f0d63156bb25 (diff)
downloadgames-word-c462ccb8de508c001004462f1a9e2be8e9dc74cd.tar.gz
games-word-c462ccb8de508c001004462f1a9e2be8e9dc74cd.zip
add is_substring and all_substrings to mess with strings made up of (not necessarily consecutive) letters in order from a given string
-rw-r--r--lib/Games/Word.pm29
1 files changed, 28 insertions, 1 deletions
diff --git a/lib/Games/Word.pm b/lib/Games/Word.pm
index ba3a03a..cd54cdb 100644
--- a/lib/Games/Word.pm
+++ b/lib/Games/Word.pm
@@ -4,7 +4,8 @@ require Exporter;
@ISA = qw/Exporter/;
@EXPORT_OK = qw/random_permutation is_permutation all_permutations
shared_letters shared_letters_by_position
- random_string_from/;
+ random_string_from
+ is_substring all_substrings/;
use strict;
use warnings;
@@ -110,6 +111,32 @@ sub random_string_from {
return $ret;
}
+sub is_substring {
+ my ($word, $substring) = @_;
+
+ return 1 unless $substring;
+ my $re = join('?', map { quotemeta } split(//, $substring)) . '?';
+ return $word =~ $re;
+}
+
+sub all_substrings {
+ my $string = shift;
+
+ return ('') unless $string;
+
+ my @substrings = ($string);
+ my $before = '';
+ my $current = substr $string, 0, 1, '';
+ while ($current) {
+ @substrings = (@substrings,
+ map { $before . $_ } all_substrings($string));
+ $before .= $current;
+ $current = substr $string, 0, 1, '';
+ }
+
+ return @substrings;
+}
+
=head1 NAME
Games::Word - ???