Question: Create an algorithm that compares timings for functions and determines whether or not a string is an anagram: INSTRUCTIONS (Please read through all of this)

Create an algorithm that compares timings for functions and determines whether or not a string is an anagram:\

INSTRUCTIONS (Please read through all of this)

-The program should have anagram Solution1, 2, 3, and 4 represent these four strategies as included; checking letters off from a first string that occur within the second string; sorting and then comparing the two strings; comparing the first string with each possible permutation of the letters in the second string; and counting the frequency of each letter in each string.

Create an algorithm that compares timings for functions and determines whether or

-Write sentences and paragraphs in your .txt or .docx discussion file, your discussion should be no longer than one page long.

Also included in the instructions:

not a string is an anagram:\ INSTRUCTIONS (Please read through all of

If you're going to help, PLEASEEE explain what your code is doing with #comments in the lines please!!!

this) -The program should have anagram Solution1, 2, 3, and 4 represent

https://pastebin.com/Sk2Jdyfw

import random #stringGenerator.py

import string

import time

from itertools import permutations

def anagramSolution1(s1,s2):

alist = list(s2)

pos1 = 0

stillOK = True

while pos1

pos2 = 0

found = False

while pos2

if s1[pos1] == alist[pos2]:

found = True

else:

pos2 = pos2 + 1

if found:

alist[pos2] = None

else:

stillOK = False

pos1 = pos1 + 1

return stillOK

def anagramSolution2(s1,s2):

alist1 = list(s1)

alist2 = list(s2)

alist1.sort()

alist2.sort()

pos = 0

matches = True

while pos

if alist1[pos]==alist2[pos]:

pos = pos + 1

else:

matches = False

return matches

#anagramSolutions3 is not implemented in Miller notes

def anagramSolution3(s1,s2):

perms = [''.join(p) for p in permutations(s2)]

found=False

for s in perms:

if s1 == s:

found = True

break

return found

def anagramSolution4(s1,s2):

c1 = [0]*26

c2 = [0]*26

for i in range(len(s1)):

pos = ord(s1[i])-ord('a')

c1[pos] = c1[pos] + 1

for i in range(len(s2)):

pos = ord(s2[i])-ord('a')

c2[pos] = c2[pos] + 1

j = 0

stillOK = True

while j

if c1[j]==c2[j]:

j = j + 1

else:

stillOK = False

return stillOK

def buildMyString(n):

s=""

for i in range(n):

aletter=random.choice(string.letters)

s=s+aletter

return(s)

#-- the root (main) program starts here

string.letters = 'abcdefghijklmopqrstuvwxyz'

n=11

s1=buildMyString(n)

if random.uniform(0.0,1.0)<.5:>

r=random.randrange(0,n-1)

aletter = random.choice(string.letters)

s2=s1[0:r]+aletter+s1[r+1:n]

else:

s2=s1

print("s1 is: ",s1)

print("s2 is: ",s2)

print(" ...timing anagramSolution1 with n = ",n)

start=time.time()

print(anagramSolution1(s1,s2))

print(time.time()-start)

print(" ...timing anagramSolution2 with n = ",n)

start=time.time()

print(anagramSolution2(s1,s2))

print(time.time()-start)

print(" ...timing anagramSolution3 with n = ",n)

start=time.time()

print(anagramSolution3(s1,s2))

print(time.time()-start)

print(" ...timing anagramSolution4 with n = ",n)

start=time.time()

print(anagramSolution4(s1,s2))

print(time.time()-start)

Compare timings for function calls that determine whether strings are anagrams for various choices of string length n (say n 10, 100, 1000, etc) with anagramSolutions 1, 2, and 4 until "one gets into size trouble." Do this by commenting out the call to anagramSolution3. Execute multiple times for various n, particularly when n is a value that seems to be the size limit of n Subsequently, determine timings for various choices of n (say n 5, 10, 11, 12,...) in anagramSolution3 until one "gets into trouble." Again, execute multiple times for various n, particularly when n is a value that seems to be the size limit of n. Obviously, as the size of the strings, s1 and s2, gets larger than, say, 30 or 40, you will not want the program to print the strings

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!