Reputation: 1039
1I'm making a "Virtual Keyboard" for typing passwords in Java.
The password must have 6 numeric chars and each button has 2 possible values. So, there are 2 values possible for each pressed key.
For example: after pressing 6 buttons, I have an array of 6 objects, each one representing the possible values for each character of the password, respectively:
(1 , 5) for char1
(3 , 2) for char2
(7 , 4) for char3
(1 , 5) for char4
(9 , 0) for char5
(8 , 6) for char6
How can I make all combination of 6-digit-strings with these entries? (I guess that would be 64 combinations: 2ˆ6)
Example of valid combinations:
137198
537198
127198
537196
and so on...
Upvotes: 0
Views: 88
Reputation: 55
Just create every combination possible and check if each of them is the correct one
Upvotes: 1
Reputation: 9816
Given a sequence of n buttons, where each can assume 2 values, this problem reduces to counting from 0 to 2^n-1 since you can interpret each bit at position i as one of the two values that each button can assume. For n=3:
000 -> 137
001 -> 134
010 -> 127
011 -> 124
100 -> 537
...
So just count from 0 to 2^6 and you automatically get your solution.
This is the same way you would go about generating the 2^n subsets of a set of n elements.
Upvotes: 2