Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions number_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,49 @@
# in article.txt
# 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"([^\na-zA-Z{./\\}]\d+ |\d+\.\d+|\d+[^\n/:\\)-}])"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much better pattern.. but still lacking.. anyways good job

numbers = []
fractions = []


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

with open(text, "r") as f:
article = f.read()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try using split() to convert the string to list for better capture because RE have no stop or continue... but if you split by space u get list of string and can use each string to match pattern and can use $ to make sure the number not followed by invalid character check read me again

pattern = re.compile(number_pattern)
match = pattern.finditer(article)
for x in match:
print(x.group(0))
numbers.append(x.group(0))

@Mrsatatima Mrsatatima May 23, 2021

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print this list you can see it is capturing newline.... which is not correct.. update your pattern

print(numbers)
return len(numbers)


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

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))
fractions.append(x.group(0))
print(fractions)
return len(fractions)

## 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