diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-14 16:32:59 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-14 16:32:59 +0000 |
| commit | ef14c20e2e2d61abe4cc73e5389733122df02d42 (patch) | |
| tree | d4346068d07aac04bae6094c595e1350fa6fe2c2 /challenge-099 | |
| parent | 40abdaf0147a405f36b5d43d7919eda15e131a3d (diff) | |
| download | perlweeklychallenge-club-ef14c20e2e2d61abe4cc73e5389733122df02d42.tar.gz perlweeklychallenge-club-ef14c20e2e2d61abe4cc73e5389733122df02d42.tar.bz2 perlweeklychallenge-club-ef14c20e2e2d61abe4cc73e5389733122df02d42.zip | |
Add Awk solution to challenge 004
Fix Awk syntax on previouws submissions - semicolon is needed at the end of statements
Diffstat (limited to 'challenge-099')
| -rw-r--r-- | challenge-099/paulo-custodio/awk/ch-1.awk | 26 | ||||
| -rw-r--r-- | challenge-099/paulo-custodio/awk/ch-2.awk | 14 |
2 files changed, 20 insertions, 20 deletions
diff --git a/challenge-099/paulo-custodio/awk/ch-1.awk b/challenge-099/paulo-custodio/awk/ch-1.awk index e69e053735..e090e3fe64 100644 --- a/challenge-099/paulo-custodio/awk/ch-1.awk +++ b/challenge-099/paulo-custodio/awk/ch-1.awk @@ -1,6 +1,6 @@ #!/usr/bin/gawk -# TASK #1 › Pattern Match +# TASK #1 > Pattern Match # Submitted by: Mohammad S Anwar # You are given a string $S and a pattern $P. # @@ -27,32 +27,32 @@ function match_pattern(s, p) { while (1) { if (s=="" && p=="") # string and pattern finished - return 1 + return 1; else if (s=="" || p=="") # either string or pattern finished - return 0 + return 0; else if (p ~ /^\?/) { # match any character - s = substr(s, 2) - p = substr(p, 2) + s = substr(s, 2); + p = substr(p, 2); } else if (p ~ /^\*/) { # match any sub-sequence - p = substr(p, 2) + p = substr(p, 2); for (i = 1; i <= length(s); i++) { if (match_pattern(substr(s, i), p)) - return 1 + return 1; } - return 0 + return 0; } else if (substr(p,1,1) != substr(s,1,1)) { # chars different - return 0 + return 0; } else { # search next char - s = substr(s, 2) - p = substr(p, 2) + s = substr(s, 2); + p = substr(p, 2); } } } BEGIN { - print match_pattern(ARGV[1], ARGV[2]) - exit 0 + print match_pattern(ARGV[1], ARGV[2]); + exit 0; } diff --git a/challenge-099/paulo-custodio/awk/ch-2.awk b/challenge-099/paulo-custodio/awk/ch-2.awk index 5c7a28d2fb..8fd50fa366 100644 --- a/challenge-099/paulo-custodio/awk/ch-2.awk +++ b/challenge-099/paulo-custodio/awk/ch-2.awk @@ -1,6 +1,6 @@ #!/usr/bin/gawk -# TASK #2 › Unique Subsequence +# TASK #2 > Unique Subsequence # Submitted by: Mohammad S Anwar # You are given two strings $S and $T. # @@ -27,24 +27,24 @@ function count_subsequences(s, t) { while (1) { if (t=="") { # t is empty, matched - return 1 + return 1; } else if (s=="") { # s is empty, did not match - return 0 + return 0; } else if (substr(s,1,1) == substr(t,1,1)) { # same char, # check two paths matching # and not matching return count_subsequences(substr(s,2), substr(t,2)) \ - + count_subsequences(substr(s,2), t) + + count_subsequences(substr(s,2), t); } else { # different char, keep pattern - s = substr(s,2) + s = substr(s,2); } } } BEGIN { - print count_subsequences(ARGV[1], ARGV[2]) - exit 0 + print count_subsequences(ARGV[1], ARGV[2]); + exit 0; } |
