Essay Example on Import Sys Reduces the operators if and is present inside

Subcategory:

Category:

Words:

276

Pages:

1

Views:

13

Import Sys Reduces the operators if and is present inside and or or inside or Example and A and B C should be and A B C def reduceOperators formula if isinstance formula str return formula operator formula 0 literals propositions for index item in enumerate formula if index 0 if isinstance item str literals append item elif isinstance item list propositions append reduceOperators item newFormula literals for item in propositions if isinstance item list and item 0 operator for i clause in enumerate item if i 0 newFormula append clause else newFormula append item newFormula insert 0 operator return newFormula Removes duplicate elements from and and or Example and A A should be A def removeDuplicates formula if isinstance formula str or isinstance formula list and formula 0 not and isinstance formula 1 str return formula for i checkItem in enumerate formula if i 



0 for j item in reversed list enumerate formula if j i if isinstance item list newItem removeDuplicates item formula insert j newItem formula remove item if checkItem item formula remove item if isinstance formula list and formula 0 not and len formula 3 return formula 1 return formula Sorts the literals and lists in the formula will be used for removing duplicate items in list The literals are in the beginning followed by lists And the and list is present in the end def sort formula if isinstance formula str return formula operator formula 0 if operator return formula literals propositions for index item in enumerate formula if index 0 if isinstance item str literals append item elif isinstance item list propositions append sort item if len literals 0 literals sort if len propositions 0 propositions sorted propositions key lambda proposition proposition 0 reverse True newFormula literals propositions newFormula insert 0 operator return newFormula Converts to CNF by taking different cases separately def convert formula A B A B if formula 0 return convert or convert not convert formula 1 convert formula 2 A B A B A B elif formula 0 return convert and convert or convert not formula 



1 formula 2 convert or formula 1 convert not formula 2 elif formula 0 not p if isinstance formula 1 str return formula p p elif isinstance formula 1 list and formula 1 0 not return convert formula 1 1 A B C A B C elif isinstance formula 1 list and formula 1 0 and disjuncts for index item in enumerate formula 1 if index 0 disjuncts append convert not item disjuncts insert 0 or return convert disjuncts A B C A B C elif isinstance formula 1 list and formula 1 0 or conjuncts for index item in enumerate formula 1 if index 0 conjuncts append convert not item conjuncts insert 0 and return convert conjuncts A B A B elif isinstance formula 1 list and formula 1 0 return convert and convert formula 1 1 not convert formula 1 2 elif isinstance formula 1 list and formula 1 0 return convert not convert formula 1 elif formula 0 or A A A formula sort formula formula

removeDuplicates formula Handling the case or A or B C formula reduceOperators formula The order will be messed up when the redundant operators are removed Handling the case when the formula is reduced For instance A or A is reduced to A formula sort formula if len formula 1 return formula if isinstance formula 1 list and formula 1 0 and A B C D A B A C A D conjuncts for i item in enumerate formula 1 if i 0 conjuncts append or formula 2 item conjuncts insert 0 and If only 2 items then remove them and also remove OR if len formula 4 return convert conjuncts else formula remove formula 1 formula remove formula 1 formula append conjuncts return convert formula Case A OR B elif isinstance formula 1 str and isinstance formula 2 str or isinstance formula 1 str and isinstance formula 2 list and formula 2 0 not and isinstance formula 2 1 str or isinstance formula 2 str and isinstance formula 1 list and formula 1 0 not and isinstance formula 1 1 str formula append or formula 1 formula 2 formula remove formula 1 formula remove formula 



1 formula reduceOperators formula formula sort formula return formula Case A OR B elif isinstance formula 1 list and formula 1 0 not and isinstance formula 1 1 str and isinstance formula 2 list and formula 2 0 not and isinstance formula 2 1 str return formula For any other operator compute the inner operator after or else disjuncts for i item in enumerate formula if i 0 disjuncts append convert item disjuncts insert 0 or return convert disjuncts elif formula 0 and Handling the case and A or C D or D C formula sort formula formula removeDuplicates formula Handling the case and A and B C formula reduceOperators formula The order will be messed up when the redundant operators are removed formula sort formula if len formula 1 return formula disjuncts for i item in enumerate formula if i 0 disjuncts append convert item disjuncts insert 0 and disjuncts reduceOperators disjuncts return disjuncts Read the input file inputFile open sys argv 2 numSentences 1 sentences for line in inputFile sentences append eval line strip outputFile open sentences_w txt w For each sentence convert it to CNF for sentence in sentences print sentence formula convert sentence formula sort formula formula removeDuplicates formula formula reduceOperators formula formula sort formula imprime a cnf print str formula outputFile write str formula n inputFile close


Write and Proofread Your Essay
With Noplag Writing Assistance App

Plagiarism Checker

Spell Checker

Virtual Writing Assistant

Grammar Checker

Citation Assistance

Smart Online Editor

Start Writing Now

Start Writing like a PRO

Start