permutation

Understanding Peter Norvig's permutation solution in PAIP

為{幸葍}努か 提交于 2021-02-20 19:00:28
问题 Peter Norvig's PAIP book contains this code as a solution to the permutation problem (some sections are removed for brevity) (defun permutations (bag) ;; If the input is nil, there is only one permutation: ;; nil itself (if (null bag) '(()) ;; Otherwise, take an element, e, out of the bag. ;; Generate all permutations of the remaining elements, ;; And add e to the front of each of these. ;; Do this for all possible e to generate all permutations. (mapcan #'(lambda (e) (mapcar #'(lambda (p)

Python permutations including substrings

邮差的信 提交于 2021-02-18 12:11:32
问题 I've come across this post: How to generate all permutations of a list in Python But I require something more, namely all of the permutations of a string as well as all the permutations of all the substrings. I know it's a big number, but is it possible? 回答1: import itertools def all_permutations_substrings(a_str): return ( ''.join(item) for length in xrange(1, len(a_str)+1) for item in itertools.permutations(a_str, length)) Note, however, that this is true permutations - as in, hello will

Optimizing (minimizing) the number of lines in file: an optimization problem in line with permutations and agenda scheduling

旧巷老猫 提交于 2021-02-18 09:55:12
问题 I have a calendar, typically a csv file containing a number of lines. Each line corresponds to an individual and is a sequence of consecutive values '0' and '1' where '0' refers to an empty time slot and '1' to an occupied slot. There cannot be two separated sequences in a line ( e.g. two sequences of '1' separated by a '0' such as '1,1,1,0,1,1,1,1'). The problem is to minimize the number of lines by combining the individuals and resolving the collisions between time slots. Note the time

Rank and unrank permutations with just one cycle

三世轮回 提交于 2021-02-17 02:04:55
问题 I want to rank and unrank permutations with one cycle in lexicographical order with a given len. A permutation with one cycles is where you can visit in this cycle each element. p:= (2,3,1) is a permutation with one cycle. Has rank 1. p:= (3,1,2) has 1 cycle too, but rank 2, because the permutation is lexicographical greater the frist so it becomes a greater rank. p:= (1,2,3) is a permutation with 3 cycles. (1),(2),(3) How can I efficently rank (permutation with one cycle to rank) and unrank

How does this complex recursive code work?

半腔热情 提交于 2021-02-15 06:57:14
问题 I am trying to understand this recursion. I know how recursion works in factorial function but when it gets to this complex recursion like this I am confused. The most confusing part to me is this code str.split('').map( (char, i) => permutations( str.substr(0, i) + str.substr(i + 1) )map( p => char + p)) First, with "abc" , say, it will split into ["a","b","c"] and go through the map function, then go through the second map function to wrap each return with a , b , c , respectively. However,

How does this complex recursive code work?

为君一笑 提交于 2021-02-15 06:55:35
问题 I am trying to understand this recursion. I know how recursion works in factorial function but when it gets to this complex recursion like this I am confused. The most confusing part to me is this code str.split('').map( (char, i) => permutations( str.substr(0, i) + str.substr(i + 1) )map( p => char + p)) First, with "abc" , say, it will split into ["a","b","c"] and go through the map function, then go through the second map function to wrap each return with a , b , c , respectively. However,

Finding all possible permutations of the characters in a set of strings using recursion

别说谁变了你拦得住时间么 提交于 2021-02-13 17:31:06
问题 I have this set of (Greek) strings: ἸἼΙἹἽ, ῇηἤήῃὴῆἡἠἢᾖἥἣῄἦᾗᾐἧᾔᾑ, σς, οὸόὀὄὅὂ, ὺὖυῦύὐὑὔΰϋὕὗὓὒῢ I'd like to find all possible permutations of the characters in these 5 strings. For example, Ἰῇσοὺ, Ἰῇσοὖ, Ἰῇσου, etc. I know it should involve recursion since the number of strings is not fixed but I'm a beginner and I'm completely dumbfounded by recursion. I did the following in Python and it does give me all combinations of the characters in each string. But I need the 'ἸἼΙἹἽ' to always come

Finding all possible permutations of the characters in a set of strings using recursion

微笑、不失礼 提交于 2021-02-13 17:28:47
问题 I have this set of (Greek) strings: ἸἼΙἹἽ, ῇηἤήῃὴῆἡἠἢᾖἥἣῄἦᾗᾐἧᾔᾑ, σς, οὸόὀὄὅὂ, ὺὖυῦύὐὑὔΰϋὕὗὓὒῢ I'd like to find all possible permutations of the characters in these 5 strings. For example, Ἰῇσοὺ, Ἰῇσοὖ, Ἰῇσου, etc. I know it should involve recursion since the number of strings is not fixed but I'm a beginner and I'm completely dumbfounded by recursion. I did the following in Python and it does give me all combinations of the characters in each string. But I need the 'ἸἼΙἹἽ' to always come

How to find all the possible permutations of a matrix in R?

喜欢而已 提交于 2021-02-10 23:20:01
问题 I have a matrix, for example, 5x5. [,1] [,2] [,3] [,4] [,5] [1,] 22 -2 -2 -2 2 [2,] -2 22 2 2 2 [3,] -2 2 22 2 2 [4,] -2 2 2 22 2 [5,] 2 2 2 2 22. As you can see, the matrix is symmetric. Above the main diagonal, there are 4+3+2+1=10 positions, and I find via combn all the possible (permutation) matrices, which have (-2) 3 times in these 10 positions. That means 10!/3!*7!=120 matrices. But some of them are equivalent. So,my problem is how to find the non-equivalent matrices from the 120. I am

How to find all the possible permutations of a matrix in R?

天大地大妈咪最大 提交于 2021-02-10 23:19:03
问题 I have a matrix, for example, 5x5. [,1] [,2] [,3] [,4] [,5] [1,] 22 -2 -2 -2 2 [2,] -2 22 2 2 2 [3,] -2 2 22 2 2 [4,] -2 2 2 22 2 [5,] 2 2 2 2 22. As you can see, the matrix is symmetric. Above the main diagonal, there are 4+3+2+1=10 positions, and I find via combn all the possible (permutation) matrices, which have (-2) 3 times in these 10 positions. That means 10!/3!*7!=120 matrices. But some of them are equivalent. So,my problem is how to find the non-equivalent matrices from the 120. I am