![]() So if weâre on 'b' and weâve gotten the list, weâd add 'b' to those, resulting in 'bac' and 'bca', each of which weâd add to our final results. Once you have the list from step 2, add each element from that list to the character from the initial string, and append the result to our list of final results.So, for example, if the current iteration is on 'b', weâd want to find all the permutations of the string 'ac'. For each character in the initial string, set aside that character and get a list of all permutations of the string thatâs left.Iterate through the initial string â e.g., âabcâ.Put in English, we want to do the following: Now, immediately this should look like a recursive problem. Write a function permute such that: permute('abc') â """ if str = None or start < 0 : return if start = len ( str ) - 1 : # Output current permutation string after Full Permutation print ( ''. I += 1 return False def permutation ( str, start ) : """ While i ![]() While i < len ( str ) : # Swap characters where start and i are located :param start: The first character subscript of the substring to be sorted Fully arrange the characters in the string (2) Recursion must have an end condition, otherwise the program will fall into an endless loop Ĭode implementation: #!/usr/bin/env python3 # -*- coding: utf-8 -*- # : 9:49 # : buu # Påharm # : def swap ( str, i, j ) : # Exchange Character Array Subscripted Characters corresponding to i and j (1) Reduce the size of the problem gradually and solve the sub-problem in the same way When using recursive solutions, be aware that: When the string is fully aligned, it can be solved recursively. (3) Because the exchange of a and b in the second step destroys the original order of the strings, it is necessary to exchange a and b again to restore the original order, then exchange the first character with the third character (exchange A and c), then fix the first character c, and complete the arrangement of the following two characters a and b. (2) Exchange the first character with the subsequent characters, that is, exchange a and b, and then arrange the latter two characters a and c in full order (1) Fix the first character a first, then arrange the second two characters b and c in full order This paper takes the string abc as an example to introduce how to arrange the strings in full order. ![]() Design a program that requires all permutations of a string to be output when a string is entered.įor example, the input string a B C requires the output of all the strings a B c, a C b, B a c, B C a, C a b, C B a that can be arranged by the letters a, b, C.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |