Define a Python 3 function shuffle_language(A, B) that returns the shuffle A||B
ID: 3737015 • Letter: D
Question
Define a Python 3 function shuffle_language(A, B) that returns the shuffle A||B of languages A and B. Return the result as a set of strings, that is, without duplicates. If you want to use your function from the previous question, you will need to include it as part of your answer to this question.
Test Result print(sorted(shuffle_language({'ab'}, {'cd', 'e'}))) ['abcd', 'abe', 'acbd', 'acdb', 'aeb', 'cabd', 'cadb', 'cdab', 'eab']
print(sorted(shuffle_language({}, {'aa', 'ab', 'bb'}))) []MInbox t) mill x studentuni er x Mail-nmi44S × y Message My × O classroom Ma r i In Notifications Cabins Kingsta x fh Quiz 4: Finite C chegg study × on × x CSecure |https//quizz018.csse.canterburyacnz/mod/quiz/attempt.phplattenpt-953088scrollipos-/643.2001953125 q13 Quiz2018 Nick Miller uesin 12 Define a Python 3 function shaffla_largag(A, B) that returms the shuffle A |B af languages A and B. Retum the result as a set of strings, that is, without duplicates. It you want to use your function trom the previous question, you will need to include it as part of your answer to this question. For example: Marked cut u 110 Flaa question Test Result print (sartod( chufflo_languag ab "ed,abed' ab' acdacdb' ab' cabd', 'cadb', 'cdab', 'oab Answer: (penalty regime: 10, 20, %) Check O Type here to search 3/25/2018 7
Explanation / Answer
program:
def find_all_permutations(str1, str2, per, m, n, i, res):
#str1 is the first string
#str2 is the second string
#per is the array of characters inserted
#m is the length of the string
#n is the length of the string
#i is the position of next character to be inserted
if m==0 and n==0: #if all characters of both strings are inserted
res.append("".join(per)) #converting array of characters to string
if m != 0: #if there are characters still remaining in string1
per[i] = str1[0] #inserting first character of string1 in per array
find_all_permutations(str1[1:], str2, per, m-1, n, i+1, res) #recursive call for inserting remaining elements
if n != 0: #if there are characters still remaining in string2
per[i] = str2[0] #inserting first character of string2 in per array
find_all_permutations(str1, str2[1:], per, m, n-1, i+1, res) #recursive call for inserting remaining elements
def all_permutations(a,b,res):
per = [''] * (len(a)+len(b)) #initializing string which contains m+n elements
find_all_permutations(a, b, per, len(a), len(b), 0, res)
def shuffle_language(a,b): #function for shuffling the characters of the string
res = [] #list for storing the formed strings
for i in a: #looping through elements in a
for j in b: #looping through elements in b
all_permutations(i,j,res) #calling permutation function
return res #returning list of strings
print(sorted(shuffle_language({'ab'},{'cd','e'})))
print(sorted(shuffle_language({},{'aa','ab','bb'})))
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.