Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
trs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
trs
Commits
98445a0e
Commit
98445a0e
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added helper function to check if a node matches a fraction format.
parent
7b359faf
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/rules/utils.py
+34
-0
34 additions, 0 deletions
src/rules/utils.py
tests/test_rules_utils.py
+10
-1
10 additions, 1 deletion
tests/test_rules_utils.py
with
44 additions
and
1 deletion
src/rules/utils.py
+
34
−
0
View file @
98445a0e
from
..node
import
ExpressionLeaf
as
L
,
OP_MUL
,
OP_DIV
def
gcd
(
a
,
b
):
"""
Return greatest common divisor using Euclid
'
s Algorithm.
...
...
@@ -20,3 +23,34 @@ def least_common_multiple(*args):
Return lcm of args.
"""
return
reduce
(
lcm
,
args
)
def
is_fraction
(
node
,
nominator
,
denominator
):
"""
Check if a node represents the fraction of a given nominator and
denominator.
>>>
from
..node
import
ExpressionLeaf
as
L
>>>
l1
,
l2
,
a
=
L
(
'
a
'
),
L
(
1
),
L
(
2
)
>>>
is_fraction
(
a
/
l2
,
a
,
2
)
True
>>>
is_fraction
(
l1
/
l2
*
a
,
a
,
2
)
True
>>>
is_fraction
(
l2
/
l1
*
a
,
a
,
2
)
False
"""
if
node
.
is_op
(
OP_DIV
):
nom
,
denom
=
node
return
nom
==
nominator
and
denom
==
denominator
if
node
.
is_op
(
OP_MUL
):
# 1 / denominator * nominator
# nominator * 1 / denominator
left
,
right
=
node
fraction
=
L
(
1
)
/
denominator
return
(
left
==
nominator
and
right
==
fraction
)
\
or
(
right
==
nominator
and
left
==
fraction
)
return
False
This diff is collapsed.
Click to expand it.
tests/test_rules_utils.py
+
10
−
1
View file @
98445a0e
import
unittest
from
src.rules.utils
import
least_common_multiple
from
src.rules.utils
import
least_common_multiple
,
is_fraction
from
tests.rulestestcase
import
tree
class
TestRulesUtils
(
unittest
.
TestCase
):
...
...
@@ -9,3 +10,11 @@ class TestRulesUtils(unittest.TestCase):
self
.
assertEqual
(
least_common_multiple
(
5
,
6
),
30
)
self
.
assertEqual
(
least_common_multiple
(
5
,
6
,
15
),
30
)
self
.
assertEqual
(
least_common_multiple
(
2
,
4
),
4
)
def
test_is_fraction
(
self
):
l1
,
a
=
tree
(
'
1, a
'
)
self
.
assertTrue
(
is_fraction
(
a
/
2
,
a
,
2
))
self
.
assertTrue
(
is_fraction
(
l1
/
2
*
a
,
a
,
2
))
self
.
assertTrue
(
is_fraction
(
a
*
(
l1
/
2
),
a
,
2
))
self
.
assertFalse
(
is_fraction
(
l1
/
3
*
a
,
a
,
2
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment