韩唐伯石 2022-02-13 03:51 采纳率: 55%
浏览 62
已结题

Jupyter一个文件里用python怎么调用类方法

想调用下面那个SFA_Ops类里的intersect方法去实现ex_1_4a,但是直接写intersect方法没反应,怎么调用啊?

at_least_three_a = { 
    'states':{'q1','q2','q3','q4','q5'},
    'alphabet':{'a','b'},
    'transitions':{
        ('q1','a'):'q2', ('q1','b'):'q3',
        ('q2','a'):'q4', ('q2','b'):'q2',
        ('q3','a'):'q2', ('q3','b'):'q3',
        ('q4','a'):'q5', ('q4','b'):'q4',
        ('q5','a'):'q5', ('q5','b'):'q5',
    },
    'start':'q1',
    'accept': {'q5'} 
 
    # write code here
    
}

at_least_two_b = {
    'states':{'q1','q2','q3','q4'},
    'alphabet':{'a','b'},
    'transitions':{
        ('q1','a'):'q2', ('q1','b'):'q3',
        ('q2','a'):'q4', ('q2','b'):'q2',
        ('q3','a'):'q2', ('q3','b'):'q3',
        ('q4','a'):'q4', ('q4','b'):'q4',
    },
    'start':'q1',
    'accept': {'q4'} 
 
    # write code here
}

ex_1_4a = intersect(at_least_three_a,at_least_two_b)
ex_1_4a_test = (['aaabb','ababa'],['','aaa','bb','aabb','aaab'])


import itertools
class SFA_Ops:
    
    @staticmethod
    def cross_states(set1, set2):
        cp = set()
        
        # code to make the cartesian product of set1 by set2
        for i in itertools.product(set1, set2):
            cp.add(i)          
            
        return cp
    
    @staticmethod
    def cross_deltas(state, alphabet, d1, d2):
        d = {} # an empty dictionary
        
        # code to make the cartesian product transition function from
        # the transition functions d1 and d2
        for i in alphabet:
            for j in state:
                d[(j,i)] = (d1[(j[0],i)],d2[(j[1],i)])
        
        return d      
       
    @staticmethod
    def cross_machine(fa1, fa2):
        assert fa1['alphabet'] == fa2['alphabet']
        """
        returns a new dictionary; however the elements are not copied
        """
        states = SFA_Ops.cross_states(
                    fa1['states'],
                    fa2['states'])
        d = SFA_Ops.cross_deltas(states,
                    fa1['alphabet'],
                    fa1['transitions'],
                    fa2['transitions'])
                
        fa = {
            'states': states,
            'alphabet': fa1['alphabet'],
            'transitions':d,
            'start': (fa1['start'],fa2['start']),
            'accept':set()
        }
        return fa
    
    @staticmethod
    def intersect(fa_o,fa_i):
        desc = SFA_Ops.cross_machine(fa_o,fa_i)
        accept = set()
        
        # create the set of accept states for the intersection of 
        for i in fa_o['accept']:
            for j in fa_u['accept']:
                accept.add(i,j)
        # languages accepted by fa_o and fa_i
        
        
        desc['accept'] = accept
        return desc
  
    @staticmethod
    def union(fa_o,fa_u):
        desc = SFA_Ops.cross_machine(fa_o,fa_u)
        accept = set()
        
        # create the set of accept states for the union of 
        # languages accepted by fa_o and fa_i
        for i in fa_o['accept']:
            for j in fa_u['states']:
                accept.add((i,j))
        for i in fa_u['accept']:
            for j in fa_o['states']:
                accept.add((j,i))
        
        desc['accept'] = accept
        return desc
    
    @staticmethod
    def complement(fa_c):
        desc = fa_c.copy() # a shallow copy, to avoid aliasing two machines
        accept = set()
        
        # create the set of accept states for the complement of 
        # language accepted by fa_c
        for i in fa_c['states']:
            if not (i in fa_c['accept']):
                accept.add(i)
            
        desc['accept'] = accept
        return desc

  • 写回答

1条回答 默认 最新

  • _GX_ 2022-02-13 04:08
    关注
    ex_1_4a = SFA_Ops.intersect(at_least_three_a,at_least_two_b)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 2月21日
  • 已采纳回答 2月13日
  • 创建了问题 2月13日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)