Given a Tuple for eg. (a, b, c).
Output : (*, *, *), (*, *, c), (*, b, *), (*, b, c), (a, *, *), (a, *, c), (a, b, *), (a, b, c)

This question can also be asked as print all subsets of a given set.

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:

This can be simply achieved by recursion. We just need to identify correct boundary conditions. Below is the code for the same. In this code, I have taken string in place of tuple but the logic stays same.

def pattern(prefix, input, len)
  # boundary condition
  if (prefix.length == len)
    puts prefix
  pattern(prefix+input[0], input[1..-1], len)
  pattern(prefix+'*', input[1..-1], len)

# for test, I took only abc, you can take the input as argument
input = "abc"
pattern("", input, input.length)

Subscribe - To get an automatic feed of all future posts subscribe here, or to receive them via email go here and enter your email address in the box. You can also like us on facebook and follow me on Twitter @akashag1001.