From 3a85a00c68fe0718ec916493826ea63e0f8a7066 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:24:43 +0800 Subject: Add files via upload --- ch-1.pl | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ch-2.pl | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 ch-1.pl create mode 100644 ch-2.pl diff --git a/ch-1.pl b/ch-1.pl new file mode 100644 index 0000000000..feb190e26c --- /dev/null +++ b/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 diff --git a/ch-2.pl b/ch-2.pl new file mode 100644 index 0000000000..bae04b73b3 --- /dev/null +++ b/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +# Perl Weekly Challenge #074 Task 2 FNR character +# task statement: +# 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. +# Usage: ch-2.pl [string] + +use strict; +use warnings; +#use Test::More tests => 5; + +sub fnr { + my @uniquestack; + my %charcount; + my $ans = ""; + my @characters = split //, $_[0]; + for my $char (@characters) { + if (!exists $charcount{$char} ) { + push @uniquestack , $char; + $charcount{$char} = 1; + $ans .= $char; + } + else { + $charcount{$char}++; + @uniquestack = grep { $charcount{$_} == 1 } @uniquestack; + $ans .= (scalar @uniquestack != 0) ? $uniquestack[-1] : "#"; + } + } + return $ans; + +} + +print fnr("$ARGV[0]"); + +=pod +is_deeply( fnr("ababc") , "abb#c", "example1 provided"); +is_deeply( fnr("xyzzyx") , "xyzyx#", "example2 provided"); +is_deeply( fnr("abcdef") , "abcdef", "trival"); +is_deeply( fnr("aaabbb") , "a##b##", "repeats"); +is_deeply( fnr( + "thequickbrownfoxjumpsoverthelazydog") , + "thequickbrownffxjjmpssvvvvvvlazyddg", + "long sentence" +); +=cut -- cgit From fec0a3a9a3e9aa82ae07a08b9d9d91571e2f5f0f 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:23 +0800 Subject: Rename ch-2.pl to challenge-074/cheok-yin-fung/perl/ch-2.pl --- ch-2.pl | 48 ------------------------------- challenge-074/cheok-yin-fung/perl/ch-2.pl | 48 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 48 deletions(-) delete mode 100644 ch-2.pl create mode 100644 challenge-074/cheok-yin-fung/perl/ch-2.pl diff --git a/ch-2.pl b/ch-2.pl deleted file mode 100644 index bae04b73b3..0000000000 --- a/ch-2.pl +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/perl -# Perl Weekly Challenge #074 Task 2 FNR character -# task statement: -# 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. -# Usage: ch-2.pl [string] - -use strict; -use warnings; -#use Test::More tests => 5; - -sub fnr { - my @uniquestack; - my %charcount; - my $ans = ""; - my @characters = split //, $_[0]; - for my $char (@characters) { - if (!exists $charcount{$char} ) { - push @uniquestack , $char; - $charcount{$char} = 1; - $ans .= $char; - } - else { - $charcount{$char}++; - @uniquestack = grep { $charcount{$_} == 1 } @uniquestack; - $ans .= (scalar @uniquestack != 0) ? $uniquestack[-1] : "#"; - } - } - return $ans; - -} - -print fnr("$ARGV[0]"); - -=pod -is_deeply( fnr("ababc") , "abb#c", "example1 provided"); -is_deeply( fnr("xyzzyx") , "xyzyx#", "example2 provided"); -is_deeply( fnr("abcdef") , "abcdef", "trival"); -is_deeply( fnr("aaabbb") , "a##b##", "repeats"); -is_deeply( fnr( - "thequickbrownfoxjumpsoverthelazydog") , - "thequickbrownffxjjmpssvvvvvvlazyddg", - "long sentence" -); -=cut diff --git a/challenge-074/cheok-yin-fung/perl/ch-2.pl b/challenge-074/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..bae04b73b3 --- /dev/null +++ b/challenge-074/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +# Perl Weekly Challenge #074 Task 2 FNR character +# task statement: +# 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. +# Usage: ch-2.pl [string] + +use strict; +use warnings; +#use Test::More tests => 5; + +sub fnr { + my @uniquestack; + my %charcount; + my $ans = ""; + my @characters = split //, $_[0]; + for my $char (@characters) { + if (!exists $charcount{$char} ) { + push @uniquestack , $char; + $charcount{$char} = 1; + $ans .= $char; + } + else { + $charcount{$char}++; + @uniquestack = grep { $charcount{$_} == 1 } @uniquestack; + $ans .= (scalar @uniquestack != 0) ? $uniquestack[-1] : "#"; + } + } + return $ans; + +} + +print fnr("$ARGV[0]"); + +=pod +is_deeply( fnr("ababc") , "abb#c", "example1 provided"); +is_deeply( fnr("xyzzyx") , "xyzyx#", "example2 provided"); +is_deeply( fnr("abcdef") , "abcdef", "trival"); +is_deeply( fnr("aaabbb") , "a##b##", "repeats"); +is_deeply( fnr( + "thequickbrownfoxjumpsoverthelazydog") , + "thequickbrownffxjjmpssvvvvvvlazyddg", + "long sentence" +); +=cut -- cgit 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 From 421c43c0346eeeaf994cba0481e66773b432a098 Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Sun, 23 Aug 2020 07:37:03 +0800 Subject: Update ch-1.pl condense --- challenge-074/cheok-yin-fung/perl/ch-1.pl | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/challenge-074/cheok-yin-fung/perl/ch-1.pl b/challenge-074/cheok-yin-fung/perl/ch-1.pl index feb190e26c..0b67899143 100644 --- a/challenge-074/cheok-yin-fung/perl/ch-1.pl +++ b/challenge-074/cheok-yin-fung/perl/ch-1.pl @@ -16,9 +16,7 @@ sub verify { my $m = $_[1]; my $c = 0; for (@array) { - if ($m==$_) { - $c++; - } + $c++ if $m==$_; } return ($c > (scalar @array)/2.0 ? 1 : undef); } @@ -32,11 +30,8 @@ sub bm_majority_vote_alg { $m = $_; $i++ } - elsif ($m == $_) { - $i++; - } else { - $i--; + $m == $_ ? $i++ : $i--; } } -- cgit