# Python code to convert SOP to POS

Write a program in python to convert standard SOP(sum of products) form to standard POS(product of sums) form.

Assumptions:The input SOP expression is standard. The variables in SOP expression are continuous i.e. if expression contains variable A then it will have variables B, C respectively and each Product term contains the alphabets in sorted order i.e. ABC (not like BAC).

Examples:

Input : ABC'+A'BC+ABC+AB'C
Output : (A+B+C).(A+B+C').(A+B'+C).(A'+B+C)

Input : A'B+AB'
Output : (A+B).(A'+B')

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. First of all convert each product term to its equivalent binary form(for ex: if ABC’ then take 1 for uncomplement variable(A, B) and take 0 for complement variable(C) so binary conversion is 110) and then finally equivalent to its decimal form(for ex: 110 = 6) and store in a list.
2. Now for POS form take all those terms which are not present in the list formed in step 1st and then convert each term to binary and hence change to SOP form.For ex: suppose 5 was not in the list then
5 ==> 101 (binary)
Now, replace 1 by complement variables(A, C)
replace 0 by uncomplement variables(B)
and between the variables use ‘+’
101 ==> A’+B+C’
After each individual sum term use ‘.’
For more clarity use brackets between each individual term.
ex: (A’+B+C’).(A+B+C’)

Python Code

 # Python code to convert standard SOP form  # to standard POS form    # function to calculate no. of variables  # used in SOP expression def count_no_alphabets(SOP):     i = 0     no_var = 0        # As expression is standard so total no.     # of alphabets will be equal      # to alphabets before first '+' character     while (SOP[i]!='+'):             # checking if character is alphabet                     if (SOP[i].isalpha()):                    no_var+= 1         i+= 1     return no_var    # function to calculate the min terms in integers def Cal_Min_terms(Min_terms, SOP):     a =""     i = 0     while (i ", POS_expr        # input2     SOP_expr ="A'B + AB'"     Min_terms =[]     no_var = count_no_alphabets(SOP_expr)     Cal_Min_terms(Min_terms, SOP_expr)     POS_expr = Cal_Max_terms(Min_terms, no_var, SOP_expr[0])     print "Standard POS form of", SOP_expr, " ==> ", POS_expr            # input3     SOP_expr ="xyz'+x'y'z'+xy'z"     Min_terms =[]     no_var = count_no_alphabets(SOP_expr)     Cal_Min_terms(Min_terms, SOP_expr)     POS_expr = Cal_Max_terms(Min_terms, no_var, SOP_expr[0])     print "Standard POS form of", SOP_expr, " ==> ", POS_expr    # driver code     if __name__=="__main__":     main()

Output:

Standard POS form of ABC'+A'BC + ABC + AB'C  ==>  (A+B+C).(A+B+C').(A+B'+C).(A+B'+C').(A'+B+C).(A'+B+C')
Standard POS form of A'B + AB'  ==>  (A+B).(A+B').(A'+B)
Standard POS form of xyz'+x'y'z'+xy'z  ==>  (x+y+z').(x+y'+z).(x+y'+z').(x'+y+z).(x'+y'+z')

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.