-
Notifications
You must be signed in to change notification settings - Fork 6
Edit functions to capture numbers and fractions from a text file #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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/:\\)-}])" | ||
| 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() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
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