PROGRAMMING INTERVIEWS: Print all tuple combinations for a given tuple
Question:
Given a Tuple for eg. (a, b, c).
Output : (*, *, *), (*, *, c), (*, b, *), (*, b, c), (a, *, *), (a, *, c), (a, b, *), (a, b, c)
Answer:
This looks that a character in the tuple can either be '*'' or itself. So we simply need to choose one character at a time, print combination on remaining substring and add them with '*' and earlier chosen character.
If we notice, it will be like below in reality:
Question:
Given a Tuple for eg. (a, b, c).
Output : (*, *, *), (*, *, c), (*, b, *), (*, b, c), (a, *, *), (a, *, c), (a, b, *), (a, b, c)
Answer:
This looks that a character in the tuple can either be '*'' or itself. So we simply need to choose one character at a time, print combination on remaining substring and add them with '*' and earlier chosen character.
If we notice, it will be like below in reality:
- def pattern(prefix, input, len)
- # boundary condition
- if (prefix.length == len)
- puts prefix
- return
- end
- pattern(prefix+input[0], input[1..-1], len)
- pattern(prefix+'*', input[1..-1], len)
- end
- # for test, I took only abc, you can take the input as argument
- input = "abc"
- pattern("", input, input.length)