From 853d1f80f9bd379710e909c89274ef5abfd788e7 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Sat, 16 Apr 2022 14:25:40 +0700 Subject: pwc160_task#1 to avoid unnecessary regex operation --- challenge-160/pokgopun/go/ch-1.go | 17 ++++++----------- challenge-160/pokgopun/perl/ch-1.pl | 13 ++++++------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/challenge-160/pokgopun/go/ch-1.go b/challenge-160/pokgopun/go/ch-1.go index 4e6506934b..82ced9abe4 100644 --- a/challenge-160/pokgopun/go/ch-1.go +++ b/challenge-160/pokgopun/go/ch-1.go @@ -4,15 +4,12 @@ import ( "fmt" "log" "os" - "regexp" "strconv" "strings" ) func main() { m := []string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"} - //fmt.Println(m) - re := regexp.MustCompile(`\S+$`) var num []int if len(os.Args) > 1 { num = argInts() @@ -21,20 +18,18 @@ func main() { } for _, n := range num { fmt.Println("Input: $n =", n) - str := m[n-1] + w := m[n-1] + var str string for { - r := re.FindString(str) - if r == "four" { + if w == "four" { str += " is magic" break } else { - nxt := m[len(r)-1] - str += " is " + nxt + ", " + nxt - r = str - + w = m[len(w)-1] + str += " is " + w + ", " + w } } - fmt.Printf("Output: %v.\n\n", strings.Title(str[:1])+str[1:]) + fmt.Print("Output: " + strings.Title(m[n-1]) + str + ".\n\n") } } func argInts() (s []int) { diff --git a/challenge-160/pokgopun/perl/ch-1.pl b/challenge-160/pokgopun/perl/ch-1.pl index cf260e0431..3aba7ed370 100644 --- a/challenge-160/pokgopun/perl/ch-1.pl +++ b/challenge-160/pokgopun/perl/ch-1.pl @@ -7,18 +7,17 @@ my @n = @ARGV && join("",@ARGV) =~ /^\d+$/ ? @ARGV : (5,7,6); foreach my $n (@n) { printf "Input: \$n = %d\n", $n; - my $str = $m[$n-1]; + my $w = $m[$n-1]; + my $str = $w; { - my $prev = $1 if $str =~ /(\S+)$/; - if ($prev eq "four") { + if ($w eq "four") { $str .= " is magic"; last; } else { - my $next = $m[length($prev)-1]; - $str .= " is $next, $next"; + $w = $m[length($w)-1]; + $str .= " is $w, $w"; } redo; } - $str =~ s/^(.)/\U$1/; - printf "Output: %s.\n\n", $str; + printf "Output: %s.\n\n", ucfirst($str); } -- cgit