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

Ive asked this question 3 times now and have had you guys copy and paste the exa

ID: 3591216 • Letter: I

Question

Ive asked this question 3 times now and have had you guys copy and paste the exact same code twice from the internet that ive already seen and doesnt do what the problem asks.Please give me an answer that does what the problem asks:"Given a number n from 1 to 9, print each of the permuations of 1, 2, ...n to the screen and Calculate the time required to do so,and print that time to the screen( by writing python code body for the function def permute(n): As the following problem states, please do this using the Johnson-Trotter Algorithm.

#Given a positive integer n between 1 and 9, generate the permuations of the set {1,2,3,4,...,n} *USING the JOHNSON-TROTTER #ALGORITHM.* Using 'import time' and 'time.clock()', determine how many seconds passes when running this code for n=3, 4, 5, and 6. Hints:I wrote the following helper functions. You may find this approach useful. #1. Is a given element mobile? Given a permutation with arrows and a position, return true if the element in that position is mobile and false if it is not mobile. #2. Is any element mobile? Given a permutation with arrows, return true if any element is mobile and false if no element is mobile. #3. What is the position of the largest mobile element? Given a permutation with arrows, return the position of the largest mobile element. #4. What is the next permutation? Given a permutation with arrows, return the next permutation with arrows.

#Given a number n from 1 to 9, print each of the permuations of 1, 2, ... n to the screen. Calculate the time required to do so, and print that time to the screen as well.

import time

def permute(n):

startTime=time.clock()

# BODY OF python FUNCTION i need GOES HERE using # desriptive variable names and comments so i can follow

endTime= time.clock()

print endTime-startTime

# HELPER FUNCTIONS MAY GO HERE if needed

Explanation / Answer

Code:

import time

def permute(num):
if (len(num) == 0):
return []
  
if (len(num) == 1):
return [num]
  
l = [] # empty list that will store current permute
for i in range(len(num)):
m = num[i]
reml = num[:i] + num[i+1:]

for p in permute(reml):
l.append([m] + p)
return l

programStart = time.clock()
n=int(input("Enter an integer from 1 to 9: "))
l = []
for i in range(1,n+1,1):
l.append(i)

permutationStart = time.clock()
for p in permute(l):
print p
permutationEnd = time.clock()

programEnd = time.clock()

print("Total time taken for computing permutations: %f seconds."%(permutationEnd-permutationStart))
print("Total time taken for program: %f seconds."%(programEnd-programStart))

Output: