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

Problem 3. (Inverse Permutation) Write a program inverse permutation py that acc

ID: 3807841 • Letter: P

Question

Problem 3. (Inverse Permutation) Write a program inverse permutation py that accepts a permutation of the integers 0 through n 1 as n command-line arguments and writes its inverse. If the permutation is a list a, its inverse is the list b such that [b [i] bla Cilj Be sure to check that the input is a valid permutation. If not, the program must exit with the message "Not a permutation" you can do this by calling the function sys.exit(msg), where mag is the message you want to write. Python inverse-permutation .py 1 2 3 4 5 Not a permutation Python inverse-permutation Py 5 3 4 0 1 2 3 4 5 1 20 Python inverse-permutation py 0 1 2 3 4 5 0 1 2 3 4 5 Python inverse-permutation py 5 4 3 2 1 0 5 4 3 2 1 0 import stdarray import stdio import sys Create a list perm consisting of the integers from the command line perm for v in Define a variable n and set it to the number of elements in perm. Make sure perm represents a valid permutation If not, exit the program with the message Not a permutation. Use a 1 list exists of n booleans t for this purpose. exist for i in if sys.exit exists Invert the permutation into a list perm inverted perm inverted for i in perm inverted t Write the inverted permutation separating each number by a space, and with a newline at the end.

Explanation / Answer

solution--

please take care of the indentation

q=input().split()
for i in range(len(q)):
   q[i]=int(q[i])

l=len(q)
s=int((l*(l+1))/2-(l))
s2=0
b=[0 for i in range(l)]
for i in range(l):
   s2=s2+q[i]
if(s==s2):
   for i in range(l):
       b[q[i]]=i
   print(b)
else:
   print('not a permutation')

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