-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations.pl
More file actions
82 lines (62 loc) · 2.24 KB
/
Copy pathpermutations.pl
File metadata and controls
82 lines (62 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#! /usr/bin/perl -W
use strict;
use warnings;
##########################################################################
# The purpose of this script it to generate all permutations of a string #
##########################################################################
# 1) create a string to test with
my $string = "abc";
print "Length of the string is ".length($string)."\n";
# 2) call our function
&getPerms($string);
###################################
# This is where the magic happens #
###################################
sub getPerms($) {
my %permutations; # we will use this hash to store the permutations
my $length;
for (my $i = 0; $i < length($_[0]); $i++) {
# get all of the letters in the string 1 character at a time
# from left to right
# determine the length of the input string
$length = length($_[0]);
# since the length of the string is one larger than indexes into the string
# we set up a little logic to continuously get the proper string length
# to provide to the substring command
if ($length - $i != 1) {
$length = $i - $length + 1;
}
else {
$length = 1;
}
my $char = substr($_[0], $i , $length);
my $prefix = substr($_[0], 0 , $i);
my $postfix = substr($_[0], $i+1, length($_[0]) - $i);
my $short = $prefix.$postfix;
print "Current Letter: $char \tprefix $prefix \tpostfix $postfix\n";
# this loop will start from the beginning of the prefix and
# go until the position of the letter we have stripped off
# here we have another off by one error since the length of the string is 1 shorter than it used to be
for (my $j = 0; $j <= length($short); $j++) {
my $lhs = substr($short, 0, $j);
my $rhs = substr($short, $j, length($short) - $j);
my $perm;
if (length($lhs) == 0) {
$perm = $char.$short;
}
elsif (length($rhs) == 0) {
$perm = $short.$char;
}
else {
$perm = $lhs.$char.$rhs;
}
print "The permuatation is: $perm\t LHS: $lhs\tCHAR: $char\tRHS: $rhs\n";
$permutations {$perm} = 1;
}
}
while ((my $key, my $value) = each(%permutations)) {
print $key."\n";
}
my $count = scalar keys %permutations;
print $count."\n";
}