Question

In: Computer Science

in python how would create permuntations of a list with digits 0-9 using recursion with at...

in python how would create permuntations of a list with digits 0-9 using recursion with at most two for loops and no other lib allowed?

needs to return all perm length between 3-7 if possbile

Solutions

Expert Solution

def permutations(digits):

        def permutationsHelper(digits):
                if len(digits) == 1:
                        return [str(digits[0])]
                c = str(digits[0])
                perms = permutationsHelper(digits[1:])
                
                result = []
                for p in perms:
                        for i in range(len(p) + 1):
                                x = p[:i] + c + p[i:]
                                result.append(x)

                return result + perms

        result = permutationsHelper(digits)
        result = [x for x in result if 3 <= len(x) <= 7]
        return result

print(permutations([0, 1, 2, 5, 4]))
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
Write a program in python that implements quicksort, first using recursion and then without recursion.
Write a program in python that implements quicksort, first using recursion and then without recursion.
Python: How would I write a function using list comprehensions to list of integers into two...
Python: How would I write a function using list comprehensions to list of integers into two halves with the evens first, followed by odds? And on top of that, the numbers should be listed in their original order.
In python write a program that first creates a list with the integers 0 through 9...
In python write a program that first creates a list with the integers 0 through 9 and then traverses that list RECURSIVELY (no for/while loops allowed) and prints out the integers on the list. NOTE: creating the list does not have to be done recursively.
1a. An ATM requires a four-digit PIN, using the digits 0-9. How many PINs have no...
1a. An ATM requires a four-digit PIN, using the digits 0-9. How many PINs have no repeated digits? 1b. How many ways can president and vice president be determined in a club with twelve members? 1c. A security team visits 12 offices each night. How many different ways can the team order its visits? 1d. In a certain lottery you select seven distinct numbers from 1 through 39, where order makes no difference. How many different ways can you make...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
How to create a FTP server using python and TCP Ports How to create a FTP...
How to create a FTP server using python and TCP Ports How to create a FTP server using python and UDP Ports
Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list...
Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list of words, eliminates from the list of words the words in the file “stopwords_en.txt” and then a. Calculates the average occurrence of the words. Occurrence is the number of times a word is appearing in the text b. Calculates the longest word c. Calculates the average word length. This is based on the unique words: each word counts as one d. Create a bar...
PYTHON Find anagrams of a word using recursion. This a possible run of your program: Enter...
PYTHON Find anagrams of a word using recursion. This a possible run of your program: Enter a word or empty string to finish: poster The word poster has the following 6 anagrams: presto repost respot stoper topers tropes
Python: Implement edit_diff using recursion, which is a diff function that returns the minimum number of...
Python: Implement edit_diff using recursion, which is a diff function that returns the minimum number of edit operations needed to transform the start word into the goal word. There are three kinds of edit operations: Add a letter to start, Remove a letter from start, Substitute a letter in start for another. Each edit operation contributes 1 to the difference between two words. >>> big_limit = 10 >>> edit_diff("roses", "arose", big_limit) # roses -> aroses -> arose 2 >>> edit_diff("tesng",...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT