Dada una lista. La tarea es imprimir el mayor número par y el mayor número impar en una lista.
Ejemplos:
Input: 1 3 5 8 6 10 Output: Largest even number is 10 Largest odd number is 5 Input: 123 234 236 694 809 Output: Largest odd number is 809 Largest even number is 694
El primer enfoque utiliza dos métodos, uno para calcular el número par más grande y el otro para calcular el número impar más grande en una lista de números, ingresados por el usuario. Cada uno de los métodos imprime el mayor número par e impar respectivamente. Mantenemos un contador para cada método, para el par o impar actual más grande y verificamos si el número es divisible por dos o no. En consecuencia, imprimimos los valores más grandes.
Python3
class LargestOddAndEven: # find largest even number of # the list def largestEven(self, list): # counter for current largest # even number curr = -1 for num in list: # converting number to integer # explicitly num = int(num) # even number is divisible by 2 and # if larger than current largest if(num % 2 == 0 and num > curr): # replace current largest even curr = num print("Largest even number is ", curr) # find largest odd number of the list def largestOdd(self, list): # current largest odd number currO = -1 for num in list: # converting number to integer # explicitly num = int(num) # even number is divisible by 2 and # if larger than current largest if(num % 2 == 1 and num > currO): # replace current largest even currO = num print("Largest odd number is ", currO) list_num = [1, 3, 5, 8, 6, 10] # creating an object of class obj = LargestOddAndEven() # calling method for largest even number obj.largestEven(list_num) # calling method for largest odd number obj.largestOdd(list_num)
Producción:
Largest even number is 10 Largest odd number is 5
El segundo enfoque utiliza una versión optimizada del primer enfoque, donde calculamos los valores más grandes en un solo método. Todavía mantenemos dos contadores, pero el ciclo for que itera sobre la lista se ejecuta solo una vez.
Python3
class LargestOddAndEven: # find largest even number of the list def largestEvenandOdd(self, list): # counter for current largest even # number curr = -1 # counter for current largest odd # number currO = -1 for num in list: # converting number to integer # explicitly num = int(num) # even number is divisible by 2 and # if larger than current largest if(num % 2 == 0 and num > curr): # replace current largest even curr = num elif(num % 2 == 1 and num > currO): # replace current largest even currO = num print("Largest odd number is ", currO) print("Largest even number is ", curr) # input a list of numbers list_num = [123, 234, 236, 694, 809] # creating an object of class obj = LargestOddAndEven() # calling method for largest even and odd number obj.largestEvenandOdd(list_num)
Producción:
Largest odd number is 809 Largest even number is 694
Método 3: Usar la comprensión de lista y la función máxima en python:
- Almacene números pares e impares en listas separadas utilizando la comprensión de listas .
- imprime max() de las listas correspondientes.
A continuación se muestra la implementación del enfoque anterior:
Python3
# Python program for the above approach def printmax(lis): # Using list comprehension storing # even and odd numbers as separate lists even = [x for x in lis if x % 2 == 0] odd = [x for x in lis if x % 2 == 1] # printing max numbers in corresponding lists print("Largest odd number is ", max(odd)) print("Largest even number is ", max(even)) # Input a list of numbers lis = [123, 234, 236, 694, 809] printmax(lis) # This code is contributed by vikkycirus
Producción:
Largest odd number is 809 Largest even number is 694