Question

In: Computer Science

USING PYTHON, write a function that takes a list of integers as input and returns a...

USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest)

Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2].

DO NOT use any special or built in functions like append, reverse etc.

Solutions

Expert Solution

source code:


def bubbleSort(arr):
n = len(arr)
  
# Traverse through all array elements
for i in range(n-1):
# range(n) also work but outer loop will repeat one time more than needed.
  
# Last i elements are already in place
for j in range(0, n-i-1):
  
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] < arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
  
def even(l):
for i in l:
if(int(i)%2==1):
l.remove(i)
bubbleSort(l)
return l


print(even( [1,6,3,8,2,5] ))
  


Related Solutions

Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters...
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters in mystring that also occur in the string ‘Python’. Using above function, write a program that repeatedly prompts the user for a string and then prints the number of letters in the string that are also in string ‘Python’. The program terminates when the user types an empty string.
All functions are written in python Write a function cube_evens_lc(values) that takes as input a list...
All functions are written in python Write a function cube_evens_lc(values) that takes as input a list of numbers called values, and that uses a list comprehension to create and return a list containing the cubes of the even numbers in values (i.e., the even numbers raised to the third power). For example: >>> cube_evens_lc([2, 5, 6, 4, 1]) result: [8, 216, 64] This version of the function may not use recursion. Write a function cube_evens_rec(values) that takes as input a...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Python: How would I write a function using list comprehensions to list of integers into two...
Python: How would I write a function using list comprehensions to list of integers into two halves with the evens first, followed by odds? And on top of that, the numbers should be listed in their original order.
python 3 please Define a function voweliest that takes as input a string and returns as...
python 3 please Define a function voweliest that takes as input a string and returns as output a tuple where the string that has the most vowels in it is the first element and the second is the number of vowels in that string. Note: don't worry about ties or capital letters Hint: consider defining and using a separate function that counts the number of vowels in a given string
write a function that takes as input the root of a general tree and returns a...
write a function that takes as input the root of a general tree and returns a binary tree generated by the conversion process illustrated in java
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT