0

Ruby - What exactly "combination" does?

c = [1,2,3,4].combination(4) puts c.to_a

7th Jan 2020, 6:15 PM
Paolo De Nictolis
Paolo De Nictolis - avatar
3 Answers
+ 11
Paolo De Nictolis Have you ever heard about permutation and combination in mathematics. Same that combination is used here which create combination of defined side as you have defines 4 as size here and all 4 elements can make one combination so for understanding the concept try to create combination of size 2 or 3 like this way. a = [1, 2, 3, 4] puts "combination a : #{a.combination(2).to_a}\n\n" b = [1, 2, 3, 4] puts "combination b : #{b. combination(3).to_a}\n\n" c = [1,2,3,4].combination(4) puts c.to_a d = [1, 2, 3, 4].combination(3) puts d.to_a https://code.sololearn.com/c2y8wS8w559K/?ref=app
7th Jan 2020, 6:32 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 9
Paolo De Nictolis what you are assuming is permutations not combination. Here is an example of permutations which are 24 for 4 length list. Which is factorial (n!) Where n is the length or size of list https://code.sololearn.com/cbCY86XcFJ7C/?ref=app
8th Jan 2020, 6:47 AM
GAWEN STEASY
GAWEN STEASY - avatar
0
Just about I heard about.... There are 24 combinations of four elements!
7th Jan 2020, 9:37 PM
Paolo De Nictolis
Paolo De Nictolis - avatar