aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-12 12:43:42 +0000
committerGitHub <noreply@github.com>2021-01-12 12:43:42 +0000
commit12e7cdb4d14920ce3bb87631ef4683a9df3c4c07 (patch)
tree2c75f670a23abed44504fab3e9d515804f82ebc3
parent69261ac16ceb1e44ea94be1f8918688b2b5cda7a (diff)
parent30aa33d809fb2e134180f2ae22faeff82ed5c099 (diff)
downloadperlweeklychallenge-club-12e7cdb4d14920ce3bb87631ef4683a9df3c4c07.tar.gz
perlweeklychallenge-club-12e7cdb4d14920ce3bb87631ef4683a9df3c4c07.tar.bz2
perlweeklychallenge-club-12e7cdb4d14920ce3bb87631ef4683a9df3c4c07.zip
Merge pull request #3224 from poizon/branch-for-challenge-094
Solution 094
-rw-r--r--challenge-094/pavel_kuptsov/perl/ch-1.pl62
-rw-r--r--challenge-094/pavel_kuptsov/perl/ch-2.pl52
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
+
+