64 lines
1.5 KiB
Python
64 lines
1.5 KiB
Python
|
from itertools import combinations
|
||
|
|
||
|
from common import replaceContent
|
||
|
|
||
|
class Set:
|
||
|
def __init__(self, elements):
|
||
|
self.elements = set(elements)
|
||
|
|
||
|
def __str__(self):
|
||
|
if len(self) == 0: return "\\emptyset"
|
||
|
return f"\\{{ {', '.join(sorted(self.elements))} \\}}"
|
||
|
|
||
|
def __iter__(self):
|
||
|
return list(sorted(self.elements))
|
||
|
|
||
|
def __len__(self):
|
||
|
return len(self.elements)
|
||
|
|
||
|
def __gt__(self, value):
|
||
|
if len(self) != len(value):
|
||
|
return len(self) > len(value)
|
||
|
return self.elements > value.elements
|
||
|
|
||
|
def __ge__(self, value):
|
||
|
return self > value
|
||
|
|
||
|
def __lt__(self, value):
|
||
|
return not self > value
|
||
|
|
||
|
def __le__(self, value):
|
||
|
return self < value
|
||
|
|
||
|
@classmethod
|
||
|
def fromString(cls, string):
|
||
|
return cls(string.split(' '))
|
||
|
|
||
|
def cardinality(self):
|
||
|
return len(self)
|
||
|
|
||
|
def powerset(self):
|
||
|
powerset = []
|
||
|
for i in range(len(self) + 1):
|
||
|
for subset in combinations(self.elements, i):
|
||
|
powerset.append(Set(list(subset)))
|
||
|
return Set(powerset)
|
||
|
|
||
|
def to_vertical_latex(self):
|
||
|
column = []
|
||
|
for e in sorted(self.elements):
|
||
|
column.append(str(e) + ' \\\\')
|
||
|
return '\\{\n' + '\n'.join(column) + '\n\\}'
|
||
|
|
||
|
|
||
|
def processFileContent(raw):
|
||
|
s = Set.fromString(raw)
|
||
|
content = s.powerset().to_vertical_latex()
|
||
|
|
||
|
return replaceContent(content, 'Powerset')
|
||
|
|
||
|
#TODO: make process input func
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
print(Set(['a', 'b', 'c']).powerset().to_vertical_latex())
|
||
|
# print(a for a in Set(['A', 'B', 'C']).powerset())
|