Question: PYTHON3; DO NOT IMPORT ANY PACKAGES REQUIREMENTS : You must solve ALL questions with RECURSION . You are not allowed to use for, while, map,

PYTHON3; DO NOT IMPORT ANY PACKAGES

REQUIREMENTS: You must solve ALL questions with RECURSION. You are not allowed to use for, while, map, or filter. You must make recursive calls to the function itself, instead of creating recursive inner functions.

PLEASE FOLLOW THE REQUIREMENTS AND DON'T CREATE ANY INNER FUNCTIONS. Need 1a and 1d

1.

a)

Given a string and two target characters (string with length 1) target0 and target1, return the difference between the count of target0 and the count of target1 in the string. This function is case sensitive (see the third example).

Restriction:

You cannot use the built-in function str.count() to count the target characters directly.

Examples:

difference_of_counts("ABCcccCBA", A, c)

2 (count of A) - 3 (count of c) = -1

difference_of_counts("ABCcccCBA", A, B)

2 (count of A) - 2 (count of B) = 0

difference_of_counts("ABCcccCBA", A, a)

2 (count of A) - 0 (count of a) = 2

def difference_of_counts(string, target0, target1):

"""

>>> difference_of_counts("ABCcccCBA", "A", "c")

-1

>>> difference_of_counts("ABCcccCBA", "A", "B")

0

>>> difference_of_counts("ABCcccCBA", "A", "a")

2

"""

# YOUR CODE GOES HERE #

d)

Given a list of non-negative integers (lst), recursively check if all elements at even indices are odd integers and all elements at odd indices are even integers. Return True if lst satisfies this requirement or lst is empty, otherwise return False.

Hint: Think about conditions you need to check in the recursive steps and how many elements to decrease the list by. These conditions can be written in a single line. Often you can shorten your if/else if you use conditions directly. For example, functions below is_marina and is_marina_short are equivalent. The second function checks if the name is the same as Marina and prints the result of comparison.

def is_marina(name):

if name == "Marina":

print(True)

else:

print(False)

>>> is_marina("Marina")

True

>>> is_marina("MarinaL")

False

def is_marina_short(name):

print(name == "Marina")

>>> is_marina_short("Marina")

True

>>> is_marina_short("MarinaL")

False

def parity_mismatch(lst):

"""

>>> parity_mismatch([])

True

>>> parity_mismatch([1, 0, 3, 2, 5, 4])

True

>>> parity_mismatch([1, 0, 3, 2, 5, 5])

False

"""

# YOUR CODE GOES HERE #

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!