diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-17 20:10:11 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-17 20:10:11 +0100 |
| commit | 01bf975e18ca9dc7753410fe1faba5a0a3cdc319 (patch) | |
| tree | 44596d90cf9c37e16048dcee169b066f52426a8d /challenge-074 | |
| parent | 175178615dbc82259edb00e9bb6d8ba4cc08af9d (diff) | |
| parent | 4aae778607028975e9b4b71b5908fc705c57d4be (diff) | |
| download | perlweeklychallenge-club-01bf975e18ca9dc7753410fe1faba5a0a3cdc319.tar.gz perlweeklychallenge-club-01bf975e18ca9dc7753410fe1faba5a0a3cdc319.tar.bz2 perlweeklychallenge-club-01bf975e18ca9dc7753410fe1faba5a0a3cdc319.zip | |
Merge pull request #2098 from oWnOIzRi/c074
add solutions for week 074
Diffstat (limited to 'challenge-074')
| -rw-r--r-- | challenge-074/steven-wilson/perl/ch-1.pl | 43 | ||||
| -rw-r--r-- | challenge-074/steven-wilson/perl/ch-2.pl | 67 |
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-074/steven-wilson/perl/ch-1.pl b/challenge-074/steven-wilson/perl/ch-1.pl new file mode 100644 index 0000000000..3810e1fa17 --- /dev/null +++ b/challenge-074/steven-wilson/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl +# Week74 Task 1 +# You are given an array of integers of size $N. +# +# Write a script to find the majority element. If none found then +# print -1. +# +# Majority element in the list is the one that appears more than +# floor(size_of_list/2). +# +# Example 1 +# Input: @A = (1, 2, 2, 3, 2, 4, 2) +# Output: 2, as 2 appears 4 times in the list which is more than +# floor(7/2). +# +# Example 2 +# Input: @A = (1, 3, 1, 2, 4, 5) +# Output: -1 as none of the elements appears more than +# floor(6/2). + +use strict; +use warnings; +use feature qw/ say /; +use POSIX qw/ floor /; + +my @A1 = ( 1, 2, 2, 3, 2, 4, 2 ); +say find_majority_element( \@A1 ); +my @A2 = ( 1, 3, 1, 2, 4, 5 ); +say find_majority_element( \@A2 ); + +sub find_majority_element { + my $elements_ref = shift; + my @elements = @{$elements_ref}; + my $majority = floor( scalar @elements / 2 ); + my %count_of_elements; + map { $count_of_elements{$_}++ } @elements; + for ( keys %count_of_elements ) { + if ( $count_of_elements{$_} > $majority ) { + return $_; + } + } + return -1; +} diff --git a/challenge-074/steven-wilson/perl/ch-2.pl b/challenge-074/steven-wilson/perl/ch-2.pl new file mode 100644 index 0000000000..20b7711f33 --- /dev/null +++ b/challenge-074/steven-wilson/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +# Week 74 Task 2 +# FNR Character +# +# You are given a string $S. +# +# Write a script to print the series of first non-repeating character +# (left -> right) for the given string. Print # if none found. +# Example 1 +# Input: $S = ‘ababc’ +# Output: ‘abb#c’ +# Pass 1: “a”, the FNR character is ‘a’ +# Pass 2: “ab”, the FNR character is ‘b’ +# Pass 3: “aba”, the FNR character is ‘b’ +# Pass 4: “abab”, no FNR found, hence ‘#’ +# Pass 5: “ababc” the FNR character is ‘c’ +# +# Example 2 +# Input: $S = ‘xyzzyx’ +# Output: ‘xyzyx#’ +# Pass 1: “x”, the FNR character is “x” +# Pass 2: “xy”, the FNR character is “y” +# Pass 3: “xyz”, the FNR character is “z” +# Pass 4: “xyzz”, the FNR character is “y” +# Pass 5: “xyzzy”, the FNR character is “x” +# Pass 6: “xyzzyx”, no FNR found, hence ‘#’ + +use strict; +use warnings; +use feature qw/ say /; +use List::MoreUtils qw/ first_index /; +use Test::More; + +my $string1_test = 'ababc'; +my $string2_test = 'xyzzyx'; +ok( first_non_repeating_characters($string1_test) eq 'abb#c', + "test \'$string1_test\'" ); +ok( first_non_repeating_characters($string2_test) eq 'xyzyx#', + "test \'$string2_test\'" ); +done_testing(); + +say first_non_repeating_characters($string1_test); +say first_non_repeating_characters($string2_test); + +sub first_non_repeating_characters { + my $string = shift; + my @non_repeating; + my @repeating; + my $result; + for my $char ( split //, $string ) { + my $in_repeating = grep { $_ eq $char } @repeating; + my $in_non_repeating = grep { $_ eq $char } @non_repeating; + if ($in_non_repeating) { + my $index = first_index { $_ eq $char } @non_repeating; + splice @non_repeating, $index, 1; + push @repeating, $char; + } + elsif ( !$in_repeating && !$in_non_repeating ) { + push @non_repeating, $char; + } + scalar @non_repeating > 0 + ? ( $result .= $non_repeating[-1] ) + : ( $result .= qw/ # / ); + } + return $result; +} + |
