3

Given two coins, the number of outcomes will be 2^2 (two coins with only two possibilities(head(up) or tail(down)). Gives the following possible combinations:

 00   
 01
 10
 11

Where, 0 means head(up) and 1 means tail(down).

Here is the code to print the previous combinations:

 for n=1:2^2
 r(n) = dec2bin(n);
 end

What I want to do is to print all the possible combinations for the same two coins but with three different possibilities (head(up), tail(down) and in between (not up or down)) To give something like:

00
01
10
11
0B
B0
B1
1B
BB

Where, B means one of the two coins is In between (not up or down)

Any Ideas?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
M sedek
  • 81
  • 7
  • 1
    Please choose a language, not a list of them. – PaulMcKenzie Jul 21 '16 at 04:24
  • I am asking for any ideas using any of these 4 languages. I can use any of the four languages in case there is anyone around who knows the solution but using one of these four. Thanks for your notice. Hope you a nice day. – M sedek Jul 21 '16 at 05:19
  • In Matlab you can use [this](http://stackoverflow.com/questions/21895335/generate-a-matrix-containing-all-combinations-of-elements-taken-from-n-vectors) with input `vectors = {[0 1 2] [0 1 2]}` or `vectors = {'01B' '01B'}` – Luis Mendo Jul 21 '16 at 11:26
  • Thanks Luis, I found it very helpful. Thanks a lot. – M sedek Jul 22 '16 at 03:11

6 Answers6

3

Python solution:

from itertools import product

possible_values = '01B'
number_of_coins = 2

for result in product(possible_values, repeat=number_of_coins):
    print(''.join(result))

# Output:
# 00
# 01
# 0B
# 10
# 11
# 1B
# B0
# B1
# BB
user94559
  • 59,196
  • 6
  • 103
  • 103
1
from itertools import product

outcomes = ["".join(item) for item in list(product('01B', repeat=2))]
for outcome in outcomes:
    print(outcome)
#reusults:
00
01
0B
10
11
1B
B0
B1
BB
DurgaDatta
  • 3,952
  • 4
  • 27
  • 34
1

Matlab Solution: n is the amount of possible solutions. In your case 3 different once. k is the amount of sets. In your case 2 coins. pshould contain a matrix with the results.

n = 3; k = 2;
nk = nchoosek(1:n,k);
p=zeros(0,k);
for i=1:size(nk,1),
    pi = perms(nk(i,:));
    p = unique([p; pi],'rows');
end

For more solutions check: Possible combinations - order is important

Community
  • 1
  • 1
C.Colden
  • 627
  • 1
  • 8
  • 28
  • Thanks Colden, but this code doesn't provide all the combinations, it works fine but still one-third of the combinations is missing. – M sedek Jul 21 '16 at 07:15
1

I found several solutions for MATLAB. (That's not completely mine code, I found some parts and adapt it). Post it because @C.Colden' answer is not full. What you want to achieve is a permutations with repetitions. C.Colden shows it without repetitions. So you can go this way:

Solution 1:

a = [1 2 3]
n = length(a)
k = 2
for ii = k:-1:1
    temp = repmat(a,n^(k-ii),n^(ii-1));
    res(:,ii) = temp(:);
end

Result:

res =

 1     1
 1     2
 1     3
 2     1
 2     2
 2     3
 3     1
 3     2
 3     3

And interesting Solution 2 if you need it in string form:

dset={'12b','12b'};
n=numel(dset);
pt=[3:n,1,2];
r=cell(n,1);
[r{1:n}]=ndgrid(dset{:});
r=permute([r{:}],pt);
r=sortrows(reshape(r,[],n));

Result:

r =

11
12
1b
21
22
2b
b1
b2
bb
Community
  • 1
  • 1
Mikhail_Sam
  • 10,602
  • 11
  • 66
  • 102
  • 1
    Thanks a lot Mikhail, that was really helpful. The two solutions are working great and it is really good. Thanks a lot. – M sedek Jul 21 '16 at 09:22
0

Granted I am not familiar with the syntax of these other languages, the solutions to them look overly complicated.

It's just a simple doubly-nested for loop:

C++

#include <iostream>
using namespace std;

int main() 
{
    const char* ch = "01B";
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 3; ++j)
            cout << ch[i] << ch[j] << '\n';
    }
}

Output:

00
01
0B
10
11
1B
B0
B1
BB
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Hi Paul, Thanks for sharing the code. Yes, your solution is very simple but it only works in case you have only two coins (represented by two nested loops) which is exactly the case in this question, but the other codes provided by the gentles here have the possibility to print all the combination for any number of coins and it is simple as well. Hope you a nice day ! – M sedek Jul 22 '16 at 03:10
0

in c++

#include<iostream>
using namespace std;
int main(){
    string s="HT";
for(int i=0;i<2;i++){
    for(int j=0;j<2;j++){
        for(int k=0;k<2;k++){
            cout<<s[i]<<s[j]<<s[k]<<endl;
        }
    }
}


}
Tofain
  • 1
  • 2
    Welcome to Stack Overflow! Thanks for you attempt but this is not correct. Your answer can simulate 3 coins with 2 possibilities H / T. Please read the question again and use **Edit** to update your answer. Also, Code only answer is not considered as an answer, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post. You may reference [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) for guidance. – HardcoreGamer Aug 13 '21 at 02:14