Skip to main content
deleted 1 character in body
Source Link
Jordan
  • 10.1k
  • 1
  • 31
  • 47

Ruby -p, 50 bytes

gsub(/./){"%07b"%$&.ord}
gsub(/./){$`=~/#$&$/?0:1}

Try it online!

Explanation

First line, same as Value Ink's answerValue Ink's answer:

gsub(/./){       $&    }   # Replace each character $&…
                   .ord    # …with its ASCII code…
                %          # …formatted as…
          "%07b"           # …binary digits padded to 7 places.

Second line:

gsub(/./){      $&      }  # Replace each character $&…
          $`               # …if the text to its left…
            =~             # …matches…
              /#  $/       # …the Regexp /c$/ where "c" is the character…
                    ?0:1   # …with 0, or 1 otherwise.

In Ruby you can use interpolation in Regexp literals, e.g. /Hello #{name}/, but withand for variables that start with $ or @ you can omit the curly braces, so if e.g. $& is "0" then the grawlixy /#$&$/ becomes /0$/.

Ruby -p, 50 bytes

gsub(/./){"%07b"%$&.ord}
gsub(/./){$`=~/#$&$/?0:1}

Try it online!

Explanation

First line, same as Value Ink's answer:

gsub(/./){       $&    }   # Replace each character $&…
                   .ord    # …with its ASCII code…
                %          # …formatted as…
          "%07b"           # …binary digits padded to 7 places.

Second line:

gsub(/./){      $&      }  # Replace each character $&…
          $`               # …if the text to its left…
            =~             # …matches…
              /#  $/       # …the Regexp /c$/ where "c" is the character…
                    ?0:1   # …with 0, or 1 otherwise.

In Ruby you can use interpolation in Regexp literals, e.g. /Hello #{name}/, but with variables that start with $ or @ you can omit the curly braces, so if e.g. $& is "0" then the grawlixy /#$&$/ becomes /0$/.

Ruby -p, 50 bytes

gsub(/./){"%07b"%$&.ord}
gsub(/./){$`=~/#$&$/?0:1}

Try it online!

Explanation

First line, same as Value Ink's answer:

gsub(/./){       $&    }   # Replace each character $&…
                   .ord    # …with its ASCII code…
                %          # …formatted as…
          "%07b"           # …binary digits padded to 7 places.

Second line:

gsub(/./){      $&      }  # Replace each character $&…
          $`               # …if the text to its left…
            =~             # …matches…
              /#  $/       # …the Regexp /c$/ where "c" is the character…
                    ?0:1   # …with 0, or 1 otherwise.

In Ruby you can use interpolation in Regexp literals, e.g. /Hello #{name}/, and for variables that start with $ or @ you can omit the curly braces, so if e.g. $& is "0" then the grawlixy /#$&$/ becomes /0$/.

deleted 7 characters in body
Source Link
Jordan
  • 10.1k
  • 1
  • 31
  • 47

Ruby -p, 50 bytes

gsub(/./){"%07b"%$&.ord}
gsub(/./){$`=~/#$&$/?0:1}

Try it online!

Explanation

First line, same as Value Ink's answer:

gsub(/./){          $&    }   # Replace each character $&…
                 $&  .ord    # …with its ASCII code…
                %          # …formatted as…
          "%07b"           # …binary digits padded to 7 places.
                       }

Second line:

gsub(/./){         $&      }  # Replace each character…character $&…
          $`               # …if the text to its left…
            =~             # …matches…
              /#$&$#  $/       # …the Regexp /c$/ where "c" is the character…
                    ?0:1   # …with 0, or 1 otherwise.
                        }

In Ruby you can use interpolation in Regexp literals, e.g. /Hello #{name}/, but with variables that start with $ or @ you can omit the curly braces, so if e.g. $& is "0" then the grawlixy /#$&$/ becomes /0$/.

Ruby -p, 50 bytes

gsub(/./){"%07b"%$&.ord}
gsub(/./){$`=~/#$&$/?0:1}

Try it online!

Explanation

First line, same as Value Ink's answer:

gsub(/./){                 # Replace each character $&…
                 $&.ord    # …with its ASCII code…
                %          # …formatted as…
          "%07b"           # …binary digits padded to 7 places.
                       }

Second line:

gsub(/./){                 # Replace each character…
          $`               # …if the text to its left…
            =~             # …matches…
              /#$&$/       # …the Regexp /c$/ where "c" is the character…
                    ?0:1   # …with 0, or 1 otherwise.
                        }

Ruby -p, 50 bytes

gsub(/./){"%07b"%$&.ord}
gsub(/./){$`=~/#$&$/?0:1}

Try it online!

Explanation

First line, same as Value Ink's answer:

gsub(/./){       $&    }   # Replace each character $&…
                   .ord    # …with its ASCII code…
                %          # …formatted as…
          "%07b"           # …binary digits padded to 7 places.

Second line:

gsub(/./){      $&      }  # Replace each character $&…
          $`               # …if the text to its left…
            =~             # …matches…
              /#  $/       # …the Regexp /c$/ where "c" is the character…
                    ?0:1   # …with 0, or 1 otherwise.

In Ruby you can use interpolation in Regexp literals, e.g. /Hello #{name}/, but with variables that start with $ or @ you can omit the curly braces, so if e.g. $& is "0" then the grawlixy /#$&$/ becomes /0$/.

deleted 7 characters in body
Source Link
Jordan
  • 10.1k
  • 1
  • 31
  • 47

Ruby -p, 50 bytes

  
gsub(/./){"%07b"%$&.ord}
gsub(/./){$`=~/#$&$/?0:1}

Try it online!

Explanation

The firstFirst line is the, same as Value Ink's answer. It replaces each character with the corresponding 7 binary digits:

gsub(/./) { "%07b" % $&.ord }

In Ruby, $& holds the text of the most recent Regexp match, so $&.ord is the ASCII value of the matched character. % is an infix alias of String#sprintf and %07b is a format string that indicates formatting as binary padded to 7 digits.

gsub(/./){                 # Replace each character $&…
                 $&.ord    # …with its ASCII code…
                %          # …formatted as…
          "%07b"           # …binary digits padded to 7 places.
                       }

The secondSecond line checks each binary digit against the previous digit and replaces it if it differs:

gsub(/./) { $` =~ /#$&$/ ? 0 : 1 }

$` holds the text to the left of the most recent match. In the Regexp /#$&$/, #$& is equivalent to the interpolation #{$&}, so e.g. if $& is "1", the resulting Regexp is /1$/. The match operator =~ returns the position of the match or nil if there's no match, so $` =~ /#$&$/ ? 0 : 1 evaluates to 0 if the character is the same as the last character to its left, and 1 otherwise.

gsub(/./){                 # Replace each character…
          $`               # …if the text to its left…
            =~             # …matches…
              /#$&$/       # …the Regexp /c$/ where "c" is the character…
                    ?0:1   # …with 0, or 1 otherwise.
                        }

Ruby -p, 50 bytes

 
gsub(/./){"%07b"%$&.ord}
gsub(/./){$`=~/#$&$/?0:1}

Try it online!

Explanation

The first line is the same as Value Ink's answer. It replaces each character with the corresponding 7 binary digits:

gsub(/./) { "%07b" % $&.ord }

In Ruby, $& holds the text of the most recent Regexp match, so $&.ord is the ASCII value of the matched character. % is an infix alias of String#sprintf and %07b is a format string that indicates formatting as binary padded to 7 digits.

The second line checks each binary digit against the previous digit and replaces it if it differs:

gsub(/./) { $` =~ /#$&$/ ? 0 : 1 }

$` holds the text to the left of the most recent match. In the Regexp /#$&$/, #$& is equivalent to the interpolation #{$&}, so e.g. if $& is "1", the resulting Regexp is /1$/. The match operator =~ returns the position of the match or nil if there's no match, so $` =~ /#$&$/ ? 0 : 1 evaluates to 0 if the character is the same as the last character to its left, and 1 otherwise.

Ruby -p, 50 bytes

 
gsub(/./){"%07b"%$&.ord}
gsub(/./){$`=~/#$&$/?0:1}

Try it online!

Explanation

First line, same as Value Ink's answer:

gsub(/./){                 # Replace each character $&…
                 $&.ord    # …with its ASCII code…
                %          # …formatted as…
          "%07b"           # …binary digits padded to 7 places.
                       }

Second line:

gsub(/./){                 # Replace each character…
          $`               # …if the text to its left…
            =~             # …matches…
              /#$&$/       # …the Regexp /c$/ where "c" is the character…
                    ?0:1   # …with 0, or 1 otherwise.
                        }
deleted 7 characters in body
Source Link
Jordan
  • 10.1k
  • 1
  • 31
  • 47
Loading
Source Link
Jordan
  • 10.1k
  • 1
  • 31
  • 47
Loading