combinations

All possible combinations of selected character substitution in a string in ruby

可紊 提交于 2019-12-29 07:14:30
问题 I was wondering if there is a simple way to do every combination of selected character substitutions in ruby in a simple way. An example: string = "this is a test" subs = ['a'=>'@','i'=>'!','s'=>'$'] subs.combination.each { |c| string.gsub c } would yield "this is @ test" "th!s !s a test" "thi$ i$ a te$t" "th!s !s @ test" "thi$ i$ @ te$t" "th!$ !$ a te$t" "th!$ !$ @ te$t" Thanks for the help! 回答1: string = "this is a test" subs = ['a'=>'@','i'=>'!','s'=>'$'] subs = subs.first.map(&:to_a) 1

All possible combinations of selected character substitution in a string in ruby

允我心安 提交于 2019-12-29 07:14:16
问题 I was wondering if there is a simple way to do every combination of selected character substitutions in ruby in a simple way. An example: string = "this is a test" subs = ['a'=>'@','i'=>'!','s'=>'$'] subs.combination.each { |c| string.gsub c } would yield "this is @ test" "th!s !s a test" "thi$ i$ a te$t" "th!s !s @ test" "thi$ i$ @ te$t" "th!$ !$ a te$t" "th!$ !$ @ te$t" Thanks for the help! 回答1: string = "this is a test" subs = ['a'=>'@','i'=>'!','s'=>'$'] subs = subs.first.map(&:to_a) 1

Create combinations of a binary vector

夙愿已清 提交于 2019-12-29 07:07:00
问题 I would like to create all possible combinations of a binary vector made of a fixed number of 0 and 1. For example: dim(v)=5x1; n1=3; n0=2; In this case I'd like to have something like: 1,1,1,0,0 1,1,0,1,0 1,1,0,0,1 1,0,1,1,0 1,0,1,0,1 1,0,0,1,1 0,1,1,1,0 0,1,1,0,1 0,1,0,1,1 0,0,1,1,1 I found some help reading this post Create all possible combiations of 0,1, or 2 "1"s of a binary vector of length n but i would like to generate only the combinations I need avoiding any waste of space (I think

In Perl, how can I generate all possible combinations of a list?

别说谁变了你拦得住时间么 提交于 2019-12-29 06:38:07
问题 I have a file with a list, and a need to make a file that compares each line to the other. for example, my file has this: AAA BBB CCC DDD EEE I would like the final list to look like this: AAA BBB AAA CCC AAA DDD AAA EEE BBB CCC BBB DDD BBB EEE CCC DDD CCC EEE DDD EEE I am trying to do this in Perl, for this first time and am having a little trouble. I do know that you need to make an array, and then split it, but after that I am having some trouble. 回答1: Use Algorithm::Combinatorics. The

Iterating over an unknown number of nested loops in python

安稳与你 提交于 2019-12-29 05:46:28
问题 I have a variable number of user-defined lists, each containing words. For example, there may be three lists like the following: list1 = ["THE", "A"] list2 = ["ELEPHANT", "APPLE", "CAR"] list3 = ["WALKED", "DROVE", "SAT"] What I want is to iterate over every combination in each list, checking each against a dictionary of known words, to see which word-groupings are most like the dictionary. That means the iterations would be like: [ "THE ELEPHANT WALKED", "THE APPLE WALKED", "THE CAR WALKED",

Generating all possibly length n combinations of two items in python

风格不统一 提交于 2019-12-29 01:51:08
问题 I am trying to generate a list of length n from two possible items. e.g. One example could be, a list of length 4 comprising zeros or ones which would be 0000, 0001, 0010, 0100, 1000, 1001, etc. Thanks in advance, Jack 回答1: With itertools.product: In [1]: from itertools import product In [2]: list(product((0, 1), repeat=4)) Out[2]: [(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1

Algorithm that can create all combinations and all groups of those combinations

一笑奈何 提交于 2019-12-28 16:13:55
问题 Let's say I have a set of elements S = { 1, 2, 3, 4, 5, 6, 7, 8, 9 } I would like to create combinations of 3 and group them in a way such that no number appears in more than one combination. Here is an example: { {3, 7, 9}, {1, 2, 4}, {5, 6, 8} } The order of the numbers in the groups does not matter, nor does the order of the groups in the entire example. In short, I want every possible group combination from every possible combination in the original set, excluding the ones that have a

How to get all combinations from multiple arrays?

最后都变了- 提交于 2019-12-28 03:10:50
问题 Supposing I have these 3 arrays $array1 = array(1,2); $array2 = array(4,5); $array3 = array(7,8); I need this output 1 4 7 1 4 8 1 5 7 1 5 8 2 4 7 2 4 8 2 5 7 2 5 8 One of my problems is that my array myght vary from 3 to 15 different arrays and each myght be empty (I might add a 0 just not to be empty) or have many values. If I have an empty array I also need to count that as a valid column. These values will be used to fill up a database in a specific order. Is there any way I can do this?

Get all the combinations of N elements of multidimensional array

情到浓时终转凉″ 提交于 2019-12-28 03:10:36
问题 I'm trying to write an algorithm to get all the possible combinations of N elements inside a multi dimensional array of M elements. Something like: function getCombinations(arr, n){ ... } var arr = [ ["A"],["B","C"],["D","E"]]; var n = 2; getCombinations(arr,n); This should produce: [ ["A","B"],["A","C"],["A","D"],["A","E"], ["B","D"],["B","E"], ["C","D"],["C","E"] ] The number of elements inside the array may vary, the only thing set is the number of elements of the combinations. The order

No of numbers less than a given number with no repeating digits

柔情痞子 提交于 2019-12-26 06:11:09
问题 How can we find the number of numbers less than a given number with no repeating digits in it? For example the number of such numbers less than 100 is 90. (11, 22, 33,44, 55,66,77,88,99 have repeating digits so are excluded). Similarly for less than 1000, digits like 101, 110, 122, 202 etc have to be excluded. 回答1: Here is a way to make it quicker. Notice that there is a correlation between the number of digits in the max number and the solution (number of numbers which I will call NON ) 100