Skip to content

Instantly share code, notes, and snippets.

@ArionHardison
Forked from pixelcandy/abba-babba.md
Last active February 13, 2020 12:57
Show Gist options
  • Select an option

  • Save ArionHardison/f0760ca6a72aed72ce7c2a6721786008 to your computer and use it in GitHub Desktop.

Select an option

Save ArionHardison/f0760ca6a72aed72ce7c2a6721786008 to your computer and use it in GitHub Desktop.
Brief Ruby Challenge

Instructions

  • Fork this gist.
  • Please complete the quick challenge below using Ruby, uploading additional files if necessary.
  • Reply back to the email you were sent with the link to your completed gist.

Abba Babba Binary Challenge

This challenge should take 10-15 minutes to complete.

Instructions:

Write a method that:

  • takes an integer as input
  • converts the number to binary
  • swaps any 1's for A's and swaps any 0's for B's and returns the output

Example: The binary representation of 12345 is 11000000111001 so your method should return AABBBBBBAAABBA

@ArionHardison
Copy link
Author

ArionHardison commented Feb 13, 2020

  sig { params(int: Integer, opts: Hash).returns(String) }
  def replace_chars(int = 12345, opts = {"1"=>"A", "0"=>"B"})
    int.to_s(2)
       .gsub(/\w/, opts)
  end

@ArionHardison
Copy link
Author

ArionHardison commented Feb 13, 2020

interface ReplaceCharParams {
  int: number;
  opts: {
    [key: string]: string;
  }
}

const replaceChars = (params: ReplaceCharParams): string => {
  const {int, opts} = params;
  const intAsBinary = int.toString(2).replace(/\w/g, (matched) => opts[matched]);

  return intAsBinary;
}

const params: ReplaceCharParams = {
  int: 12345, 
  opts: {'1': 'A', '0': 'B'}
}

replaceChars(params);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment