前言:
一年前在知乎上看到一个回答,答主说自己学了两天Python,用十几个小时做完了全部的编译原理课程作业,当时吓傻了我,现在看来,虽然两天学会比不上,但Python做课程作业的速度简直是快,课程作业1里我还傻傻的用list的extend和append,加上set函数,到第二次作业里我才发现,
没有什么结构体是一个list不能解决的,如果有,那就再套一个list
课程作业题:
消除无用产生式
# -*- coding: utf-8 -*- class Solution: def __init__(self,vn,vt,p1,p2,s): self.vn = vn self.vt = vt self.p1 = p1 self.p2 = p2 self.s = s def step1(self): #算法2.1 set_vn = set(self.vn)#集合vn vt set_vt = set(self.vt) #1 self.vn_1 = [] self.p1_1 = [] self.p2_1 = [] #2 for i in range(len(self.p2)): if set_vt >= set(self.p2[i]): self.vn_1.extend(list(self.p1[i])) self.vn_1 = list((set(self.vn_1)))#去重 #print self.vn_1 #3#4 while 1: len_vn_1 = len(set(self.vn_1)) for i in range(len(self.p2)): if (set(self.vn_1)|set_vt)>=(set(self.p2[i])): self.vn_1.extend(self.p1[i]) if len_vn_1 == len(set(self.vn_1)): break self.vn_1 = list((set(self.vn_1)))#去重 #print self.vn_1 #5 for i in range(len(self.p2)): if (set(self.vn_1)|set_vt)>=(set(self.p2[i])|set(self.p1[i])): self.p1_1.append(self.p1[i]) self.p2_1.append(self.p2[i]) return self.vn_1,self.vt,self.p1_1,self.p2_1,self.s def step2(self): #算法2.2 set_vn = set(self.vn)#集合vn vt set_vt = set(self.vt) #1 self.vn_2 = [] self.vt_2 = [] self.p1_2 = [] self.p2_2 = [] #2 self.vn_2 = [‘S‘] #3#4 while 1: len_vn_2 = len(set(self.vn_2)) len_vt_2 = len(set(self.vt_2)) for i in range(len(self.p1)): if set(self.p1[i])<=set(self.vn_2): set_action_2 = set(self.p2[i]) for x in set_action_2: if x in self.vn: self.vn_2.append(x) else: self.vt_2.append(x) if (len_vn_2 == len(set(self.vn_2)))and(len_vt_2 == len(set(self.vt_2))): break self.vn_2 = list((set(self.vn_2)))#去重 self.vt_2 = list((set(self.vt_2)))#去重 #5 for i in range(len(self.p1)): if (set(self.p1[i])|set(self.p2[i]))<=(set(self.vn_2)|set(self.vt_2)): self.p1_2.append(self.p1[i]) self.p2_2.append(self.p2[i]) return self.vn_2,self.vt_2,self.p1_2,self.p2_2,self.s class io: def __init__(self,vn,vt,p1,p2,s): self.vn = vn self.vt = vt self.p1 = p1 self.p2 = p2 self.s = s def i(self): vn = [] vt = [] vn = raw_input("please input Vn: ")#读入Vn vt = raw_input("please input Vt: ")#读入Vt vn = vn.strip().split(‘ ‘) vt = vt.strip().split(‘ ‘) p1 = [] p2 = [] s = [] while(1): p = raw_input("please input P: (print ‘q‘ to quit)") if p == "q": break lists_p = p.strip().split(‘->‘) p1.append(lists_p[0]) p2.append(lists_p[1]) s = raw_input("please input S: ")#读入S s = s.strip().split(‘ ‘) print vn,vt,p1,p2,s return vn,vt,p1,p2,s def o(self): print "the result is: " print "Vn: " + " ".join(self.vn) print "Vt: " + " ".join(self.vt) print "P: " for x in range(len(self.p1)): print str(self.p1[x]) + "->" + str(self.p2[x]) print "S: " + " ".join(self.s) print "example: if we input:\nVn: S U V W\nVt: a b c\nP: S->aS\nP: S->W\np: S->U\nP: U->a\nP: V->bV\nP: V->ac\nP: W->aW\nS: S" vn = [‘S‘, ‘U‘, ‘V‘, ‘W‘] vt = [‘a‘, ‘b‘, ‘c‘] p1 = [‘S‘, ‘S‘, ‘S‘, ‘U‘, ‘V‘, ‘V‘, ‘W‘] p2 = [‘aS‘, ‘W‘, ‘U‘, ‘a‘, ‘bV‘, ‘ac‘, ‘aW‘] s = [‘S‘] while 1 : m = Solution(vn,vt,p1,p2,s)#计算 vn,vt,p1,p2,s = m.step1() m = Solution(vn,vt,p1,p2,s) vn,vt,p1,p2,s = m.step2() n = io(vn,vt,p1,p2,s)#输出 n.o() h = io(vn,vt,p1,p2,s) vn,vt,p1,p2,s = h.i()
按着书中的算法2.1和2.2做的
总结起来用到了以下几个函数方法:
1.list:
list L1.extend(list L2) 合并list,
append()
方法向列表的尾部添加一个新的元素。只接受一个参数。
extend()
方法只接受一个列表作为参数,并将该参数的每个元素都添加到原有的列表中。
实际上list L.append(x)就太万能了,能加list或者是元素,一对多的映射也完全可以用[[]]来解决
2.set:
list(set(list L))去重组合拳
3.读文件
list L = string s.strip().split(‘ ‘) 读字符串为链表太方便
4.写文件
string s = " " .join(List L) 写链表为字符串太方便
时间: 2024-10-27 05:22:06