Home > OS >  Store and compare variables inside a loop - python
Store and compare variables inside a loop - python

Time:10-08

how to compare several input results inside a for loop in python? I'm trying to store the result in a new variable, but i don't know how exactly to do that. Help pls. Here is my tiny code:

n = int(input())
diff1 = -10000
sum_num = 0

for i in range (1, n   1) :
    num1 = int(input())
    num2 = int(input())
    sum_num1 = num1   num2

here i have to compare sum_num1 with the other n number of input sums and also i have to find the biggest difference between two subsequent input sums (e.g. sum_num1[1] = 3, sum_num1[2] = 5, so thedifference is 2) and num. I wrote my logic, but I think it's very wrong.

diff = abs(sum_num - sum_num1)
if diff > diff1 :
    diff1 = diff

CodePudding user response:

Here is one solution:

n = int(input())

inputs = [int(input())   int(input()) for _ in range(n)]
print(max(abs(inputs[i] - inputs[i   1]) for i in range(len(inputs) - 1)))

for each iteration you take two input and sum them to create list inputs.

Now these are the numbers you want to do the subtraction with. Every item with its next item.

CodePudding user response:

I am still a little unclear what you are after. My first suggestion would be to separate out your input data gathering from your algorithm as this will make it much easier to test.

I believe for your input data, you are trying to collect a list of pairs, so something like this:

input_data = [
    (1, -1),
    (0, 3),
    (15, -1),
]

Updating your code minimally, this can be collected like this:

user_input_count = int(input())
user_input_data = [(int(input()), int(input())) for i in range(user_input_count)]
print(user_input_data)

Assuming this is the format you are expecting, from your comments, I think you are want a two step calculation, first taking the different between the pairs, then maximising the distance between two neighbouring outputs, so something like:

input_data = [
    (1, -1),
    (0, 3),
    (15, -1),
]

expected_sum_num = [2, 3, 16]
expected_max_diff = 13

given these expected outputs, we can write an algorithm and test we get the expected results:

input_data = [
    (1, -1),
    (0, 3),
    (15, -1),
]

expected_sum_num = [2, 3, 16]
expected_max_diff = 13

def calc_sum_num(data):
    return [abs(a - b) for a, b in data]

def calc_max_diff(data):
    return max((abs(data[i] - data[i   1]) for i in range(len(data) - 1)))

assert expected_sum_num == calc_sum_num(input_data)
assert expected_max_diff == calc_max_diff(expected_sum_num)

hence:

result = calc_max_diff(calc_sum_num(input_data)))

Let me know if I have misunderstood what you are asking. If I have, it would be useful if you could provide some examples of input_data, expected_sum_num and expected_max_diff values.

As background, this approach is a typical unit testing pattern where you tests algorithms with inputs and expected outputs. I have chosen not to confuse this post by including any unit testing frameworks, but if you are interested I would recommend you look at the python standard library unittest framework, or pytest (another popular unittesting framework for python)

CodePudding user response:

Thanks for the suggestions, but i'm not familiar with lists yet. This task should be done just with for loop(s) and if-else statements. I hope someone in stack_overflow knows how to do it.

  • Related