Question

In: Computer Science

Write a Python function which finds the smallest even and odd numbers in a given list....

Write a Python function which finds the smallest even and odd numbers in a given list. (Use for loop and if conditions for this problem)

Solutions

Expert Solution

PYTHON CODE:

def small_odd_even(lst):

    # variable to store the smallest odd and even
    small_even=lst[0]
    small_odd=lst[0]

    # iterating through every number in the list
    for i in lst:

        # checking for even number
        if i % 2 == 0:

            # checking for smallest even number
            if i < small_even:
                small_even = i
        else:

            # checking for smallest odd number
            if i < small_odd:
                small_odd = i

    # printing the smallest odd and even numbers
    print('Smallest even integer in a list: ',small_even)
    print('smallest odd integer in a list: ',small_odd)          

# testing
if __name__=='__main__':

    # calling the function
    small_odd_even([12,34,23,11,7])
    small_odd_even([111,34,11,7,78,-8,88,-24])


SCREENSHOT FOR CODING:

SCREENSHOT FOR OUTPUT:


Related Solutions

Define a Python function which finds the all of the odd numbers and the product of...
Define a Python function which finds the all of the odd numbers and the product of the odd numbers. (Use for loop and if conditions for this problem. Do not use existing codes. Use your own codes).(You need to use append function to obtain a list of odd numbers)
Write an algorithm that finds both the smallest and largest numbers in a list of n...
Write an algorithm that finds both the smallest and largest numbers in a list of n numbers. Try to find a method that does at most 1.5n comparisons of array items.(but please code in java).
Write a function in Python to compute the sample mean of a list of numbers. The...
Write a function in Python to compute the sample mean of a list of numbers. The return value should be the sample mean and the input value should be the list of numbers. Do not use a built-in function for the mean. Show an example of your function in use.    Thank You
Given a list x = [2,3,5,6,7,8,9], write a python program that find those numbers which are...
Given a list x = [2,3,5,6,7,8,9], write a python program that find those numbers which are divisible by 3. Arrange these numbers in a list called “result”. Hint: similar to page 9 in the slides.
determine whether the given function is even, odd, or neither. Please write a code in MatLab...
determine whether the given function is even, odd, or neither. Please write a code in MatLab to solve this problem below: 1.f(x) = sin 3x please only use Matlab to solve this problem
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in...
Write a smallest_gap(start_num, end_num) function that finds smallest gap between successive primes, considering prime numbers in the range from start_num to end_num (inclusive). For example, start_num = 5 and end_num = 12, the prime numbers in that range are: [5, 7, 11]. The smallest gap between any two prime numbers in this list is 2, so the function would return 2. You may want to modify your solution from Problem 1 on Assignment 4, or you can use this starter...
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Count all nonzero elements, odd numbers and even numbers in a linked list. Code needed in...
Count all nonzero elements, odd numbers and even numbers in a linked list. Code needed in java.
Count all nonzero elements, odd numbers and even numbers in a linked list. Code needed in...
Count all nonzero elements, odd numbers and even numbers in a linked list. Code needed in java through single linked list.
using python 3 2. Write a python program that finds the numbers that are divisible by...
using python 3 2. Write a python program that finds the numbers that are divisible by both 2 and 7 but not 70, or that are divisible by 57 between 1 and 1000. 3. Write a function called YesNo that receives as input each of the numbers between 1 and 1000 and returns True if the number is divisible by both 2 and 7 but not 70, or it is divisible by 57. Otherwise it returns False. 4. In your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT