Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 8e331fa

Browse files
define __len__() atrribute
1 parent cdb4c0b commit 8e331fa

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/sage/logic/boolformula.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,3 +1424,48 @@ def get_next_op(self, str):
14241424
while i < len(str) - 1 and str[i] != '&' and str[i] != '|':
14251425
i += 1
14261426
return str[i]
1427+
1428+
def __len__(self):
1429+
r"""
1430+
Return the length of a Boolean formula.
1431+
1432+
OUTPUT:
1433+
1434+
The length of the Boolean formula. This is the number of operators plus
1435+
the number of variables (counting multiplicity). Parentheses are ignored.
1436+
1437+
EXAMPLES::
1438+
1439+
sage: import sage.logic.propcalc as propcalc
1440+
sage: s = propcalc.formula("a")
1441+
sage: len(s)
1442+
1
1443+
sage: s = propcalc.formula("(a)")
1444+
sage: len(s)
1445+
1
1446+
sage: s = propcalc.formula("~a")
1447+
sage: len(s)
1448+
2
1449+
sage: s = propcalc.formula("a -> b")
1450+
sage: len(s)
1451+
3
1452+
sage: s = propcalc.formula("alpha -> beta")
1453+
sage: len(s)
1454+
3
1455+
sage: s = propcalc.formula("a -> a")
1456+
sage: len(s)
1457+
3
1458+
sage: s = propcalc.formula("~(a -> b)")
1459+
sage: len(s)
1460+
4
1461+
sage: s = propcalc.formula("((a&b)|(a&c))->~d")
1462+
sage: len(s)
1463+
10
1464+
1465+
TESTS::
1466+
1467+
sage: s = propcalc.formula("(((alpha) -> ((beta))))")
1468+
sage: len(s)
1469+
3
1470+
"""
1471+
return len(flatten(self.full_tree()))

0 commit comments

Comments
 (0)