diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-10 23:35:53 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-11 21:49:58 +0000 |
| commit | 405681844e7b386ae53eb97f88e58f3d0d090a62 (patch) | |
| tree | d32aeec98a2ab9608451d0d893a008864d16e925 | |
| parent | 6d58732cdd23a50220c754bdec2ddf3a505003e0 (diff) | |
| download | perlweeklychallenge-club-405681844e7b386ae53eb97f88e58f3d0d090a62.tar.gz perlweeklychallenge-club-405681844e7b386ae53eb97f88e58f3d0d090a62.tar.bz2 perlweeklychallenge-club-405681844e7b386ae53eb97f88e58f3d0d090a62.zip | |
Add Awk solution to challenge 99
| -rw-r--r-- | challenge-098/paulo-custodio/awk/ch-1.awk | 2 | ||||
| -rw-r--r-- | challenge-099/paulo-custodio/awk/ch-1.awk | 58 | ||||
| -rw-r--r-- | challenge-099/paulo-custodio/awk/ch-2.awk | 50 |
3 files changed, 109 insertions, 1 deletions
diff --git a/challenge-098/paulo-custodio/awk/ch-1.awk b/challenge-098/paulo-custodio/awk/ch-1.awk index 51d8f3a8f6..4711a7babb 100644 --- a/challenge-098/paulo-custodio/awk/ch-1.awk +++ b/challenge-098/paulo-custodio/awk/ch-1.awk @@ -31,7 +31,7 @@ function readN(filename, read_len) { BEGIN { for (i = 1; i < ARGC - 1; i += 2) { - text = readN(ARGV[i], ARGV[i+1]); + text = readN(ARGV[i], ARGV[i+1]) print text } exit 0 diff --git a/challenge-099/paulo-custodio/awk/ch-1.awk b/challenge-099/paulo-custodio/awk/ch-1.awk new file mode 100644 index 0000000000..e69e053735 --- /dev/null +++ b/challenge-099/paulo-custodio/awk/ch-1.awk @@ -0,0 +1,58 @@ +#!/usr/bin/gawk + +# TASK #1 › Pattern Match +# Submitted by: Mohammad S Anwar +# You are given a string $S and a pattern $P. +# +# Write a script to check if given pattern validate the entire string. +# Print 1 if pass otherwise 0. +# +# The patterns can also have the following characters: +# +# ? - Match any single character. +# * - Match any sequence of characters. +# Example 1: +# Input: $S = "abcde" $P = "a*e" +# Output: 1 +# Example 2: +# Input: $S = "abcde" $P = "a*d" +# Output: 0 +# Example 3: +# Input: $S = "abcde" $P = "?b*d" +# Output: 0 +# Example 4: +# Input: $S = "abcde" $P = "a*c?e" +# Output: 1 + +function match_pattern(s, p) { + while (1) { + if (s=="" && p=="") # string and pattern finished + return 1 + else if (s=="" || p=="") # either string or pattern finished + return 0 + else if (p ~ /^\?/) { # match any character + s = substr(s, 2) + p = substr(p, 2) + } + else if (p ~ /^\*/) { # match any sub-sequence + p = substr(p, 2) + for (i = 1; i <= length(s); i++) { + if (match_pattern(substr(s, i), p)) + return 1 + } + return 0 + } + else if (substr(p,1,1) != substr(s,1,1)) { # chars different + return 0 + } + else { # search next char + s = substr(s, 2) + p = substr(p, 2) + } + } +} + +BEGIN { + 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 new file mode 100644 index 0000000000..5c7a28d2fb --- /dev/null +++ b/challenge-099/paulo-custodio/awk/ch-2.awk @@ -0,0 +1,50 @@ +#!/usr/bin/gawk + +# TASK #2 › Unique Subsequence +# Submitted by: Mohammad S Anwar +# You are given two strings $S and $T. +# +# Write a script to find out count of different unique subsequences matching +# $T without changing the position of characters. +# +# Example 1: +# Input: $S = "littleit', $T = 'lit' +# Output: 5 +# +# 1: [lit] tleit +# 2: [li] t [t] leit +# 3: [li] ttlei [t] +# 4: litt [l] e [it] +# 5: [l] ittle [it] +# Example 2: +# Input: $S = "london', $T = 'lon' +# Output: 3 +# +# 1: [lon] don +# 2: [lo] ndo [n] +# 3: [l] ond [on] + +function count_subsequences(s, t) { + while (1) { + if (t=="") { # t is empty, matched + return 1 + } + else if (s=="") { # s is empty, did not match + 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) + } + else { # different char, keep pattern + s = substr(s,2) + } + } +} + +BEGIN { + print count_subsequences(ARGV[1], ARGV[2]) + exit 0 +} |
