From 5960d197c36cff4fefcb0bb8ed11cba9d9865407 Mon Sep 17 00:00:00 2001 From: jluehrs2 Date: Thu, 31 Jan 2008 03:49:51 -0500 Subject: implement random_permutation and is_permutation --- lib/Games/Word.pm | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/Games/Word.pm b/lib/Games/Word.pm index b0f37f8..0b57357 100644 --- a/lib/Games/Word.pm +++ b/lib/Games/Word.pm @@ -2,8 +2,35 @@ package Games::Word; use strict; use warnings; - - +use Math::Combinatorics qw/factorial/; +use Test::Deep::NoTest; + +sub random_permutation { + my $word = shift; + return '' if $word eq ''; + + use integer; + my $len = length $word; + my $perm_index = shift; + $perm_index = defined($perm_index) ? $perm_index : + int(rand(factorial($len))); + die "invalid permutation index" if $perm_index >= factorial($len) || + $perm_index < 0; + my $current_index = $perm_index / factorial($len - 1); + my $rest = $perm_index % factorial($len - 1); + + my $first_letter = substr($word, $current_index, 1); + substr($word, $current_index, 1) = ''; + + return $first_letter . random_permutation($word, $rest); +} + +sub is_permutation { + my @word_letters = split //, shift; + my @perm_letters = split //, shift; + + return eq_deeply(\@word_letters, bag(@perm_letters)); +} =head1 NAME -- cgit v1.2.3-54-g00ecf