permutation

How to calculate permutations of group labels with R?

落爺英雄遲暮 提交于 2021-01-27 19:22:29
问题 Given a vector like: labels <- c(1,2,3,3,3) How to get all possible group relabelings? For this example: 1,2,3,3,3 1,3,2,2,2 2,1,3,3,3 2,3,1,1,1 3,1,2,2,2 3,2,1,1,1 I have been looking at the permute package but I don't see how to apply it to this case. 回答1: How about this solution labels <- c(1,2,3,3,3) library(data.table) a <- do.call(cbind, combinat::permn(unique(labels))) data.table(a)[,lapply(.SD, function(x)x[labels]),] # V1 V2 V3 V4 V5 V6 #1: 1 1 3 3 2 2 #2: 2 3 1 2 3 1 #3: 3 2 2 1 1 3

String permutations using recursion in Java

六月ゝ 毕业季﹏ 提交于 2021-01-27 06:45:31
问题 I came across THIS post which tried very hard to explain the recursive solution to print all string. public class Main { private static void permutation(String prefix, String str){ int n = str.length(); if (n == 0) System.out.println(prefix); else { for (int i = 0; i < n; i++) permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1)); } } public static void main(String[] args) { permutation("", "ABCD"); } } But still I am not able to get the part when we start popping off

Heap's algorithm time complexity

陌路散爱 提交于 2021-01-27 06:01:55
问题 can anyone tell me what exactly is the time complexity of this Heap's algorithm shown in wikipedia, https://en.wikipedia.org/wiki/Heap%27s_algorithm ? I searched several websites and the answers are all vague, some of them say the time complexity is O(N!) some of them say it's O(NlogN). Which one is the correct answer? And why? Thank you. 回答1: There are N ! permutations in all and generating all of them requires Θ( N !) time and Θ(N) space. In other words, each permutation requires amortised

Heap's algorithm time complexity

五迷三道 提交于 2021-01-27 06:01:40
问题 can anyone tell me what exactly is the time complexity of this Heap's algorithm shown in wikipedia, https://en.wikipedia.org/wiki/Heap%27s_algorithm ? I searched several websites and the answers are all vague, some of them say the time complexity is O(N!) some of them say it's O(NlogN). Which one is the correct answer? And why? Thank you. 回答1: There are N ! permutations in all and generating all of them requires Θ( N !) time and Θ(N) space. In other words, each permutation requires amortised

Heap's algorithm time complexity

≯℡__Kan透↙ 提交于 2021-01-27 06:01:34
问题 can anyone tell me what exactly is the time complexity of this Heap's algorithm shown in wikipedia, https://en.wikipedia.org/wiki/Heap%27s_algorithm ? I searched several websites and the answers are all vague, some of them say the time complexity is O(N!) some of them say it's O(NlogN). Which one is the correct answer? And why? Thank you. 回答1: There are N ! permutations in all and generating all of them requires Θ( N !) time and Θ(N) space. In other words, each permutation requires amortised

Python every possible combination of a string

不羁的心 提交于 2020-12-29 08:44:38
问题 Hi so I'm working with python and I'm trying to write a method where given a string, it would find every combination of that string and append it to a list. I'll give the string and show the outcome that I want. string: x = 'god' outcome: lst = ['g', 'o', 'd', 'go', 'gd', 'og', 'od', 'dg', 'do', 'god', 'gdo', 'ogd', 'odg', 'dgo', 'dog'] A letter can only be used by the number of times it appears on the string given, so if our string is 'god' , 'gg' or 'goo' etc. cannot be appended. If this

Python every possible combination of a string

纵然是瞬间 提交于 2020-12-29 08:43:03
问题 Hi so I'm working with python and I'm trying to write a method where given a string, it would find every combination of that string and append it to a list. I'll give the string and show the outcome that I want. string: x = 'god' outcome: lst = ['g', 'o', 'd', 'go', 'gd', 'og', 'od', 'dg', 'do', 'god', 'gdo', 'ogd', 'odg', 'dgo', 'dog'] A letter can only be used by the number of times it appears on the string given, so if our string is 'god' , 'gg' or 'goo' etc. cannot be appended. If this

Using permutations to find the next biggest number

杀马特。学长 韩版系。学妹 提交于 2020-12-15 06:03:14
问题 Create a function that takes a positive integer and returns the next bigger number that can be formed by rearranging its digits. To accomplish this I used the following code: Logic used: Write a script do all the permutations of the number e.g. 123 -> 132,321,231,... select the largest number from the possible combinations if that number is bigger than original number provided it returns that number - if not it returns -1. Code Block: import itertools def next_bigger(n): # [int(x) for x in

Using permutations to find the next biggest number

╄→гoц情女王★ 提交于 2020-12-15 06:02:47
问题 Create a function that takes a positive integer and returns the next bigger number that can be formed by rearranging its digits. To accomplish this I used the following code: Logic used: Write a script do all the permutations of the number e.g. 123 -> 132,321,231,... select the largest number from the possible combinations if that number is bigger than original number provided it returns that number - if not it returns -1. Code Block: import itertools def next_bigger(n): # [int(x) for x in