From e9dcdecd99edb78db01d5b81e4a9f3a1c527ae1b Mon Sep 17 00:00:00 2001 From: Michael Lee Firkins Date: Thu, 21 Apr 2022 09:35:56 +0700 Subject: pwc161 refactor perl solution, go solution to accept argument as path input file --- challenge-161/pokgopun/go/ch-1.go | 6 +++++- challenge-161/pokgopun/go/ch-2.go | 6 +++++- challenge-161/pokgopun/perl/ch-1.pl | 21 +++++++++++---------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/challenge-161/pokgopun/go/ch-1.go b/challenge-161/pokgopun/go/ch-1.go index 9e15a8cf09..863f6cbd05 100644 --- a/challenge-161/pokgopun/go/ch-1.go +++ b/challenge-161/pokgopun/go/ch-1.go @@ -9,7 +9,11 @@ import ( ) func main() { - f, err := os.Open("../../../data/dictionary.txt") + dict := "../../../data/dictionary.txt" + if len(os.Args) > 1 { + dict = os.Args[1] + } + f, err := os.Open(dict) if err != nil { log.Fatal(err) } diff --git a/challenge-161/pokgopun/go/ch-2.go b/challenge-161/pokgopun/go/ch-2.go index 28ccb9f249..07fee3a83c 100644 --- a/challenge-161/pokgopun/go/ch-2.go +++ b/challenge-161/pokgopun/go/ch-2.go @@ -31,7 +31,11 @@ func (bs byteSeen) countUnseen(s string) (n int) { return n } func main() { - f, err := os.Open("../../../data/dictionary.txt") + dict := "../../../data/dictionary.txt" + if len(os.Args) > 1 { + dict = os.Args[1] + } + f, err := os.Open(dict) if err != nil { log.Fatal(err) } diff --git a/challenge-161/pokgopun/perl/ch-1.pl b/challenge-161/pokgopun/perl/ch-1.pl index f792a41e4d..567b05f850 100644 --- a/challenge-161/pokgopun/perl/ch-1.pl +++ b/challenge-161/pokgopun/perl/ch-1.pl @@ -2,6 +2,7 @@ use strict; use warnings; my $dict = @ARGV ? shift : "../../../data/dictionary.txt"; +printf "%s\n", join(", ",abc($dict)); sub abc{ my $dict = shift; @@ -12,20 +13,20 @@ sub abc{ my @abc; while (my $line = ref $IN eq 'GLOB' ? <$IN> : shift @$IN){ chomp $line; - my @a = split //,$line; - my @b; - { - push @b, shift @a; - last unless @a; - last if $b[-1] gt $a[0]; - redo; - } - push @abc,$line if length($line)==@b; + push @abc,$line if isAbc($line); } @abc = sort{ length $b <=> length $a} @abc unless ref $IN eq 'ARRAY'; return @abc } -printf "%s\n", join(", ",abc($dict)); +sub isAbc{ + my @a = split //, shift; + if (@a > 1) { + for (my $i = 1; $i < @a; $i++){ + return 0 if $a[$i] lt $a[$i-1]; + } + } + return 1; +} -- cgit