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

As a preface to this question, i must say ive posted this question in 4 or five

ID: 3591942 • Letter: A

Question

As a preface to this question, i must say ive posted this question in 4 or five diffent ways to make it as clear as possible. I received one good solution of code but its not commented out well and the variable names are not very descriptive or clear. I've had two other responses where the person simply copy and pasted code from the internet that i've already seen and doesn't accomplish what i need. And the most recent response was that my question wasn't clear enough. I feel that only one of these post should count toward my 20 monthly questions with this being another if someone can answer it. I cant imagine how much clearer i can be. I simply want the part of this question in all caps and surrounded by asterisks addressed. There is already code that solves the problem.

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: USING INFO BASED ON THE JOHNSON-TROTTER ALGORITHM. You may find that asking yourself the following four questions will help to think in terms of the Johnson-Trotter algorithm #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.

*SO THE FOLLOWING CODE SEEMS TO WORK FOR WHAT THE ABOVE INSTRUCTIONS ASK FOR, BUT MY QUESTION IS DOES THIS ACTUALLY USE THE JOHNSON-TROTTER ALGORITHM AND IF SO CAN YOU PLEASE COMMENT THIS OUT IN A DETAILED MANNER SO I CAN UNDERSTAND WHAT IS GOING ON? OR YOU COULD CHANGE THE VARIABLES TO SOMETHING MORE APPROPRIATE THAT ALLOW ME TO UNDERSTAND WHAT THIS CODE IS DOING STEP BY STEP. FOR EXAMPLE, I HAVE NO IDEA WHAT THE ARGUEMENT (RECL) DOES OR STANDS FOR. WHAT DO THE m AND p AND l VARIABLES REPRESENT AND HOW DOES num DIFFER FROM n IN THEIR USE IN THE CODE. THANK YOU FOR ANY HELP!!*

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))

Explanation / Answer

The code is fixed

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))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote