194k views
2 votes
Complete the function binary_strings(string: str) -> Generator[str, None, None]. binary_strings accepts a string of 0's, 1's, and X's and returns a generator that goes through all possible strings where the X's could be either 0's or 1's. For example with the string '0XX1', the possible strings are '0001', '0011' '0101', and '0111'. When generating the strings, the X's should first be replaced with 0's and then be replaced with 1's. Requirements You may NOT use anything from the itertools or functools modules You must use the yield keyword in your solution to receive credit To receive credit, you must use recursion in this problem. binary_strings does not itself need to be recursive but you must use recursion in your solution

User Pdr
by
4.3k points

1 Answer

5 votes

Answer:

A python program was used for this given question to complete the function binary_strings(string: str) -> Generator[str, None, None].

binary_strings accepts a string of 0's, 1's, and X's and returns a generator that goes through all possible strings where the X's could be either 0's or 1's

Step-by-step explanation:

Solution:

PYTHON CODE:

def binary_strings(string):

binary_string = list(string) # Converting To List

Generator(binary_string,0) # Calling The Recursive Generator Function

''' Recursive Function To Generate All Binary Strings

Formed By Replacing Each 'X' Character By 0 Or 1 '''

def Generator(binary_string,index):

if index == len(binary_string):

print(''.join(binary_string))

return

if binary_string[index] == "X":

# Replace 'X' By '0' And Call Recursively For Next Index

binary_string[index] = '0'

Generator(binary_string, index + 1)

# Replace 'X' By '1' And Call Recursively For Next Index

binary_string[index] = '1'

Generator(binary_string, index + 1)

binary_string[index] = 'X'

else:

# If The Character At A Particular Index is Not 'X' Then

# Calling For Function For Next Index

Generator(binary_string, index + 1)

# Driver code

string = input()

binary_strings(string)

User Ajay Chinni
by
4.2k points