From 2beb8d5e88d43282d28574361836d02da421dcfd Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Mon, 17 Aug 2020 15:54:36 +0100 Subject: add solutions for week 074 --- challenge-074/steven-wilson/perl/ch-1.pl | 43 ++++++++++++++++++ challenge-074/steven-wilson/perl/ch-1.pl.bak | 43 ++++++++++++++++++ challenge-074/steven-wilson/perl/ch-2.pl | 67 ++++++++++++++++++++++++++++ challenge-074/steven-wilson/perl/ch-2.pl.bak | 67 ++++++++++++++++++++++++++++ 4 files changed, 220 insertions(+) create mode 100644 challenge-074/steven-wilson/perl/ch-1.pl create mode 100644 challenge-074/steven-wilson/perl/ch-1.pl.bak create mode 100644 challenge-074/steven-wilson/perl/ch-2.pl create mode 100644 challenge-074/steven-wilson/perl/ch-2.pl.bak 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-1.pl.bak b/challenge-074/steven-wilson/perl/ch-1.pl.bak new file mode 100644 index 0000000000..506ce33d7e --- /dev/null +++ b/challenge-074/steven-wilson/perl/ch-1.pl.bak @@ -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; +} + diff --git a/challenge-074/steven-wilson/perl/ch-2.pl.bak b/challenge-074/steven-wilson/perl/ch-2.pl.bak new file mode 100644 index 0000000000..20b7711f33 --- /dev/null +++ b/challenge-074/steven-wilson/perl/ch-2.pl.bak @@ -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; +} + -- cgit From 4aae778607028975e9b4b71b5908fc705c57d4be Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Mon, 17 Aug 2020 16:06:54 +0100 Subject: delete .bak files --- challenge-074/steven-wilson/perl/ch-1.pl.bak | 43 ------------------ challenge-074/steven-wilson/perl/ch-2.pl.bak | 67 ---------------------------- 2 files changed, 110 deletions(-) delete mode 100644 challenge-074/steven-wilson/perl/ch-1.pl.bak delete mode 100644 challenge-074/steven-wilson/perl/ch-2.pl.bak diff --git a/challenge-074/steven-wilson/perl/ch-1.pl.bak b/challenge-074/steven-wilson/perl/ch-1.pl.bak deleted file mode 100644 index 506ce33d7e..0000000000 --- a/challenge-074/steven-wilson/perl/ch-1.pl.bak +++ /dev/null @@ -1,43 +0,0 @@ -#!/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.bak b/challenge-074/steven-wilson/perl/ch-2.pl.bak deleted file mode 100644 index 20b7711f33..0000000000 --- a/challenge-074/steven-wilson/perl/ch-2.pl.bak +++ /dev/null @@ -1,67 +0,0 @@ -#!/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; -} - -- cgit