How could my current code that reads integers until -1 is input and checks for the longest pattern of even/odd/even be modified to check for the longest continuous sequence where the previous number is a divisor of the current number.
Code:
longest=[]
current=[]
while True:
n = int(input())
if n == -1:
break
if current and current[-1] % 2 != n % 2:
current.append(n)
else:
current = [n]
if len(current) > len(longest):
longest = current
print(len(longest))
Closest attempt: Code:
longest=[]
current=[]
while True:
n = int(input())
if n == -1:
break
if current % current[-1] == 0:
current.append(n)
else:
current = [n]
if len(current) > len(longest):
longest = current
print(len(longest))
Since current[-1]
is the previous number, you can simply change:
if current and current[-1] % 2 != n % 2:
to:
if current and n % current[-1] == 0:
Sample input:
1
2
4
8
2
4
-1
would output:
4