Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write it in python Write a function called Scramble_letters, which takes two str

ID: 3798402 • Letter: W

Question

write it in python

Write a function called Scramble_letters, which takes two strings as parameters. and returns a string containing the same characters but randomly reordered. Remember we've used the random, rand into() function before, and it'll be every useful here. For example, scramble_letters('sensible') might return 'sienbels' or 'blenssie' Write a function called is_anagram, which takes two strings as parameters. It returns a boolean (True of False) indicating whether the strings are anagrams of each other. An anagram means they have the exact same letters but in a different order. Examples: is_anagram('pots', 'spot') returns True, is_anagram ('hello', 'goodbye') returns False. The frequency of letters does matter for anagrams, so is_anagram ('choose', chose') should return False.)

Explanation / Answer

def scramble_letters(n):
l = len(n)#finding length of the string
s=""#taking an empty string
a=[]#creating empty list..
for x in range(l):
r = random.randint(0,l-1)#finding random values to generate random string
while r in a:
   r = random.randint(0,l-1)
a.append(r)
s=s+n[r]#generating random string
return s#returning string

def toList(string):

    tmp = []

    for i in string:

        tmp.append(i)

    return tmp

def toString(List):

    return ''.join(List)

# function to check whether two strings are anagram

# of each other

def areAnagram(str1, str2):

    # Get lengths of both strings

    n1 = len(str1)

    n2 = len(str2)

    # If lenght of both strings is not same, then

    # they cannot be anagram

    if n1 != n2:

        return False

    # Sort both strings

    str1 = quickSort(str1, 0, n1 - 1)

    str2 = quickSort(str2, 0, n2 - 1)

    # Compare sorted strings

    for i in xrange(n1):

        if str1[i] != str2[i]:

            return False

    return True

# Following functions (exchange and partition are

# needed for quickSort)

def exchange(a, b):

    return b, a

def partition(A, si, ei):

    x = A[ei]

    i = si - 1

    for j in xrange(si, ei):

        if A[j] <= x:

            i+=1

            A[i], A[j] = exchange(A[i], A[j])

    A[i+1], A[ei] = exchange(A[i+1], A[ei])

    return i + 1

# Implementation of Quick Sort

# A[] --> Array to be sorted

# si --> Starting index

# ei --> Ending index

def quickSort(string, si, ei):

    A = toList(string)

    pi = 0

    if si < ei:

        pi = partition(A, si, ei)

        quickSort(A, si, pi-1)

        quickSort(A, pi+1, ei)

    return toString(A)

# Driver program to test the above function

str1 = "test"

str2 = "ttew"

if areAnagram(str1, str2):

    print "The two strings are anagram of each other"

else:

    print "The two strings are not anagram of each other"