-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap_char.pl
50 lines (45 loc) · 1.13 KB
/
swap_char.pl
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
########################################################
# file name : swap_char.pl
# description :
# creation date : 21-01-2014
# author : dainv
# update
#
########################################################
#!/usr/bin/perl
use v5.18;
use warnings;
sub swap {
if (scalar(@_) != 1) { die; }
if ($_[0] eq "") { die; }
my @result = ();
my @chars = split('', $_[0]);
my $num = scalar(@chars);
my $word = "";
if ($num == 1) {
push (@result, $chars[0]);
}
elsif ($num == 2) {
$word = join("", @chars);
push(@result, $word);
$word = join("", reverse(@chars));
push(@result, $word);
}
elsif ($num > 2) {
for(my $i = 0; $i <= $#chars; $i++) {
my $first = shift(@chars);
my @tmp = swap(join('', @chars));
foreach my $w(@tmp) {
$word = join("", $first, $w);
push(@result, $word);
}
push(@chars, $first);
}
}
return (@result);
}
my $input = $ARGV[0] || "";
my @arr = swap($input);
for(my $i = 0; $i <= $#arr; $i++) {
print "$arr[$i]\n";
}