Question

In: Computer Science

This is python: #Write a function called count_positive_evens. This function #should take as input a list...

This is python:

#Write a function called count_positive_evens. This function
#should take as input a list of integers, and return as
#output a single integer. The number the function returns
#should be the count of numbers from the list that were both
#positive and even.
#
#For example:
#
# count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1
# count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6
# count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0
#
#0 should be not counted as a positive even number.
#
#Hint: Remember, even numbers are numbers that have a
#remainder of 0 when divided by 2.


#Write your function here!


#The lines below will test your code. Feel free to modify
#them. If your code is working properly, these will print
#the same output as shown above in the examples.
print(count_positive_evens([5, 7, 9, 8, -1, -2, -3]))
print(count_positive_evens([2, 4, 6, 8, 10, 12, 15]))
print(count_positive_evens([-2, -4, -6, -8, -10, 1]))

Solutions

Expert Solution

# Write a function called count_positive_evens. This function
# should take as input a list of integers, and return as
# output a single integer. The number the function returns
# should be the count of numbers from the list that were both
# positive and even.
#
# For example:
#
# count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1
# count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6
# count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0
#
# 0 should be not counted as a positive even number.
#
# Hint: Remember, even numbers are numbers that have a
# remainder of 0 when divided by 2.

def count_positive_evens(lst):
    count = 0
    for num in lst:
        if num > 0 and num % 2 == 0:
            count += 1
    return count


# The lines below will test your code. Feel free to modify
# them. If your code is working properly, these will print
# the same output as shown above in the examples.
print(count_positive_evens([5, 7, 9, 8, -1, -2, -3]))
print(count_positive_evens([2, 4, 6, 8, 10, 12, 15]))
print(count_positive_evens([-2, -4, -6, -8, -10, 1]))



Related Solutions

In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input,...
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input, and returns the highest sum of three neighboring elements in it. Write a main method that initializes the following five lists, gets the ThreeSum result for all of them using the above function, and prints the result to the screen. Example of the output: List 1: [4,5,4,5] , Three sum = 14 List 2: [7] , Three sum = 7 List 3: [ ]...
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings....
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings. The first string is #the filename of a file to which to write (output_file), and #the second string is the filename of a file from which to read #(input_file). # #In the input file, there will be an integer on every line. #To the output file, you should write the integer from the #original file multiplied by the line number on which it #appeared....
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.
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
write a matlab function for frequency analysis using DFT. the function should take as input a...
write a matlab function for frequency analysis using DFT. the function should take as input a signal, and as output the number of sinusoids and their frequencies.
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should...
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should output an array, b, that is computed as: b=3a+5. Write a MATLAB function called “fit_line” that accepts 2 input arguments: a column vector of x data and a column vector of y data. The nth element in the input arguments should correspond to the nth Cartesian data point i.e. (xn,yn). The function should compute and return 2 outputs: the slope, m, and the y...
using python 1. #Write a function called multiply_by_index. multiply_by_index #should have one parameter, a list; you...
using python 1. #Write a function called multiply_by_index. multiply_by_index #should have one parameter, a list; you may assume every item #in the list will be an integer. multiply_by_index should #return a list where each number in the original list is #multipled by the index at which it appeared. # #For example: # #multiply_by_index([1, 2, 3, 4, 5]) -> [0, 2, 6, 12, 20] # #In the example above, the numbers 1, 2, 3, 4, and 5 appear #at indices 0,...
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...
In Python Create a function called ℎ?????. The function has as arguments a list called ??????...
In Python Create a function called ℎ?????. The function has as arguments a list called ?????? and a list call center. • List ?????? contains lists that represent points. o For example, if ?????? = [[4,2], [3,2], [6,1]], the list [4,2] represents the point with coordinate ? at 4 and y coordinate at 2, and so on for the other lists. Assume that all lists within points contain two numbers (that is, they have x, y coordinates). • List ??????...
Use python write a function that translates the input string into Pig Latin. The translation should...
Use python write a function that translates the input string into Pig Latin. The translation should be done word by word, where all words will be separated by only one space. You may assume that each word must have at least one vowel (a,e,i,o,u and uppercased counterparts), and there will be no punctuation or other special characters in the input string. The Pig Latin rules are as follows: For words that begin with consonants, all letters before the initial vowel...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT