C
C
cyberkilla2020-06-08 18:36:54
Python
cyberkilla, 2020-06-08 18:36:54

Is it possible to visually shorten a piece of code?

It doesn't matter why, but it is required to visually shorten a piece of code, specifically this part:

5ede5a96b4aa6127172345.png

I tried everything I knew and could, but nothing came of it. Maybe someone has a couple of ideas. Thanks in advance!

from __future__ import division
import mpmath
import sympy
from sympy import *

arr1 = (open("f.txt")).read()
mark = 0
kits = set()
kits.add(arr1)


for i in range(1,9) :
    for i in range(len(kits)) :
        arr1 = list(kits)[i]
        for j in range(1,9) :
            array = arr1
            array = array.replace(str(j), "0")
            kits.add(array)

for i in range(len(kits)) :
    array = list(kits)[i]
    x=symbols('x1:9', positive=True)
    x1,x2,x3,x4,x5,x6,x7,x8=x
    mark=0
   
    for j in range(1,9):
      if array[j-1] == "0" :
        if j == 1:
          x1 = 0
        if j == 2  :
          x2 = 0
        if j == 3:
          x3 = 0
        if j == 4:
          x4 = 0
        if j == 5:
          x5 = 0
        if j == 6 :
          x6 = 0
        if j == 7:
          x7 = 0
        if j == 8 :
          x8 = 0
        mark+=1
        
    if mark >= 3 :
      eq=(3*x1 - 11*x2 + 11*x3 + 4*x4 - 5*x5 + 6*x6 - 2*x7 + 8*x8 - 112, -2*x1 + 0*x2 - 50/11*x3 + 11*x4 + 12*x5 - 6*x6 -1*x7 + 3*x8 - 113,-2*x1 + 4*x2 + 6*x3 + 7*x4 + 12*x5 -11*x6 -1*x7 + 13*x8 - 109,-1*x1 + 3*x2 - 11/4*x3 - 4*x4 + 5*x5 - 1*x6 + 12*x7 - 1*x8 - 132,2*x1 + 5*x2 + 7*x3 -11*x4 +6*x5 +1*x6 + 1*x7 - 2*x8 - 211)
      solver = sympy.solve(eq, x1,x2,x3,x4,x5,x6,x7,x8)
      if str(type(solver)) != "<class 'list'>" :
        print('\n Точка с координатами: '+ (str(solver)[1:-1]), end =', если ')
        xs=(x1,x2,x3,x4,x5,x6,x7,x8)
        for i in range(8):
          if xs[i]==0:
            print('x'+str(i+1)+'=0', end = ', ')
        print('\n ______________________________________________________')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Sobolev, 2020-06-08
@Sobolev5

Good afternoon. Read about eval
https://pythonz.net/references/named/eval/
This is exactly your case.
In general, if you have a similar situation, you are definitely doing something wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question