From 0dc9a9f30184ec760f442d60aec1fdf9b92dbbaf Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Wed, 19 Aug 2020 10:25:59 +0800 Subject: Rename ch-1.pl to challenge-074/cheok-yin-fung/perl/ch-1.pl --- ch-1.pl | 54 ------------------------------- challenge-074/cheok-yin-fung/perl/ch-1.pl | 54 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 54 deletions(-) delete mode 100644 ch-1.pl create mode 100644 challenge-074/cheok-yin-fung/perl/ch-1.pl diff --git a/ch-1.pl b/ch-1.pl deleted file mode 100644 index feb190e26c..0000000000 --- a/ch-1.pl +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/perl -# ref: -# https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm -# Perl Weekly Challenge #074 Task 1 Majority Element -# task statement: -# Write a script to find the majority element. -# If none found then print -1. -# Usage: ch-1.pl [ARRAY] -use strict; -use warnings; -#use Test::More tests => 3; - - -sub verify { - my @array = @{$_[0]}; - my $m = $_[1]; - my $c = 0; - for (@array) { - if ($m==$_) { - $c++; - } - } - return ($c > (scalar @array)/2.0 ? 1 : undef); -} - -sub bm_majority_vote_alg { - my @array = @{$_[0]}; - my $i = 0; - my $m; - for (@array) { - if ($i == 0) { - $m = $_; - $i++ - } - elsif ($m == $_) { - $i++; - } - else { - $i--; - } - } - - - return ( verify(\@array, $m) ? $m : -1 ); -} - - -print bm_majority_vote_alg(\@ARGV); - -=pod -is_deeply( bm_majority_vote_alg( [1, 2, 2, 3, 2, 4, 2] ) , "2", "example1 provided"); -is_deeply( bm_majority_vote_alg( [1, 3, 1, 2, 4, 5] ) , "-1", "example2 provided"); -is_deeply( bm_majority_vote_alg( [2, 2, 2, 3, 1, 3, 4] ) , "-1", "array: 2223134"); -=cut diff --git a/challenge-074/cheok-yin-fung/perl/ch-1.pl b/challenge-074/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..feb190e26c --- /dev/null +++ b/challenge-074/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl +# ref: +# https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm +# Perl Weekly Challenge #074 Task 1 Majority Element +# task statement: +# Write a script to find the majority element. +# If none found then print -1. +# Usage: ch-1.pl [ARRAY] +use strict; +use warnings; +#use Test::More tests => 3; + + +sub verify { + my @array = @{$_[0]}; + my $m = $_[1]; + my $c = 0; + for (@array) { + if ($m==$_) { + $c++; + } + } + return ($c > (scalar @array)/2.0 ? 1 : undef); +} + +sub bm_majority_vote_alg { + my @array = @{$_[0]}; + my $i = 0; + my $m; + for (@array) { + if ($i == 0) { + $m = $_; + $i++ + } + elsif ($m == $_) { + $i++; + } + else { + $i--; + } + } + + + return ( verify(\@array, $m) ? $m : -1 ); +} + + +print bm_majority_vote_alg(\@ARGV); + +=pod +is_deeply( bm_majority_vote_alg( [1, 2, 2, 3, 2, 4, 2] ) , "2", "example1 provided"); +is_deeply( bm_majority_vote_alg( [1, 3, 1, 2, 4, 5] ) , "-1", "example2 provided"); +is_deeply( bm_majority_vote_alg( [2, 2, 2, 3, 1, 3, 4] ) , "-1", "array: 2223134"); +=cut -- cgit