Skip to content

Edit functions to capture numbers and fractions from a text file#4

Open
ab-majeed wants to merge 3 commits into
Mrsatatima:mainfrom
ab-majeed:main
Open

Edit functions to capture numbers and fractions from a text file#4
ab-majeed wants to merge 3 commits into
Mrsatatima:mainfrom
ab-majeed:main

Conversation

@ab-majeed

Copy link
Copy Markdown

The functions worked successfully

Comment thread number_search.py Outdated
# 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+[:])"

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.

this pattern will capture numbers preceded by : i.e 23: which is no a number

Comment thread number_search.py Outdated
# 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+})'

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.

this pattern will capture any text that starts with \ .... try specifying the text frac or tinyfrac

Comment thread number_search.py Outdated
"""
pass #TODO update the fucntion to pass test

count = -1

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.

count should start from 0

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ok sir

@Mrsatatima Mrsatatima left a comment

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.

though it pass all test cases... its capturing invalid numbers and skipping valid numbers.. try converting the article file into list for more efficiency

Comment thread number_search.py
count = -1

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

Comment thread number_search.py
print(x.group(0))
count += 1
return count
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

@Mrsatatima Mrsatatima left a comment

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.

your fraction pattern is effective... but capturing numbers is still the issue

Comment thread number_search.py
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/:\\)-}])"

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

@Mrsatatima Mrsatatima left a comment

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.

great effort.. but pytest is failing.. i recommended the other time.. instead of passing the whole string to the finditer.. convert the large string to list(array) of strings then loop through it and match the pattern using re.search, re.match or findall...
will be posting a better way to do it soon... Good effort, will be closing the pull request.. have already graded you... but if you want to get the full score make the changes stated above and make a new pull request... Congrats

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants