aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-11-03 15:24:00 +0000
committerGitHub <noreply@github.com>2019-11-03 15:24:00 +0000
commitf3bce14cc2b060bed6430bf73232f1851856f13c (patch)
tree153eb3b08724b118b873f08493ce8f1d1f9eb957
parent7c664dfab584d8e735edcfd32ebaa2e299499d1d (diff)
parente382b51b2112cfcbbcb4e316aa028bb954afa73a (diff)
downloadperlweeklychallenge-club-f3bce14cc2b060bed6430bf73232f1851856f13c.tar.gz
perlweeklychallenge-club-f3bce14cc2b060bed6430bf73232f1851856f13c.tar.bz2
perlweeklychallenge-club-f3bce14cc2b060bed6430bf73232f1851856f13c.zip
Merge pull request #881 from toshikFedotov/master
Added solution by Anton Fedotov for Challenge 032
-rwxr-xr-xchallenge-032/anton-fedotov/perl5/ch-1.pl47
-rwxr-xr-xchallenge-032/anton-fedotov/perl5/ch-2.pl57
-rw-r--r--challenge-032/anton-fedotov/perl5/example0.txt13
-rw-r--r--challenge-032/anton-fedotov/perl5/example1.txt18
-rw-r--r--challenge-032/anton-fedotov/perl5/example2.txt12
5 files changed, 147 insertions, 0 deletions
diff --git a/challenge-032/anton-fedotov/perl5/ch-1.pl b/challenge-032/anton-fedotov/perl5/ch-1.pl
new file mode 100755
index 0000000000..30488cf93c
--- /dev/null
+++ b/challenge-032/anton-fedotov/perl5/ch-1.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+
+#########################
+# Challenge 032, part 1 #
+#########################
+
+use strict;
+use warnings;
+use v5.10;
+
+my $encoding = ':encoding(UTF-8)';
+
+binmode STDIN, $encoding;
+binmode STDOUT, $encoding;
+
+my %words;
+my $space_pos = 15;
+
+# Reading and counting all words from
+# all files
+while (my $file_name = shift) {
+ open (my $file_handle, "< $encoding", $file_name)
+ or die "Can't open file $file_name: $!";
+ while (<$file_handle>) {
+ chomp;
+ $words{$_}++ if defined $words{$_};
+ $words{$_} = 1 unless defined $words{$_};
+ }
+ close $file_handle;
+}
+
+# Sorting all words by the number of
+# occurrences
+for my $word (sort { $words{$b} <=> $words{$a} } keys %words) {
+ my $spaces = count_spaces($word);
+ say $word.$spaces.$words{$word};
+}
+
+# Subfunction for calculating the number
+# of spaces to position $space_pos
+sub count_spaces {
+ my $word = shift;
+ my $n_spaces = $space_pos - length $word;
+ my $spaces = ' ';
+ for (1 .. $n_spaces) {$spaces = $spaces . ' '}
+ return $spaces;
+}
diff --git a/challenge-032/anton-fedotov/perl5/ch-2.pl b/challenge-032/anton-fedotov/perl5/ch-2.pl
new file mode 100755
index 0000000000..b96b0bddaa
--- /dev/null
+++ b/challenge-032/anton-fedotov/perl5/ch-2.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/env perl
+
+##########################
+# Challenge 032, part 2 #
+##########################
+
+use strict;
+use warnings;
+use v5.10;
+
+my $encoding = ':encoding(UTF-8)';
+binmode STDOUT, $encoding;
+
+my $data = {
+ lenovo => 5,
+ acer => 4,
+ xiaomi => 6,
+ opticom => 1,
+ tatysh_corp => 10
+};
+generate_bar_graph($data);
+
+sub generate_bar_graph {
+ my $info = shift;
+ my $line_pos = calc_line_pos($info);
+ for my $word (sort {$info->{$b} <=> $info->{$a}} keys %$info) {
+ my $spaces = calc_start_spaces($word, $line_pos);
+ my $bars = calc_bars($info->{$word});
+ say $spaces.$word.' | '.$bars;
+ }
+}
+
+sub calc_line_pos {
+ my $info = shift;
+ my $max_length = 0;
+ foreach my $word (keys %$info) {
+ my $w_length = length $word;
+ $max_length = $w_length if $max_length < $w_length;
+ }
+ return $max_length++;
+}
+
+sub calc_start_spaces {
+ my ($word, $line_pos) = @_;
+ my $w_length = length $word;
+ my $n_spaces = $line_pos - $w_length;
+ my $spaces = '';
+ for (1 .. $n_spaces) {$spaces .=' '}
+ return $spaces;
+}
+
+sub calc_bars {
+ my $val = shift;
+ my $result = '';
+ for (1 .. $val) {$result.='###'}
+ return $result;
+}
diff --git a/challenge-032/anton-fedotov/perl5/example0.txt b/challenge-032/anton-fedotov/perl5/example0.txt
new file mode 100644
index 0000000000..943f4f93ab
--- /dev/null
+++ b/challenge-032/anton-fedotov/perl5/example0.txt
@@ -0,0 +1,13 @@
+чебурашка
+чебурашка
+банан
+лол
+кек
+чебурек
+мем
+чебурашка
+мем
+супер кек
+дарт вейдер
+люк
+дарт вейдер
diff --git a/challenge-032/anton-fedotov/perl5/example1.txt b/challenge-032/anton-fedotov/perl5/example1.txt
new file mode 100644
index 0000000000..bea37d6f0e
--- /dev/null
+++ b/challenge-032/anton-fedotov/perl5/example1.txt
@@ -0,0 +1,18 @@
+чебурашка
+чебурашка
+банан
+лол
+кек
+чебурек
+мем
+чебурашка
+мем
+супер кек
+дарт вейдер
+люк
+дарт вейдер
+perl
+perl
+perl
+raku
+raku
diff --git a/challenge-032/anton-fedotov/perl5/example2.txt b/challenge-032/anton-fedotov/perl5/example2.txt
new file mode 100644
index 0000000000..427e9fcaa0
--- /dev/null
+++ b/challenge-032/anton-fedotov/perl5/example2.txt
@@ -0,0 +1,12 @@
+perl
+raku
+camel
+camelia
+camel
+camelia
+camelia
+camelia
+camel
+camel
+perl
+perl