Level 1: Beginner
Hi guys, I show my code here:
#Definir Tupla
numbers = (10, 5, 8, 20, 15)
#Convertir tupla en lista
numbers = list(numbers)
#Ordenar de mayor a menor
numbers.sort()
#Eliminar valores duplicados
uniques_values = list(set(numbers))
#Impresion
print(uniques_values[-2])
I included an instruction to remove duplicate values only per creative
This function efficiently finds the second largest number in a single pass through the tuple, without sorting or modifying the original data. It has a time complexity of O(n), where n is the number of elements in the tuple.
def second_largest_in_tuple(t):
largest = second_largest = float('-inf')
for num in t:
if num > largest:
second_largest = largest
largest = num
elif num > second_largest and num != largest:
second_largest = num
return second_largest
# Example usage
result = second_largest_in_tuple(numbers)
print(result) # Output: 15
Hi guys, I show my code here:
#Definir Tupla
numbers = (10, 5, 8, 20, 15)
#Convertir tupla en lista
numbers = list(numbers)
#Ordenar de mayor a menor
numbers.sort()
#Eliminar valores duplicados
uniques_values = list(set(numbers))
#Impresion
print(uniques_values[-2])
I included an instruction to remove duplicate values only per creative
This function efficiently finds the second largest number in a single pass through the tuple, without sorting or modifying the original data. It has a time complexity of O(n), where n is the number of elements in the tuple.
def second_largest_in_tuple(t):
largest = second_largest = float('-inf')
for num in t:
if num > largest:
second_largest = largest
largest = num
elif num > second_largest and num != largest:
second_largest = num
return second_largest
# Example usage
numbers = (10, 5, 8, 20, 15)
result = second_largest_in_tuple(numbers)
print(result) # Output: 15