Answer the question
In order to leave comments, you need to log in
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 =(
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))
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question