From 676599243e41c9115d030d3fda2cfd2a098a4d55 Mon Sep 17 00:00:00 2001 From: Abdulmajid_Babale Date: Thu, 20 May 2021 11:31:59 +0100 Subject: [PATCH 1/3] Edit functions to capture numbers and fractions from a text file --- number_search.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/number_search.py b/number_search.py index e456f5d..24246d3 100644 --- a/number_search.py +++ b/number_search.py @@ -2,26 +2,46 @@ # in article.txt # use regex to match numbers and fractions import re +frac_pattern = r'(-?\d+\\[A-Za-z]+{\d+}{\d+} |-?\\[A-Za-z]+{\d+}{\d+})' +number_pattern = r"([^a-zA-Z{./\\}]\d+ |\d+\.\d+|[:]\d+|\d+[:])" + def count_numbers(text): """ counts whole number and decimals i.e -ve and +ve whole numbers decimals TODO update doc strings """ - pass #TODO update the fucntion to pass test - + count = -1 + with open(text, "r") as f: + article = f.read() + pattern = re.compile(number_pattern) + match = pattern.finditer(article) + for x in match: + print(x.group(0)) + count += 1 + return count -def count_fraction(): +def count_fraction(text): """ counts fractional numbers i.e both negative an positive TODO update doc strings """ - pass #TODO update the fucntion to pass test + count = -1 + + with open(text, "r") as f: + article = f.read() + pattern = re.compile(frac_pattern) + match = pattern.finditer(article) + for x in match: + print(x.group(0)) + count += 1 + return count -## Use this for your debugging purposes (all print statements should go here) + +# Use this for your debugging purposes (all print statements should go here) if __name__ == "__main__": - + # Replace pass with your debugging code if any pass From 6a2eea3ac89a8aa8ebb992e7fbce8bd5a03d769e Mon Sep 17 00:00:00 2001 From: Abdulmajid_Babale Date: Sat, 22 May 2021 08:31:46 +0100 Subject: [PATCH 2/3] Edit pattern to capture numbers and fractions from a text file --- number_search.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/number_search.py b/number_search.py index 24246d3..8176bd4 100644 --- a/number_search.py +++ b/number_search.py @@ -2,8 +2,10 @@ # in article.txt # use regex to match numbers and fractions import re -frac_pattern = r'(-?\d+\\[A-Za-z]+{\d+}{\d+} |-?\\[A-Za-z]+{\d+}{\d+})' -number_pattern = r"([^a-zA-Z{./\\}]\d+ |\d+\.\d+|[:]\d+|\d+[:])" +frac_pattern = r'(-?\d+\\tinyfrac{\d+}{\d+}[^A-Za-z]|-?\\frac{\d+}{\d+}[^A-Za-z])' +number_pattern = r'([^a-zA-Z{./\\}]\d+ |\d+\.\d+| \d+[^/:\\)-}])' +numbers = [] +fractions = [] def count_numbers(text): @@ -11,15 +13,15 @@ def count_numbers(text): counts whole number and decimals i.e -ve and +ve whole numbers decimals TODO update doc strings """ - count = -1 + with open(text, "r") as f: article = f.read() pattern = re.compile(number_pattern) match = pattern.finditer(article) for x in match: print(x.group(0)) - count += 1 - return count + numbers.append(x.group(0)) + return len(numbers) def count_fraction(text): @@ -27,7 +29,6 @@ def count_fraction(text): counts fractional numbers i.e both negative an positive TODO update doc strings """ - count = -1 with open(text, "r") as f: article = f.read() @@ -36,8 +37,8 @@ def count_fraction(text): match = pattern.finditer(article) for x in match: print(x.group(0)) - count += 1 - return count + fractions.append(x.group(0)) + return len(fractions) # Use this for your debugging purposes (all print statements should go here) From 4c33f5e3485b2742d829e79cd29aff980f1aaa90 Mon Sep 17 00:00:00 2001 From: Abdulmajid_Babale Date: Sat, 29 May 2021 06:22:07 +0100 Subject: [PATCH 3/3] Remove new line from list of captured numbers --- number_search.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/number_search.py b/number_search.py index 8176bd4..4d2f6f3 100644 --- a/number_search.py +++ b/number_search.py @@ -3,7 +3,7 @@ # use regex to match numbers and fractions import re frac_pattern = r'(-?\d+\\tinyfrac{\d+}{\d+}[^A-Za-z]|-?\\frac{\d+}{\d+}[^A-Za-z])' -number_pattern = r'([^a-zA-Z{./\\}]\d+ |\d+\.\d+| \d+[^/:\\)-}])' +number_pattern = r"([^\na-zA-Z{./\\}]\d+ |\d+\.\d+|\d+[^\n/:\\)-}])" numbers = [] fractions = [] @@ -21,6 +21,7 @@ def count_numbers(text): for x in match: print(x.group(0)) numbers.append(x.group(0)) + print(numbers) return len(numbers) @@ -38,6 +39,7 @@ def count_fraction(text): for x in match: print(x.group(0)) fractions.append(x.group(0)) + print(fractions) return len(fractions)