Question

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 the different permutations of the elements in 1st. Each such permutation should be represented as a list. For example, if lst= [ 1, 2, 3],the call permutations (1st, 0, 2) could return t1, 2, 3], [2, 1, 3], [1, 3, 2], [3, 2, 1], [3, 1, 2], [2, 3, 1]] Hint: Think recursion!!! Take for example 1st-[i, 2, 3, 4]:to compute the permutation list of lst, try to first write down the list containing all permutations of 1, 2, 3], then, think how to modify it, so you get the permutation list of[1, 2, 3, 4

0 1
Add a comment Improve this question Transcribed image text
Answer #1
def permutations(lst, low, high):
        if low == high:
                return [[lst[low]]]
        perms = permutations(lst, low+1, high)
        result = []

        x = lst[low]

        for p in perms:
                # note p is a list,
                # we need to fit x in whatever place possible
                for i in range(len(p) + 1):
                        pCopy = p[::]
                        pCopy.insert(i, x)
                        result.append(pCopy)
        return result

print(permutations([1, 2, 3], 0, 2))

main.py Bsaved 1 def permutations(lst, low, high): Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux if lowhigh: return [[1st[low]]] 4. perms permutations(1st, lowt1, high) 6 7 8 9 10 result = x - lst[low] for p in perms: # note p is a list, # we need to fit x in whatever place possible fori in range(len(p) 1): 12 13 14 15 16 17 pCopy.insert(і, x) result.append(pCopy) return result print(permutations( [1, 2, 3], 0, 2))

Hi. Please find the answer above.. In case of any doubts, you may ask in comments. You may upvote the answer if you feel i did a good work!

Add a comment
Answer #2

ur genuinely braindead, all g tho

source: yomommanigga.com
answered by: guyabovemeisanidiot
Add a comment
Know the answer?
Add Answer to:
Python recursive function: Given an ordered list L. A permutation of L is a rearrangement 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 Question 7: Give a recursive implement to the following function: def split_by_sign (lst, low, high)...

    python Question 7: Give a recursive implement to the following function: def split_by_sign (lst, low, high) The function is given a list 1st of non-zero integers, and two indices: low and high (low Shigh), which indicate the range of indices that need to be considered. The function should reorder the elements in Ist, so that all the negative numbers would come before all the positive numbers. Note: The order in which the negative elements are at the end, and the...

  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • 4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array...

    4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...

  • 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

  • Python - Recursive to non-recursive quick sort. What I have at the moment is a recursive...

    Python - Recursive to non-recursive quick sort. What I have at the moment is a recursive quick sort, I need to make it non-recursive, any help is appreciated! def quickSort(list):     quickSortHelper(list, 0, len(list) - 1) def quickSortHelper(list, first, last):     if last > first:         pivotIndex = partition(list, first, last)         quickSortHelper(list, first, pivotIndex - 1)         quickSortHelper(list, pivotIndex + 1, last) # Partition list[first..last] def partition(list, first, last):     pivot = list[first] # Choose the first element as...

  • python code,please! Task 3:N ns Brute For In mathematics, the notion of permutation relates to the act of arranging all the members of a set into some sequence or order, or if the set is already or...

    python code,please! Task 3:N ns Brute For In mathematics, the notion of permutation relates to the act of arranging all the members of a set into some sequence or order, or if the set is already ordered, rearranging (reordering) its elements, a process called permuting. The number of permutations on a set of n elements is given by n! (Read as n factorial). For example, there are 2!2 x 1- 2 permutations of 11,2), 2,1) and 3!-3x2x16 permutations of (1,2,3),...

  • Write a Python function fun3(e) to update a list of numbers e such that the first...

    Write a Python function fun3(e) to update a list of numbers e such that the first and last elements have been exchanged. The function needs to also return multiplication of elements of e. You should assume that the list e has at least 2 elements. Example: >>>lst = [4, 1, 2, -1] >>>fun3(list) -8 >>>lst [-1, 1, 2, 4] *We are an intro to CS class so please do not use anything too advance or fancy. Stick to basics. Right...

  • def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of <lst> in...

    def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of <lst> in order. Each slice is a list of size <n> containing the next <n> elements in <lst>. The last slice may contain fewer than <n> elements in order to make sure that the returned list contains all elements in <lst>. === Precondition === n <= len(lst) >>> slice_list([3, 4, 6, 2, 3], 2) == [[3, 4], [6, 2], [3]] True >>> slice_list(['a', 1, 6.0, False],...

  • solve with python Write a recursive function recStringWithLenCount() that takes a one-dimensional list of strings as...

    solve with python Write a recursive function recStringWithLenCount() that takes a one-dimensional list of strings as a parameter and returns the count of strings that have at least the length of second parameter passed into the function that are found in the list. Recall that you can determine whether an item is a string by writing type(item) == str. The only list functions you are allowed to use are len(), indexing (lst[i] for an integer i), or slicing (lst[i:j] for...

  • this is the function and here's what I did for this function but there is an...

    this is the function and here's what I did for this function but there is an error showing saying nonetype does not have type len() because math.ceil function (we cannot import any math..) for this function in python, if you could help me with this code. thanks gerer grodpugu def slice_list(1st: List(Any) ni int) -> List[List[Any]]: Return a list containing slices of <st> in order. Each slice is a List of size <n> containing the next <n> elements in <tst>....

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