aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Wilson <steven1170@zoho.eu>2020-08-17 16:06:54 +0100
committerSteven Wilson <steven1170@zoho.eu>2020-08-17 16:06:54 +0100
commit4aae778607028975e9b4b71b5908fc705c57d4be (patch)
tree993e240fafd1099011721de13faed3090b36cb51
parent2beb8d5e88d43282d28574361836d02da421dcfd (diff)
downloadperlweeklychallenge-club-4aae778607028975e9b4b71b5908fc705c57d4be.tar.gz
perlweeklychallenge-club-4aae778607028975e9b4b71b5908fc705c57d4be.tar.bz2
perlweeklychallenge-club-4aae778607028975e9b4b71b5908fc705c57d4be.zip
delete .bak files
-rw-r--r--challenge-074/steven-wilson/perl/ch-1.pl.bak43
-rw-r--r--challenge-074/steven-wilson/perl/ch-2.pl.bak67
2 files changed, 0 insertions, 110 deletions
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;
-}
-