From f4861222f6b825a4a7cccf615de81cba12976028 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 11 Feb 2021 00:45:19 +0000 Subject: Add Python solution to challenge 099 --- challenge-099/paulo-custodio/python/ch-1.py | 53 +++++++++++++++++++++++++++++ challenge-099/paulo-custodio/python/ch-2.py | 41 ++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 challenge-099/paulo-custodio/python/ch-1.py create mode 100644 challenge-099/paulo-custodio/python/ch-2.py (limited to 'challenge-099/paulo-custodio/python') diff --git a/challenge-099/paulo-custodio/python/ch-1.py b/challenge-099/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..60295131f4 --- /dev/null +++ b/challenge-099/paulo-custodio/python/ch-1.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +# 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 + +import sys + +def match(s, p): + while True: + if s=="" and p=="": + return True + elif s=="" or p=="": + return False + elif p[0]=="?": + s=s[1:] + p=p[1:] + elif p[0]=="*": + p=p[1:] + for i in range(0, len(s)): + if match(s[i:], p): + return True + return False + elif s[0]!=p[0]: + return False + else: + s=s[1:] + p=p[1:] + +if match(sys.argv[1], sys.argv[2]): + print(1) +else: + print(0) diff --git a/challenge-099/paulo-custodio/python/ch-2.py b/challenge-099/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..e065b17674 --- /dev/null +++ b/challenge-099/paulo-custodio/python/ch-2.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +# TASK #2 > Unique Sub-sequence +# Submitted by : Mohammad S Anwar +# You are given two strings $S and $T. +# +# Write a script to find out count of different unique sub-sequences 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] + +import sys + +def count_subsequences(s, t): + while True: + if t=="": + return 1 + elif s=="": + return 0 + elif s[0]==t[0]: + return (count_subsequences(s[1:], t[1:]) + + count_subsequences(s[1:], t)) + else: + s=s[1:] + +print(count_subsequences(sys.argv[1], sys.argv[2])) -- cgit From ecebe513c84edd4e5b259a8ef245232f771ae77d Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 18 Feb 2021 21:04:48 +0000 Subject: Fix comments --- challenge-099/paulo-custodio/python/ch-1.py | 2 ++ challenge-099/paulo-custodio/python/ch-2.py | 2 ++ 2 files changed, 4 insertions(+) (limited to 'challenge-099/paulo-custodio/python') diff --git a/challenge-099/paulo-custodio/python/ch-1.py b/challenge-099/paulo-custodio/python/ch-1.py index 60295131f4..930c9dc328 100644 --- a/challenge-099/paulo-custodio/python/ch-1.py +++ b/challenge-099/paulo-custodio/python/ch-1.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +# Challenge 099 +# # TASK #1 > Pattern Match # Submitted by: Mohammad S Anwar # You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/python/ch-2.py b/challenge-099/paulo-custodio/python/ch-2.py index e065b17674..e82e57f635 100644 --- a/challenge-099/paulo-custodio/python/ch-2.py +++ b/challenge-099/paulo-custodio/python/ch-2.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +# Challenge 099 +# # TASK #2 > Unique Sub-sequence # Submitted by : Mohammad S Anwar # You are given two strings $S and $T. -- cgit From 86f1e20620f3acc15e30b400119b9c9b8c161f97 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 19 Feb 2021 20:09:30 +0000 Subject: Add missing comment line --- challenge-099/paulo-custodio/python/ch-1.py | 2 -- challenge-099/paulo-custodio/python/ch-2.py | 2 -- 2 files changed, 4 deletions(-) (limited to 'challenge-099/paulo-custodio/python') diff --git a/challenge-099/paulo-custodio/python/ch-1.py b/challenge-099/paulo-custodio/python/ch-1.py index 930c9dc328..60295131f4 100644 --- a/challenge-099/paulo-custodio/python/ch-1.py +++ b/challenge-099/paulo-custodio/python/ch-1.py @@ -1,7 +1,5 @@ #!/usr/bin/env python -# Challenge 099 -# # TASK #1 > Pattern Match # Submitted by: Mohammad S Anwar # You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/python/ch-2.py b/challenge-099/paulo-custodio/python/ch-2.py index e82e57f635..e065b17674 100644 --- a/challenge-099/paulo-custodio/python/ch-2.py +++ b/challenge-099/paulo-custodio/python/ch-2.py @@ -1,7 +1,5 @@ #!/usr/bin/env python -# Challenge 099 -# # TASK #2 > Unique Sub-sequence # Submitted by : Mohammad S Anwar # You are given two strings $S and $T. -- cgit From 6ba83654820bad00ba3c679bbe18764269dabe0f Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 5 Mar 2021 20:22:55 +0000 Subject: Update comments --- challenge-099/paulo-custodio/python/ch-1.py | 2 ++ challenge-099/paulo-custodio/python/ch-2.py | 2 ++ 2 files changed, 4 insertions(+) (limited to 'challenge-099/paulo-custodio/python') diff --git a/challenge-099/paulo-custodio/python/ch-1.py b/challenge-099/paulo-custodio/python/ch-1.py index 60295131f4..4516a3254f 100644 --- a/challenge-099/paulo-custodio/python/ch-1.py +++ b/challenge-099/paulo-custodio/python/ch-1.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +# Challenge 099 +# # TASK #1 > Pattern Match # Submitted by: Mohammad S Anwar # You are given a string $S and a pattern $P. diff --git a/challenge-099/paulo-custodio/python/ch-2.py b/challenge-099/paulo-custodio/python/ch-2.py index e065b17674..e82e57f635 100644 --- a/challenge-099/paulo-custodio/python/ch-2.py +++ b/challenge-099/paulo-custodio/python/ch-2.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +# Challenge 099 +# # TASK #2 > Unique Sub-sequence # Submitted by : Mohammad S Anwar # You are given two strings $S and $T. -- cgit