diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-04-16 19:32:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-16 19:32:27 +0100 |
| commit | 76bf7437ed7bef160a6f4900a2828a502f45cc8e (patch) | |
| tree | 510c5e83f4eaee6f9877abfb9f8b99a4961acf03 | |
| parent | c12e77f777195bae49dc884d3f53b3c6d585407d (diff) | |
| parent | 853d1f80f9bd379710e909c89274ef5abfd788e7 (diff) | |
| download | perlweeklychallenge-club-76bf7437ed7bef160a6f4900a2828a502f45cc8e.tar.gz perlweeklychallenge-club-76bf7437ed7bef160a6f4900a2828a502f45cc8e.tar.bz2 perlweeklychallenge-club-76bf7437ed7bef160a6f4900a2828a502f45cc8e.zip | |
Merge pull request #5946 from pokgopun/pwc160
pwc160_task#1 to avoid unnecessary regex operation
| -rw-r--r-- | challenge-160/pokgopun/go/ch-1.go | 17 | ||||
| -rw-r--r-- | 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); } |
