linear functions in python

Solutions on MaxInterview for linear functions in python by the best coders in the world

showing results for - "linear functions in python"
Angelo
24 Jul 2016
1def neg(formula: list):
2    if formula[3] == "-":
3        formula[4] = float("-" + str(formula[4]))
4        formula[3] = "+"
5
6def conv(lis: list) -> list:
7    if len(lis) == 3:
8        lis += ["+", "0"]
9    lis[2] = float(lis[2])
10    lis[4] = float(lis[4])
11    return lis
12
13def process(cmd: str, formula: list):
14    cmd = cmd.split(" ")
15    if len(cmd) == 3:
16        if cmd == ["x", "=", cmd[2]]:
17            neg(formula)
18            print("y =", float(cmd[2]) * formula[2] + formula[4])
19        elif cmd == ["y", "=", cmd[2]]:
20            neg(formula)
21            print("x =", (float(cmd[2]) - formula[4])/formula[2])
22    elif cmd == ["y-intercept"]:
23        print(formula[4])
24    elif cmd == ["x-intercept"]:
25        if formula[3] == "-":
26            formula[4] = float("-" + str(formula[4]))
27        print((0 - formula[4])/formula[2])
28    elif cmd == ["run", cmd[1]]:
29        for i in range(int(cmd[1])):
30            print("(" + str(i) + ", " + str(i * formula[2] + formula[4]) + ")")
31
32
33formula = conv(input("Type the formula:\n").replace("x", "").split(" "))
34#Type your formula like this y = mx + b
35
36print("\nCommands:")#The commands are x = value, y = value, y-intercept, and x-intercept to find your answer and run (value) how many times.
37while True:
38    process(input(), formula)
39    print()
40