Objective:
A year ago, I saw an answer, the Lord said that he had learned two days of Python, with more than 10 hours to do all the compiling principles of the course homework, at that time scared silly me, now it seems, although two days to learn less than, but Python do the course of the speed is almost fast, Course Assignment 1 I was silly with the list of extend and append, plus the SET function, to the second job I found that
No structure is a list can not solve, if there is, then set a list
Course Assignments:
Eliminate the useless production type
#-*-coding:utf-8-*-classSolution:def __init__(self,vn,vt,p1,p2,s): self.vn=vn self.vt=VT Self.p1=P1 SELF.P2=P2 Self.s=sdefStep1 (self):#Algorithm 2.1SET_VN = Set (self.vn)#set VN vtSET_VT =Set (SELF.VT)#1Self.vn_1 =[] self.p1_1=[] Self.p2_1= [] #2 forIinchRange (len (SELF.P2)):ifSET_VT >=set (Self.p2[i]): Self.vn_1.extend (List (self.p1[i)) self.vn_1= List ((set (self.vn_1)))#Go heavy #Print self.vn_1 #3#4 while1: len_vn_1=Len (Set (self.vn_1)) forIinchRange (len (SELF.P2)):if(Set (self.vn_1) |SET_VT) >=(Set (Self.p2[i)): Self.vn_1.extend (Self.p1[i])ifLen_vn_1 = =Len (Set (self.vn_1)): Breakself.vn_1= List ((set (self.vn_1)))#Go heavy #Print self.vn_1 #5 forIinchRange (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]) returnSelf.vn_1,self.vt,self.p1_1,self.p2_1,self.sdefStep2 (self):#Algorithm 2.2SET_VN = Set (self.vn)#set VN vtSET_VT =Set (SELF.VT)#1Self.vn_2 =[] self.vt_2=[] self.p1_2=[] self.p2_2= [] #2Self.vn_2 = ['S'] #3#4 while1: Len_vn_2=Len (Set (self.vn_2)) Len_vt_2=Len (Set (self.vt_2)) forIinchRange (len (SELF.P1)):ifSet (Self.p1[i]) <=Set (self.vn_2): Set_action_2=set (Self.p2[i]) forXinchset_action_2:ifXinchself.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))): Breakself.vn_2= List ((set (self.vn_2)))#Go heavySelf.vt_2 = List ((set (self.vt_2)))#Go heavy #5 forIinchRange (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])returnSelf.vn_2,self.vt_2,self.p1_2,self.p2_2,self.sclassIO:def __init__(self,vn,vt,p1,p2,s): self.vn=vn self.vt=VT Self.p1=P1 SELF.P2=P2 Self.s=sdefI (self): VN=[] VT=[] vn= Raw_input ("Please input Vn:")#Read in vnVT = Raw_input ("Please input Vt:")#Read in VTVN = Vn.strip (). Split (' ') VT= Vt.strip (). Split (' ') P1=[] P2=[] s= [] while(1): P= Raw_input ("Please input P: (print ' Q ' to quit)") ifp = ="Q": Breaklists_p= P.strip (). Split (' -') P1.append (lists_p[0]) p2.append (lists_p[1]) s= Raw_input ("Please input S:")#read in Ss = S.strip (). Split (' ') Printvn,vt,p1,p2,sreturnvn,vt,p1,p2,sdefO (self):Print "The result is:" Print "Vn:"+" ". Join (self.vn)Print "Vt:"+" ". Join (SELF.VT)Print "P:" forXinchRange (len (SELF.P1)):PrintSTR (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'] while1: M= Solution (Vn,vt,p1,p2,s)#CalculationVn,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)#Outputn.o () H=io (vn,vt,p1,p2,s) vn,vt,p1,p2,s= H.I ()
According to the book's Algorithms 2.1 and 2.2.
The following functional methods are used to sum up:
1.list:
List l1.extend (list L2) merges list,
append()Method adds a new element to the tail of the list. Only one parameter is accepted.
extend()Method accepts only one list as an argument and adds each element of the parameter to the existing list.
In fact, the list l.append (x) is too versatile, can add a list or elements, a one-to-many mappings can also be fully used [[]] to solve
2.set:
List (list L) to re-assemble the fist
3. Read the file
List L = String S.strip (). Split (") read string is a linked list too convenient
4. Writing files
string s = "". Join (List L) write linked list as String too handy
Compiling principle course assignment 1 eliminating useless production