exercicio 1502 resolvido em python
This commit is contained in:
@ -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
|
||||
Reference in New Issue
Block a user