From 1d5f440ba7f52aab4b661b459a77558fe2ae7256 Mon Sep 17 00:00:00 2001 From: Luis Rodrigues Date: Wed, 12 Feb 2025 19:24:19 +0000 Subject: [PATCH] exercicio 1502 resolvido em python --- .../python/1502.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 1502-can_make_arithmetic_progression_from_sequence/python/1502.py diff --git a/1502-can_make_arithmetic_progression_from_sequence/python/1502.py b/1502-can_make_arithmetic_progression_from_sequence/python/1502.py new file mode 100644 index 0000000..6a1e57b --- /dev/null +++ b/1502-can_make_arithmetic_progression_from_sequence/python/1502.py @@ -0,0 +1,28 @@ +# 1502. Can Make Arithmetic Progression From Sequence +# +# A sequence of numbers is called an arithmetic progression if the difference +# between any two consecutive elements is the same. +# +# Given an array of numbers arr, return true if the array can be rearranged to +# form an arithmetic progression. Otherwise, return false. +# + +# usar o metodo sort() para ordenar o vector +# se o vector tiver tamanho 2, retorna True (só temos a diferença entre dois +# numeros) +# calculamos a diferença entre os dois primeiros numeros, e procuramos nos +# restantes pares de numeros consecutivos se a diferença é igual, se não for +# retornamos False +# se chegarmos ao fim do vector e se mantiver a diferença, então o vector +# tem uma sequencia de progressão aritmetica + + +def canMakeArithmeticProgression(array: list[int]) -> bool: + array.sort() + if len(array) == 2: + return True + diff: int = array[1] - array[0] + for index in range(1, len(array)): + if diff != (array[index] - array[index - 1]): + return False + return True