J
J
Jungles2020-08-11 20:30:49
Python
Jungles, 2020-08-11 20:30:49

Algorithm for finding progression?

task - a sequence of numbers is entered, you need to find the largest number of consecutive numbers forming an arithmetic progression.
wrote this, but the code does not work very confidently, I tried to subtract different numbers, change the sequence, but I did not find the right answer =(

spoiler
list_number = []
while True:
    a = int(input())
    
    if a == 0:
        break
    else:
        list_number.append(a)

count_max = 0
count_list = []

for i in range(0,len(list_number)-2):
    if list_number[i+1] > list_number[i]:
        d = abs(list_number[i+1] - list_number[i])
    else:
        d = list_number[i+1] - list_number[i]
    for j in range((i+1), len(list_number)-2):
        if list_number[j] + d == list_number[j+1]:
            if  j == (len(list_number)-1):
                if list_numberj+1] + d == list_number[j+2]:
                    count_max += 1
        else:
            counter.append(count_max)
            count_max = 0
            break

        
    count_list.append(count_max)
    count_max = 0
        
print(max(list_count))


can be checked on these examples

a) 3; 6; nine; 12; fifteen; 17
b) 1; 12; 23; 34; 45
c) -5; −1; 3; 7; eleven; 15
d) -6; 5; 17; 28; 39
e)3; 6; nine; 12; fifteen; eighteen

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-08-11
@Jungles

I don't know Python, I write in pseudocode:

delta = arr[1] - arr[0]
max_count = 2
count = 2
for (i = 1; i < length(arr)-1; i += 1) {
  if (arr[i+1] - arr[i] == delta) {
    count += 1
  } else {
    delta = arr[i+1] - arr[i]
    count = 2
  }
  if (count > max_count) {
    max_count = count
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question