Python biedt directe methoden om permutaties en combinaties van een reeks te vinden. Deze methoden zijn aanwezig in het itertools-pakket.
Permutatie
Importeer eerst het itertools-pakket om de permutatiemethode in Python te implementeren. Deze methode neemt een lijst als invoer en retourneert een objectlijst met tupels die alle permutaties in een lijstvorm bevatten.
Python3
# A Python program to print all> # permutations using library function> from> itertools> import> permutations> # Get all permutations of [1, 2, 3]> perm> => permutations([> 1> ,> 2> ,> 3> ])> # Print the obtained permutations> for> i> in> list> (perm):> > print> (i)> |
>
mijn levende krekel
>
Uitgang:
(1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1)>
Tijdcomplexiteit: O(n!), waarbij n de lengte van de invoerlijst is. Dit komt omdat er n! permutaties van n elementen, en het programma genereert en drukt ze allemaal af.
Hulpruimte: O(n!), omdat het programma alle n moet opslaan! permutaties in het geheugen voordat u ze afdrukt. Concreet slaat de perm-variabele die is gemaakt door het aanroepen van permutaties([1, 2, 3]) alle n op! permutaties in het geheugen als een lijst.
Het genereert n! permutaties als de lengte van de invoerreeks n is.
Als je permutaties van lengte L wilt krijgen, implementeer deze dan op deze manier.
Python3
# A Python program to print all> # permutations of given length> from> itertools> import> permutations> # Get all permutations of length 2> # and length 2> perm> => permutations([> 1> ,> 2> ,> 3> ],> 2> )> # Print the obtained permutations> for> i> in> list> (perm):> > print> (i)> |
>
>
Uitgang:
(1, 2) (1, 3) (2, 1) (2, 3) (3, 1) (3, 2)>
De tijdscomplexiteit van dit programma is O(n^r), waarbij n de lengte is van de invoerarray en r de lengte is van de te genereren permutaties.
De ruimtecomplexiteit is ook O(n^r), aangezien alle permutaties vóór het afdrukken in het geheugen worden opgeslagen.
Het genereert nCr*r! permutaties als de lengte van de invoerreeks n is en de invoerparameter r is.
vergelijk met strings in Java
Combinatie
Deze methode neemt een lijst en een invoer r als invoer en retourneert een objectlijst van tupels die alle mogelijke combinaties van lengte r in een lijstvorm bevatten.
Python3
# A Python program to print all> # combinations of given length> from> itertools> import> combinations> # Get all combinations of [1, 2, 3]> # and length 2> comb> => combinations([> 1> ,> 2> ,> 3> ],> 2> )> # Print the obtained combinations> for> i> in> list> (comb):> > print> (i)> |
Java is leeg
>
>
Uitgang:
(1, 2) (1, 3) (2, 3)>
1. Combinaties worden uitgezonden in lexicografische sorteervolgorde van invoer. Dus als de invoerlijst is gesorteerd, worden de combinatietupels in gesorteerde volgorde geproduceerd.
Python3
# A Python program to print all> # combinations of a given length> from> itertools> import> combinations> # Get all combinations of [1, 2, 3]> # and length 2> comb> => combinations([> 1> ,> 2> ,> 3> ],> 2> )> # Print the obtained combinations> for> i> in> list> (comb):> > print> (i)> |
tekenreeks naar char java
>
>
Uitgang:
(1, 2) (1, 3) (2, 3)>
2. Elementen worden als uniek behandeld op basis van hun positie, niet op basis van hun waarde. Dus als de invoerelementen uniek zijn, zijn er geen herhaalde waarden in elke combinatie.
Python3
# A Python program to print all combinations> # of given length with unsorted input.> from> itertools> import> combinations> # Get all combinations of [2, 1, 3]> # and length 2> comb> => combinations([> 2> ,> 1> ,> 3> ],> 2> )> # Print the obtained combinations> for> i> in> list> (comb):> > print> (i)> |
>
>
Uitgang:
(2, 1) (2, 3) (1, 3)>
3. Als we een combinatie van hetzelfde element met hetzelfde element willen maken, gebruiken we combinaties_met_vervanging.
Python3
int omzetten naar dubbele java
# A Python program to print all combinations> # with an element-to-itself combination is> # also included> from> itertools> import> combinations_with_replacement> # Get all combinations of [1, 2, 3] and length 2> comb> => combinations_with_replacement([> 1> ,> 2> ,> 3> ],> 2> )> # Print the obtained combinations> for> i> in> list> (comb):> > print> (i)> |
>
>
Uitgang:
(1, 1) (1, 2) (1, 3) (2, 2) (2, 3) (3, 3)>