Python Forum
propositional logic formula converter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
propositional logic formula converter
#8
Your algorithm is ineffictient because at line 128, 129 you generate too many subsets and then you eliminate subsets having a non empty intersection. I think you can simplify this a lot
from itertools import chain, combinations, filterfalse

def almost_powerset(iterable):
    "almost_powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)))

def con_ant(n):
    ind = list(range(1, n+1))
    for s in almost_powerset(ind):
        b = set(s)
        t = [x for x in filterfalse(b.__contains__, ind)]
        yield s, t
        
def build_lst_nums(num):
    lst = ["1." + str(x) for x in range(1, num + 1)]
    u = lst[:1]
    for s, t in con_ant(num-1):
        a = u + [lst[x] for x in s]
        b = [lst[x] for x in t]
        yield a, b
    yield lst[1:], u

        
if __name__ == '__main__':
    for s, t in build_lst_nums(4):
        print(s, t)
Output:
['1.1'] ['1.2', '1.3', '1.4'] ['1.1', '1.2'] ['1.3', '1.4'] ['1.1', '1.3'] ['1.2', '1.4'] ['1.1', '1.4'] ['1.2', '1.3'] ['1.1', '1.2', '1.3'] ['1.4'] ['1.1', '1.2', '1.4'] ['1.3'] ['1.1', '1.3', '1.4'] ['1.2'] ['1.2', '1.3', '1.4'] ['1.1']
Reply


Messages In This Thread
RE: propositional logic formula converter - by Gribouillis - Dec-03-2018, 04:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Decimal to binary weighted number converter hobbyprogrammer 1 2,399 Nov-28-2018, 12:38 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020