combinatorics

Sum-of-Product of subsets

送分小仙女□ 提交于 2019-12-30 03:14:26
问题 Is there a name for this operation? And: is there a closed-form expression? For a given set of n elements, and value k between 1 and n, Take all subsets (combinations) of k items Find the product of each subset Find the sum of all those products I can express this in Python, and do the calculation pretty easily: from operator import mul from itertools import combinations from functools import reduce def sum_of_product_of_subsets(list1, k): val = 0 for subset in combinations(list1, k): val +=

How can I print out all possible letter combinations a given phone number can represent?

心不动则不痛 提交于 2019-12-29 10:08:10
问题 I just tried for my first programming interview and one of the questions was to write a program that given a 7 digit telephone number, could print all possible combinations of letters that each number could represent. A second part of the question was like what about if this would have been a 12 digit international number? How would that effect your design. I don't have the code that I wrote in the interview, but I got the impression he wasn't happy with it. What is the best way to do this?

How can I print out all possible letter combinations a given phone number can represent?

夙愿已清 提交于 2019-12-29 10:07:59
问题 I just tried for my first programming interview and one of the questions was to write a program that given a 7 digit telephone number, could print all possible combinations of letters that each number could represent. A second part of the question was like what about if this would have been a 12 digit international number? How would that effect your design. I don't have the code that I wrote in the interview, but I got the impression he wasn't happy with it. What is the best way to do this?

Google Interview: Arrangement of Blocks

泄露秘密 提交于 2019-12-29 10:06:23
问题 You are given N blocks of height 1…N. In how many ways can you arrange these blocks in a row such that when viewed from left you see only L blocks (rest are hidden by taller blocks) and when seen from right you see only R blocks? Example given N=3, L=2, R=1 there is only one arrangement {2, 1, 3} while for N=3, L=2, R=2 there are two ways {1, 3, 2} and {2, 3, 1} . How should we solve this problem by programming? Any efficient ways? 回答1: This is a counting problem, not a construction problem,

knight's tour efficient solution

為{幸葍}努か 提交于 2019-12-29 08:47:14
问题 I have build a code in prolog to find a series of legal moves in which the knight lands on each square of the chessboard(8x8) exactly once. I have used a logic like below: There 8 types of knight moves: right 1 down 2 left 1 down 2 right 2 down 1 left 2 down 1 right 1 up 2 left 1 up 2 right 2 up 1 left 2 up 1 right 1 down 2 moves: move(X,Y) :- C_X is X mod 8, R_X is X // 8, C_Y is C_X + 1, % 1 right C_Y < 8, R_Y is R_X + 2, % 2 down R_Y < 8, Y is R_Y * 8 + C_Y, Y >= 0, X >= 0, X < 64, Y < 64.

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

Generating all unique pair permutations

牧云@^-^@ 提交于 2019-12-28 17:56:47
问题 I need to generate all possible pairings, but with the constraint that a particular pairing only occurs once in the results. So for example: import itertools for perm in itertools.permutations(range(9)): print zip(perm[::2], perm[1::2]) generates all possible two-paired permutations; here's a small subset of the output: ... [(8, 4), (7, 6), (5, 3), (0, 2)] [(8, 4), (7, 6), (5, 3), (1, 0)] [(8, 4), (7, 6), (5, 3), (1, 2)] [(8, 4), (7, 6), (5, 3), (2, 0)] [(8, 4), (7, 6), (5, 3), (2, 1)] [(8, 5

All possible combinations of elements from different bins (one element from every bin) [duplicate]

安稳与你 提交于 2019-12-25 20:00:32
问题 This question already has answers here : Generate list of all possible combinations of elements of vector (6 answers) Closed 3 years ago . I have a list, where each element is a set of numbers. Lengths of all sets are different: a <- list(1,c(2,3),c(4,5,6)) #> a #[[1]] #[1] 1 #[[2]] #[1] 2 3 #[[3]] #[1] 4 5 6 I'd like to get all possible combinations of one element from each set. In this example it should be: 1 2 4, 1 2 5, 1 2 6, 1 3 4, 1 3 5, 1 3 6 I feel that some combination of *apply

Print all permutation of length k that can be formed from a set of n characters?

可紊 提交于 2019-12-25 19:59:21
问题 public class PrintStrLenK { public static void main(String[] args){ int k = 2; char[] set = {'0', '1'}; char[] str = new char[k]; generate(k, set, str, 0); } static void generate(int k, char[] set, char[] str, int index){ if (index == k){ System.out.println(new String(str)); } else { for (int i = 0; i < set.length; i++){ str[index] = set[i]; generate(k, set, str, index + 1); } } } } I found this code, the problem is that I was asked to have just one char change between permutations Output: 00

No of ways of placing k distinguishable items into n distinguishable boxes

杀马特。学长 韩版系。学妹 提交于 2019-12-25 07:12:45
问题 Distinguishable objects into distinguishable boxes It is very similar to this question posted. I'm trying to get python code for this question. Note although it is similar there is a key difference. i.e. A bucket can be empty, while the other buckets contain all the items. Even this case will be considered as a separate case. for example: Consider I have 3 items A,B,C and 3 buckets B1, B2, B3 The table below will show the expected result: B1 B2 B3 (A,B,C) () () () (A,B,C) () () () (A,B,C) (A)