March 31 - Generate All Binary Strings from Given Pattern in Ruby
This guide walks through how to generate all binary strings that can be formed by replacing each wildcard character with a either a '0' or '1'.
Guide Tasks
  • Read Tutorial
  • Complete the Exercise

Summary

Implement a method that returns all potential permutations of a given pattern after replacing wildcard characters with '0' or '1'.

Exercise Description

Given a pattern, create a method that generates all possible binary strings by replacing question mark elements with '0' or '1'.

Examples

str = "1??0?101"
binary_swapper = BinarySwapper.new(binary_string: "1??0?101") => # '0', '1'
# => 
  [
     '10000101',
     '10001101',
     '10100101',
     '10101101',
     '11000101',
     '11001101',
     '11100101',
     '11101101'
   ]

binary_swapper = BinarySwapper.new(binary_string: "01??") => # '0', '1'
# =>
  [
    '0100',
    '0101',
    '0110',
    '0111'
  ]

Real World Usage

Although there is a fairly simple solution, this exercise forces you to generate a method that swaps out and replaces unknown values with a '0' or '1' and then returns a list of all the possible permutations of that certain binary string. This could come in handy when given much larger patterns or strings. You would just need to implement the same actions and it would produce the correct list of outcomes.

Test Cases

Code File