diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-04-17 16:53:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-17 16:53:19 +0100 |
| commit | 07ef14ea26fc5b2e570075a48f2f0eae3f40662a (patch) | |
| tree | 4df5a3e4c40c4b53af00af72f580efc675436d0a | |
| parent | b4f38875d2be2a08869f1d46048fa22b5a5cf8e6 (diff) | |
| parent | 24176c775e09d300cdedc76b0d0dd37e07add0c8 (diff) | |
| download | perlweeklychallenge-club-07ef14ea26fc5b2e570075a48f2f0eae3f40662a.tar.gz perlweeklychallenge-club-07ef14ea26fc5b2e570075a48f2f0eae3f40662a.tar.bz2 perlweeklychallenge-club-07ef14ea26fc5b2e570075a48f2f0eae3f40662a.zip | |
Merge pull request #3901 from lakpatashi/branch-005
Finished challenge-005 with perl
| -rw-r--r-- | challenge-005/lakptashi/README | 1 | ||||
| -rwxr-xr-x | challenge-005/lakptashi/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-005/lakptashi/perl/ch-2.pl | 28 |
3 files changed, 59 insertions, 0 deletions
diff --git a/challenge-005/lakptashi/README b/challenge-005/lakptashi/README new file mode 100644 index 0000000000..bc153bd576 --- /dev/null +++ b/challenge-005/lakptashi/README @@ -0,0 +1 @@ +Solution by lakpatashi diff --git a/challenge-005/lakptashi/perl/ch-1.pl b/challenge-005/lakptashi/perl/ch-1.pl new file mode 100755 index 0000000000..caaf6003fe --- /dev/null +++ b/challenge-005/lakptashi/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +# PART 1 +my $word = "abcde"; +print "Given word: $word\n"; +my @arr = split '', $word; +my $count=0; +for ( permutate(@arr) ){ + print join '',(@$_),"\n"; + $count++; +} +print "Total no. of words: $count\n"; + +sub permutate { + return [@_] if @_ <= 1; + map { + my ($f, @r) = list_with_x_first($_, @_); + map [$f, @$_], permutate(@r); + } 0..$#_; +} + +sub list_with_x_first { + return if @_ == 1; + my $i = shift; + ($_[$i], @_[0..$i-1], @_[$i+1..$#_]); +} + diff --git a/challenge-005/lakptashi/perl/ch-2.pl b/challenge-005/lakptashi/perl/ch-2.pl new file mode 100755 index 0000000000..1c6591f1a1 --- /dev/null +++ b/challenge-005/lakptashi/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +# PART 2 +use Data::Dumper; +my %anaCount; +my @wordList = ('abc','aabb','aedccc','aabbcc'); + +my @max = (0,''); +for my $word (@wordList){ + my %dict; + ++$dict{$_} for split('', $word); + #print Dumper(\%dict); + my $deno = 1; + $deno *= factorial($_) for values %dict; + my $result = int factorial(length($word)) / $deno; + @max = ($result,$word) if $result> $max[0]; +} +print "word with max anagrams:\n"; +print "@max\n"; +sub factorial { + my ($n) = @_; + $n *= $_ for 2 .. $n - 1; + return $n +} + |
