diff options
| author | Pavel Kuptsov <pavel@kuptsov.info> | 2021-01-11 15:26:50 +0300 |
|---|---|---|
| committer | Pavel Kuptsov <pavel@kuptsov.info> | 2021-01-11 15:26:50 +0300 |
| commit | 30aa33d809fb2e134180f2ae22faeff82ed5c099 (patch) | |
| tree | ef3a5e5b0d0f54235be383101aee4e213010e77d | |
| parent | 5ab993f243ab1a835d8f30aa6c97b2d3b99496af (diff) | |
| download | perlweeklychallenge-club-30aa33d809fb2e134180f2ae22faeff82ed5c099.tar.gz perlweeklychallenge-club-30aa33d809fb2e134180f2ae22faeff82ed5c099.tar.bz2 perlweeklychallenge-club-30aa33d809fb2e134180f2ae22faeff82ed5c099.zip | |
Solution 094
| -rw-r--r-- | challenge-094/pavel_kuptsov/perl/ch-1.pl | 62 | ||||
| -rw-r--r-- | challenge-094/pavel_kuptsov/perl/ch-2.pl | 52 |
2 files changed, 114 insertions, 0 deletions
diff --git a/challenge-094/pavel_kuptsov/perl/ch-1.pl b/challenge-094/pavel_kuptsov/perl/ch-1.pl new file mode 100644 index 0000000000..5842d9fc6e --- /dev/null +++ b/challenge-094/pavel_kuptsov/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl -w + +use strict; +use warnings; +use feature qw(say); +use DDP; + +=head2 + +TASK #1 › Group Anagrams +Submitted by: Mohammad S Anwar +You are given an array of strings @S. + +Write a script to group Anagrams together in any random order. + +An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. + +Example 1: + Input: ("opt", "bat", "saw", "tab", "pot", "top", "was") + Output: [ ("bat", "tab"), + ("saw", "was"), + ("top", "pot", "opt") ] +Example 2: + Input: ("x") + Output: [ ("x") ] + +=cut + +my @S = ("opt", "bat", "saw", "tab", "pot", "top", "was"); + +my %output; +my @output; + +for my $str(@S) +{ + my $anagram; + $anagram .= $_ for (sort split //, $str ); + push @{$output{$anagram}}, $str; +} + +@output = values %output; + +p @output; + +=head2 +perl ch-1.pl +[ + [0] [ + [0] "opt", + [1] "pot", + [2] "top" + ], + [1] [ + [0] "bat", + [1] "tab" + ], + [2] [ + [0] "saw", + [1] "was" + ] +] +=cut
\ No newline at end of file diff --git a/challenge-094/pavel_kuptsov/perl/ch-2.pl b/challenge-094/pavel_kuptsov/perl/ch-2.pl new file mode 100644 index 0000000000..6492dd4744 --- /dev/null +++ b/challenge-094/pavel_kuptsov/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl -w + +use strict; +use warnings; +use feature qw(say); +use Tree::Binary; + +=head2 + +TASK #2 › Binary Tree to Linked List +Submitted by: Mohammad S Anwar +You are given a binary tree. + +Write a script to represent the given binary tree as an object and flatten it to a linked list object. Finally print the linked list object. + +Example: + Input: + + 1 + / \ + 2 3 + / \ + 4 5 + / \ + 6 7 + + Output: + + 1 -> 2 -> 4 -> 5 -> 6 -> 7 -> 3 + +=cut + +my($btree) = Tree::Binary->new('1')->setLeft( Tree::Binary->new('2')->setLeft(Tree::Binary->new('4'))->setRight(Tree::Binary->new('5')->setLeft(Tree::Binary->new('6'))->setRight( Tree::Binary->new('7') ) ))->setRight(Tree::Binary->new('3')); + +$btree->traverse +( + sub + { + my($tree) = @_; + print " -> " unless $tree->isRoot; + print $tree->getNodeValue; + } +); + +print "\n"; + +__END__ + +perl ch-2.pl +1 -> 2 -> 4 -> 5 -> 6 -> 7 -> 3 + + |
