Question

def countEvens(listOfInts): ''' - Returns an integer value representing the number of even numbers that exist...

def countEvens(listOfInts):
    '''
    - Returns an integer value representing the number of even numbers that
    exist in listOfInts.
    - Return 0 if listOfInts is not a list type or if no even number exists
    in listOfInts.
    - Note: elements in listOfInts can contain any data type.
    '''
    return "stub"
# Tests for countEvens
def test_countEvens_1():
    assert countEvens([0,2,4]) == 3

def test_countEvens_2():
    assert countEvens([0,"2","Four", 6]) == 2

def test_countEvens_3():
    assert countEvens((0,2,4)) == 0

def test_countEvens_4():
    assert countEvens([-1,1,3,5,7]) == 0
    
def test_countEvens_5():
    assert countEvens([1.0,2.0,3.0,4.0,5.0,6.0,7.0,-2.0]) == 4
0 0
Add a comment Improve this question Transcribed image text
Answer #1
def countEvens(listOfInts):
    '''
    - Returns an integer value representing the number of even numbers that
    exist in listOfInts.
    - Return 0 if listOfInts is not a list type or if no even number exists
    in listOfInts.
    - Note: elements in listOfInts can contain any data type.
    '''
    count = 0
    if type(listOfInts) == list:
        for item in listOfInts:
            if (type(item) == int or type(item) == float) and item % 2 == 0:
                count += 1
    return count


# Tests for countEvens
def test_countEvens_1():
    assert countEvens([0, 2, 4]) == 3


def test_countEvens_2():
    assert countEvens([0, "2", "Four", 6]) == 2


def test_countEvens_3():
    assert countEvens((0, 2, 4)) == 0


def test_countEvens_4():
    assert countEvens([-1, 1, 3, 5, 7]) == 0


def test_countEvens_5():
    assert countEvens([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, -2.0]) == 4

Add a comment
Know the answer?
Add Answer to:
def countEvens(listOfInts): ''' - Returns an integer value representing the number of even numbers that exist...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • def second_index(a_list, number): Accepts a list of integers and an integer, and returns the index of...

    def second_index(a_list, number): Accepts a list of integers and an integer, and returns the index of the SECOND occurrence of the integer in the list. It returns None if the number does not occur two or more times in the list. Example 1: second_index ([2,34,3,45,34,45,3,3), 3) returns 6 Example 2: second_index( [2,34,3,45,34,45,3,3), 45) returns 5 Example 3: second_index ( [2,34,3,45,134,45,3,3), 134) returns None Example 4: second_index( [2,34,3,45, 134,45,3,3), 100) returns None return None def hasEveryLetter(s): #s is a string Returns...

  • Create a program that will determine the even number(s) from a list of numbers. Your program...

    Create a program that will determine the even number(s) from a list of numbers. Your program should create and call a function FindEven that accepts a list of numbers and returns a list of only the even numbers in sorted order. Note that the original list is given in the starter code. def body(): numberlist = [1, 1000, 5, -3, 2, 16 # Write your code here and notice level # Do NOT include: if __name__ == been provided for...

  • def count_neighbors(cells, row, col): • Return value: An integer; the number of "alive" neighbors that the...

    def count_neighbors(cells, row, col): • Return value: An integer; the number of "alive" neighbors that the cell in the given row and column has. • Assumptions: cells will be a two-dimensional list with at least one row and one element in that row. • Notes: The tests for this function are hidden. Be careful: row or column may not be valid (return -1 if so) • Examples: count_neighbors([[0,0,0],[0,1,0],[0,0,0]],1,1) → 0 count_neighbors([[0,1,0,0],[0,1,0,1],[1,1,0,1]],2,1) → 3 count_neighbors([[0,0,0],[0,1,0],[0,0,0]],-1,1) → -1

  • Write another function buddies(...) that receives a list (with at least 3 integer numbers), and returns...

    Write another function buddies(...) that receives a list (with at least 3 integer numbers), and returns the number of pairs of buddies in the list. We will define a “pair of buddies” to be the pair of elements that are contiguous in the list and that are equal, and also so that the contiguous elements to the pair are different (or the pair is in an extreme of the list). For example, [5,2,2,3] has 1 pair of buddies (the 2’s)...

  • I need to remove the even numbers in the back of the list, any ideas? #f....

    I need to remove the even numbers in the back of the list, any ideas? #f. Demonstrate moving even elements to the front of the list. data = list(ONE_TEN) def evenToFront(data): for i in range(len(data)): if i % 2 == 0: data.insert(0, i) comments too please

  • Help with solution using Python test using def riffle_generator(seed): random.seed(seed) for i in range(1000): n =...

    Help with solution using Python test using def riffle_generator(seed): random.seed(seed) for i in range(1000): n = random.randint(0, 100) items = [random.randint(0, 10**6) for j in range(2 * n)] yield (items, True) yield (items, False) Riffle def riffle(items, out True): Given a list of items that is guaranteed to contain an even number of elements (note that the integer zero is an even number), create and return a list produced by performing a perfect riffle to the items by interleaving the...

  • o Write a function that outputs even numbers less than the input number. • Function name...

    o Write a function that outputs even numbers less than the input number. • Function name should be 'evenprint'. • A number is given as input to the function. . The function must return a list of even numbers def evenprint(num): #write statement >[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74,...

  • Python question class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value...

    Python question class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Question 2.1. Empty Node In some cases in it convenient to have a notion of an empty linked list. Usually it means that the linked list does not have any elements in it. In order to keep things simple (for now) we will assume that the list is empty, if it has a single node and its value is None. Add...

  • Number 4 please and thank you Complete the Function (4) The isslice function returns True if...

    Number 4 please and thank you Complete the Function (4) The isslice function returns True if its first argument is a subsequence o s hrst argument is a subsequence of its second argument, for any sequence type. So, for example: >>> isslice('bam', "bimb amboo") True >>> isslice ([21,22), list (range (10))) False >>> isslice ([0,0,0).[0,0,0]) True Unfortunately, the definition is incomplete. Fill in the blanks to complete the definition. def isslice(x, S): return -----([x==s [ _____) for i in range(_____)...

  • 2. Consider the following function # listOfNumbers is a list of only numbers # def processList(listOfNumbers):...

    2. Consider the following function # listOfNumbers is a list of only numbers # def processList(listOfNumbers): result = [] for i in listOfNumbers: if i<0 == 0: result.append(i*i) else: result.append((i*i)+1) return result First, study and test processList(listOfNumbers) to determine what it does Then rewrite its body so that it accomplishes the same task with a one-line list comprehension. Thus, the resulting function will have exactly two lines, the def line and a return line containing a list comprehension expression. 3....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT