DEV Community

Discussion on: Daily Challenge #299 - Time to Grille!

Collapse
 
biancapower profile image
Bianca Power

ruby

def grille(message, code)
  output = ""

  # convert code to binary
  binary_code = code.to_s(2)

  # prepend the appropriate number of zeros to match length of message
  while message.length > binary_code.length
    binary_code.prepend('0')
  end

  # combine messages and binary_code into a hash
  arrays_hash = Hash[message.split("").zip(binary_code.split(""))]

  # if the value is 1, add the key to the output string
  arrays_hash.each do |k, v|
    if v == "1"
      output << k
    end
  end

  return output
end
Enter fullscreen mode Exit fullscreen mode