This article example describes the full alignment algorithm for Python strings. Shared for your reference, as follows:
Title Description
Input a string, print out all the arrangements of the characters in the string in dictionary order. For example, if you enter the string abc, you can print out all the characters abc, acb, bac, bca, cab, and cba that can be arranged by the characters a, b, and c.
Input Description
Enter a string of characters up to 9 (there may be duplicated characters), the characters include only upper and lower case letters.
Note that there is a possibility of duplication, so judgment is required
Note the difference between the append method of a list and the + method of a list
The append method adds an element to the list.
+ method to add list after list
If append(list) is used, then all elements in the list are inserted as one item
swap
function swaps the new element with all the previous elements, returning a list with one element inserted for each swap, and therefore isappend
methodologies
def swap(self, newElem, Elem): result = [] listElem = list(Elem) (0, newElem) (''.join(listElem)) for i in range(1, len(listElem)): preList = listElem[:] # Watch this place listElem[0], listElem[i] = listElem[i], listElem[0] if listElem != preList: # Dealing with duplicates (''.join(listElem)) listElem[0], listElem[i] = listElem[i], listElem[0] return result
If the + method is used:
def swap(newElem, Elem): result = [] listElem = list(Elem) (0, newElem) #(''.join(listElem)) result += ''.join(listElem) for i in range(1, len(listElem)): preList = listElem[:] # Watch this place listElem[0], listElem[i] = listElem[i], listElem[0] if listElem != preList: # Dealing with duplicates #(''.join(listElem)) result += ''.join(listElem) listElem[0], listElem[i] = listElem[i], listElem[0] return result print(swap('1', '234')) >>>>['1', '2', '3', '4', '2', '1', '3', '4', '3', '2', '1', '4', '4', '2', '3', '1']
Recursively calling a function
The + sign should be used in this place, because it is added to the result list (with multiple elements) of each call, not to theappend
def recurtionPermutation(self, ss, index): result = [] if index == 0: (ss[0]) else: previousList = (ss, index - 1) newElem = ss[index] #print(previousList) for Elem in previousList: result += (newElem, Elem) # Here the return is an array, array plus array use +, array plus element use append sign return result
Sort by dictionary
Here I follow the bubbling dictionary sort, which is not really necessary, as comparing character sizes can be done directly with thesorted
function.
sorted
Functions are convenient and efficient
def BubbleSortByDic(self, result): for i in range(len(result)): for j in range(len(result) - 1, i, -1): if result[j] < result[i]: result[i], result[j] = result[j], result[i] return result
AC Code:
class Solution:
def swap(self, newElem, Elem): result = [] listElem = list(Elem) (0, newElem) (''.join(listElem)) for i in range(1, len(listElem)): preList = listElem[:] # Watch this place listElem[0], listElem[i] = listElem[i], listElem[0] if listElem != preList: # Dealing with duplicates (''.join(listElem)) listElem[0], listElem[i] = listElem[i], listElem[0] return result def recurtionPermutation(self, ss, index): result = [] if index == 0: (ss[0]) else: previousList = (ss, index - 1) newElem = ss[index] #print(previousList) for Elem in previousList: result += (newElem, Elem) # Here the return is an array, array plus array use +, array plus element use append sign return result # def BubbleSortByDic(self, result): # for i in range(len(result)): # for j in range(len(result) - 1, i, -1): # if result[j] < result[i]: # result[i], result[j] = result[j], result[i] # return result def Permutation(self, ss): # write code here if ss == '': return [] #return ((ss, len(ss) - 1)) return sorted((ss, len(ss) - 1)) print(Solution().Permutation('acdfb'))
Readers interested in more Python related content can check out this site's topic: theSummary of Python mathematical operations techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials》
I hope that what I have said in this article will help you in Python programming.