repl.it
@Scoder12/

linear equation finder

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import operator
import time
import replit;replit.clear()

invert_dict = {
    '*': '/',
    '/': '*',
    '+': '-',
    '-': '+'
}

oper_dict = {
    '*': operator.mul,
    '/': operator.truediv,
    '-': operator.sub,
    '+': operator.add,
}

format_oper_dict = {v: k for k, v in oper_dict.items()}

stops_dict = {
    '+': operator.gt,
    '-': operator.lt
}

def get_data():
    ops = input("Enter operations: ").split(' ')
    #ofs = [oper_dict[i] for i in ops]
    for o in ops:
        if o not in oper_dict:
            print("Invalid operator")
            continue
    data = []
    while True:
        try:
            x, res = input('Enter x and result, separate with comma:').split(',')
            x = int(x)
            res = int(res)
        except KeyboardInterrupt:
            break
        data.append([x, res])
    return ops, data

def gen_range(xres, yres):
    if xres < 0 or yres < 0:
        return range(-100, 100)
    return range(100)

def solve(ops, x, xres, y, yres, pad=''):
    print(' '.join(ops))
    if len(ops) < 2:
        if len(ops) != 1:
            raise ValueError('e')
    op = oper_dict[ops[0]]

    if len(ops) == 1:
        if ops[0] in stops_dict:
            if stops_dict[ops[0]](x, xres) and (xres > 0 and yres > 0):
                print(f'OPTM none for {x} {ops[0]} *')
                return None
        for i in gen_range(xres, yres):
            if op(x, i) == xres:
                print(f'{pad} return {op}, {i}')
                time.sleep(1)
                return op, i
        print(f'none for {x} {ops[0]} *')
        return None
    
    for term in gen_range(xres, yres):
        #print(i)
        n = op(x, term)
        print(f'{x} {ops[0]} {term} = {n}')
        val = solve(ops[1:], n, xres, y, yres, pad=' '*3)
        if val:
            print(pad, 'ops:', ops)
            print(pad, 'x:', x)
            print(pad, 'xres:', xres)
            print(pad, 'y:', y)
            print(pad, 'yres:', yres)
            val = [(op, term), val]
            n = y
            print(val)
            for newop, i in val:
                n = newop(n, i)
                if n == yres:
                    print(f"return {n}, {ops[0]}, {val}")
                    time.sleep(1)
                    return n, ops[0], val
                #else:
                #    print('no')
            #print('no')
        
    
    raise ValueError("No solution")

while True:
    ops, data = get_data()
    #ops = ['*', '-']
    #data = [[3, 9], [4, 21]]
    print('Finding...')
    x, xres = data[0]
    y, yres = data[1]
    _, _, val = solve(ops, x, xres, y, yres)
    s = ['f(x) = x']
    for op, term in val:
        if op in format_oper_dict:
            op = format_oper_dict[op]
        s.append(f"{op} {term}")
    print(' '.join(s))
    break