Question: In the file tdd_1.py, create the function count_letter. Takes two arguments The first argument is the string to count the letter in The second argument

In the file tdd_1.py, create the function count_letter. Takes two arguments The first argument is the string to count the letter in The second argument is the letter to search for Returns an integer: the number of occurrences of a letter in a string

Task 1.2 (20%) Adjust the function so it raises a TypeError exception if any of the arguments is not a string. Example:

Task 1.3 (20%) Adjust the function so it raises a ValueError exception: if the second argument is an empty string (the first argument can be an empty string) if the second argument is a string with more than 1 character

2. Function add_lists The following tasks are testable using test_add_lists.py. Task 2.1 (20%) In the file tdd_1.py, create the function add_lists. It takes two lists as arguments, and computes their sum element-wise.

Task 2.2 (20%) Modify the function so it raises an IndexError exception if the arguments do not have the same length.

Tast_add_list

import pytest from tdd_1 import add_lists

class Helper: @staticmethod def list_1(): return [1, 5, 10] @staticmethod def list_2(): return [100, 100, 100]

def test_add_lists(): assert add_lists(Helper.list_1(), Helper.list_2()) == [101, 105, 110]

def test_add_lists_different_lengths(): try: with pytest.raises(IndexError): add_lists(Helper.list_1(), []) except: assert False

try: with pytest.raises(IndexError): add_lists([1], Helper.list_1()) except: assert False

Test_count_letter

mport pytest from tdd_1 import count_letter

def test_count_letter(): assert count_letter('fghj', 'a') == 0 assert count_letter('aabbcda', 'a') == 3 assert count_letter('b' * 10, 'b') == 10

def test_count_letter_invalid_arg_types(): try: with pytest.raises(TypeError): count_letter(20, 'a') with pytest.raises(TypeError): count_letter('a', 20) except: assert False

def test_count_letter_invalid_second_argument(): try: with pytest.raises(ValueError): count_letter('a', '') with pytest.raises(ValueError): count_letter('a', 'aa') except: assert False

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!