Question

Write a python function that returns a list or array of tuples containing every permutation of...

Write a python function that returns a list or array of tuples containing every permutation of numbers 1 to n. For example:

permutate(1): [(1,)]

permutate(2): [(1, 2), (2, 1)]

permutate(3): [(1, 2, 3), (1, 3, 2), (3, 1, 2), (3, 2, 1), (2, 3, 1), (2, 1, 3)]

#here is some starting code

def permutate(n):

result = []

return result

0 0
Add a comment Improve this question Transcribed image text
Answer #1
SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.


# permutations of numbers 1 to n
def permutate(n):
    result = []
    numbers = list(range(1, n + 1))
    # take an list of lists empty
    answer = [[]]
    # Loop through the numbers
    for x in numbers:
        perm = []
        for p in answer:
            for i in range(len(p) + 1):
                # append that permutation to the list
                perm.append(p[:i] + [x] + p[i:])
                answer = perm
    # convert it to tuples
    for l in answer:
        result.append(tuple(l))

    # return the tuples in the order needed
    return  list(reversed(result[len(result) // 2:])) + result[:len(result) // 2]


print(permutate(1))
print(permutate(2))
print(permutate(3))

-=============

SCREENSHOT:

Add a comment
Know the answer?
Add Answer to:
Write a python function that returns a list or array of tuples containing every permutation of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python recursive function: Given an ordered list L. A permutation of L is a rearrangement of...

    Python recursive function: Given an ordered list L. A permutation of L is a rearrangement of its elements in some order. For example (1,3, 2) and (3, 2, 1) are two different permutations of L=(1,2,3). Implement the following function: def permutations (lst, low, high) The function is given a list 1st of integers, and two indices: low and high (lows high), which indicate the range of indices that need to be considered The function should return a list containing all...

  • Python. Write a function that takes in a list and returns the first nonzero entry. def...

    Python. Write a function that takes in a list and returns the first nonzero entry. def nonzero(lst): """ Returns the first nonzero element of a list >>> nonzero([1, 2, 3]) 1 >>> nonzero([0, 1, 2]) 1 >>> nonzero([0, 0, 0, 0, 0, 0, 5, 0, 6]) 5 """ "*** YOUR CODE HERE ***"

  • Write function all_coprime_pairs( ) which takes as input a list, L, with positive unique integers (i.e....

    Write function all_coprime_pairs( ) which takes as input a list, L, with positive unique integers (i.e. no duplicated integers), and should return: 1. List (of tuples) containing all pairs of numbers in L that are coprime. 2. Empty list, If no pair of numbers in L is coprime. Your solution must include a function call to the function coprime designed in the previous question. The order of the tuples or the order of the two numbers within the tuples is...

  • Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function...

    Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function that takes in a string containing some combination of letters, numbers, and spaces, and return the starting index of the first instance of “waldo” contained in that string. If the string does not contain “waldo”, return -1. This function IS case sensitive, so “waldo” is not the same as “WALDO”. Code using string functions such as .index() and .find() that oversimplify the problem will...

  • Python Write a move function that takes a list of tuples, where each tuple is an...

    Python Write a move function that takes a list of tuples, where each tuple is an x-y-z coordinate pair. This function should update the original list in-place to shift/move/translate the coordinates by an x_move, y_move, and z_move (respectively) and return None.

  • In PYTHON: Write a function that receives a list of integers and returns the number of...

    In PYTHON: Write a function that receives a list of integers and returns the number of integers that are larger than the element immediately before them. For example, given [1,2,3,-5,0,-5,-10] the function should return 3 (since 1<2, 2<3, and -5<0).

  • Write a function in python that takes a set A and a relation R(x, y) on...

    Write a function in python that takes a set A and a relation R(x, y) on A (as a python function such that R(x, y) returns true if and only if the relation xRy holds), and returns True if and only if the relation R is reflexive. Here is the function signature you need to use. def is reflexive(A, R): You can test your code as follows. s = {1,2,3} def y(x, y): return x == y def n(x, y):...

  • (Python)Implement the function deep_list, which takes in a list, and returns a new list which contains...

    (Python)Implement the function deep_list, which takes in a list, and returns a new list which contains only elements of the original list that are also lists. Use a list comprehension. def deep_list(seq): "" "Returns a new list containing elements of the original list that are lists. >>> seq = [49, 8, 2, 1, 102] >>> deep_list(seq) [] >>> seq = [[500], [30, 25, 24], 8, [0]] >>> deep_list(seq) [[500], [30, 25, 24], [0]] >>> seq = ["hello", [12, [25], 24],...

  • Python: Using your favorite text editor, write a program that outputs the string representation of numbers...

    Python: Using your favorite text editor, write a program that outputs the string representation of numbers from 1 to n. But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. Submit by uploading a .py file Example: n = 15, Return: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"...

  • Language: Python Topic: Tuples Function name : todo_tuple Parameters : todo (list of tuples of strings),...

    Language: Python Topic: Tuples Function name : todo_tuple Parameters : todo (list of tuples of strings), completed (list of strings) Returns: final_list (list) Description : Write a function that takes in a list of tuples of strings that represents the work you have to do in each class, and a list of strings that represent the work you have already completed. Each tuple in the todo list represents the work for a single class. For this function, go through the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT