Reverse and Invert a String

27

2

Reverse and Invert a String

Challenge

In this challenge. You'll be writing a program which will output or return the input, reversed and inverted.

First, each character should be converted to its character code. Then, that should be converted to base-2. Following, that string should be reversed. After, the string should be inverted (1 -> 0 and 0 -> 1). Finally, that should be converted back to base 2 and then converted back to a character. If an character results to be an unprintable, you may optionally output it but they do not have to be removed.

H -> 72  -> 1001000 -> 0001001 -> 1110110 -> 118 -> v
e -> 101 -> 1100101 -> 1010011 -> 0101100 -> 44  -> ,
l -> 108 -> 1101100 -> 0011011 -> 1100100 -> 100 -> d
l -> 108 -> 1101100 -> 0011011 -> 1100100 -> 100 -> d
o -> 111 -> 1101111 -> 1111011 -> 0000100 -> 4   -> (unprintable)
, -> 44  -> 101100  -> 001101  -> 110010  -> 50  -> 2
  -> 32  -> 100000  -> 000001  -> 111110  -> 62  -> >
W -> 87  -> 1010111 -> 1110101 -> 0001010 -> 10  -> (newline)
o -> 111 -> 1101111 -> 1111011 -> 0000100 -> 4   -> (unprintable)
r -> 114 -> 1110010 -> 0100111 -> 1011000 -> 88  -> X
l -> 108 -> 1101100 -> 0011011 -> 1100100 -> 100 -> d
d -> 100 -> 1100100 -> 0010011 -> 1101100 -> 108 -> l
! -> 33  -> 100001  -> 100001  -> 011110  -> 30  -> (unprintable)

Scoring

Shortest code in bytes wins.

-15% Bonus: if your program removes un-printables from the output. This must be at least all characters below 32 except newlines (char 10)

Downgoat

Posted 2015-11-06T01:11:40.090

Reputation: 27 116

I need to get my Simplex interpreter working again XD GBktnkZs – Conor O'Brien – 2015-11-06T01:13:22.160

So the characters in the string aren't reversed, but the bits in each character are? – xnor – 2015-11-06T01:36:27.957

Just to be sure: for 0010000 is the bit reverse 0000100 or 00001? – Digital Trauma – 2015-11-06T02:37:55.900

@DigitalTrauma If the binary code is 0010000, it should be treated as 10000 so the reverse would be 00001 – Downgoat – 2015-11-06T03:06:37.030

For the bonus, do we need to leave newlines, or can we remove anything below 00010000 == space? – ETHproductions – 2015-11-06T04:01:59.773

@ETHproductions clarified, you should leave newlines – Downgoat – 2015-11-06T04:05:43.910

2Can we assume just ASCII (as your examples), or should this work for whatever is a character in my language? (Also, if a language uses a different character code, should I use this instead of ASCII/Unicode)? – Paŭlo Ebermann – 2015-11-06T17:30:08.060

32 and below? You want to remove spaces too? – aditsu quit because SE is EVIL – 2015-11-06T18:34:50.030

@PaŭloEbermann if your language supports only ASCII then you may support only ASCII otherwise you should support the whole character set of your language – Downgoat – 2015-11-06T19:18:38.377

@aditsu nope spaces are fine :p Fixed – Downgoat – 2015-11-06T19:19:00.330

You say "or return" but you only say "a program." Is a function allowed? – Level River St – 2015-11-06T20:12:03.760

@steveverrill of course – Downgoat – 2015-11-06T20:19:28.067

Also, the title is a bit misleading, because I would have supposed that the whole string is reversed, too, not just each individual character. – Paŭlo Ebermann – 2015-11-06T21:52:24.613

So you want ASCII 10 and 32 to 126 printed in the bonus version? Are 9 to 13 allowed? – Titus – 2017-04-15T22:13:49.150

Answers

4

CJam, 14

q{i2bW%:!2bc}%

Try it online

Explanation:

Pretty straightforward:

q       read input
{…}%    convert each character
  i     convert to number
  2b    convert to base 2 (digit array)
  W%    reverse
  :!    invert each digit
  2b    convert from base 2
  c     convert to character

"Printable" version, 20 - 15% = 17

q{i2bW%:!2bc' ,N--}%

aditsu quit because SE is EVIL

Posted 2015-11-06T01:11:40.090

Reputation: 22 326

Dangit, you. I was just about to post a CJam answer D: – anOKsquirrel – 2015-11-06T01:20:59.057

@anOKsquirrel sorry ^^ was it similar? – aditsu quit because SE is EVIL – 2015-11-06T01:21:52.577

It would have been if I had finished it. – anOKsquirrel – 2015-11-06T01:23:22.447

How does W% work? W is -1, so... – anOKsquirrel – 2015-11-06T01:25:25.633

1

@anOKsquirrel see the documentation here

– aditsu quit because SE is EVIL – 2015-11-06T01:26:41.840

Just out of interest, it would be nice to have version which removes unprintables, as separate answer probably(@anOKsquirrel ?). – hyde – 2015-11-06T06:50:40.817

@hyde Okay, will do. I feel it should just be though because I would probably just copy his and I also want to use a different language for this – anOKsquirrel – 2015-11-06T12:53:48.533

9

Pyth, 14 bytes

smCi!M_jCd2 2z

Try it online.

How it works

 m           z  Map over the input with lambda d:
        Cd        Cast d to character.
       j  2       Convert to base 2.
      _           Reverse the resulting array.
    !M            Mapped logical NOT.
   i        2     Convert back to integer.
  C               Cast to character.
s               Concatenate the resulting characters.

Dennis

Posted 2015-11-06T01:11:40.090

Reputation: 196 637

2Alternative solutions (all 14 bytes): smCi!MvM_.Bd2z smCi.r_.Bd`T2z smCiqR\0_.Bd2z – Dennis – 2015-11-06T02:38:52.470

How about a version which removes unprintables, just out of interest/for comparison? Probably as a separate answer. – hyde – 2015-11-06T06:51:38.963

8

Perl, 57 51 characters

(50 characters code + 1 character command-line option.)

s/./$_=unpack b8,$&;s|0+$||;"chr 0b".y|10|01|r/gee

Sample run:

bash-4.3$ perl -pe 's/./$_=unpack b8,$&;s|0+$||;"chr 0b".y|10|01|r/gee' <<< 'Hello, World!'
v,dd2>
Xdl

bash-4.3$ perl -pe 's/./$_=unpack b8,$&;s|0+$||;"chr 0b".y|10|01|r/gee' <<< 'Hello, World!' | od -tad1
0000000    v    ,    d    d  eot    2    >   nl  eot    X    d    l   rs   nl
         118   44  100  100    4   50   62   10    4   88  100  108   30   10
0000016

manatwork

Posted 2015-11-06T01:11:40.090

Reputation: 17 865

151 bytes: -p s/./$_=unpack b8,$&;s|0+$||;"chr 0b".y|10|01|r/gee. unpack b8,$& is shorter than sprintf'%b',ord$&, and additionally decodes in reverse order. Unfortunately it also produces trailing 0s, which need to be removed. – primo – 2015-11-07T11:08:24.527

Thank you @primo. unpack is still unexplored terrain for me. – manatwork – 2015-11-07T17:28:01.510

42 bytes: -p s/./"chr 0b".unpack(b8,~$&)=~s|1+$||r/gee. Invert the character, no need to transliterate ;) – primo – 2015-11-09T11:48:28.527

7

JavaScript (ES6 ES7), 119 114 108 bytes

This turned out way longer than expected :(

Thanks to @vihan for 5 bytes saved! Thanks to @ETHProductions for another 6 bytes saved!

To test:  Run the snippet below, enter input like "Hello, World!", and click Test!

x=>String.fromCharCode(...[for(y of x)+('0b'+[...y.charCodeAt().toString(2)].reverse().map(z=>z^1).join``)])
<!--                               Try the test suite below!                              --><strong id="bytecount" style="display:inline; font-size:32px; font-family:Helvetica"></strong><strong id="bytediff" style="display:inline; margin-left:10px; font-size:32px; font-family:Helvetica; color:lightgray"></strong><br><br><pre style="margin:0">Code:</pre><textarea id="textbox" style="margin-top:5px; margin-bottom:5px"></textarea><br><pre style="margin:0">Input:</pre><textarea id="inputbox" style="margin-top:5px; margin-bottom:5px"></textarea><br><button id="testbtn">Test!</button><button id="resetbtn">Reset</button><br><p><strong id="origheader" style="font-family:Helvetica; display:none">Original Code Output:</strong><p><div id="origoutput" style="margin-left:15px"></div><p><strong id="newheader" style="font-family:Helvetica; display:none">New Code Output:</strong><p><div id="newoutput" style="margin-left:15px"></div><script type="text/javascript" id="golfsnippet">var bytecount=document.getElementById("bytecount");var bytediff=document.getElementById("bytediff");var textbox=document.getElementById("textbox");var inputbox=document.getElementById("inputbox");var testbtn=document.getElementById("testbtn");var resetbtn=document.getElementById("resetbtn");var origheader=document.getElementById("origheader");var newheader=document.getElementById("newheader");var origoutput=document.getElementById("origoutput");var newoutput=document.getElementById("newoutput");textbox.style.width=inputbox.style.width=window.innerWidth-50+"px";var _originalCode=null;function getOriginalCode(){if(_originalCode!=null)return _originalCode;var allScripts=document.getElementsByTagName("script");for(var i=0;i<allScripts.length;i++){var script=allScripts[i];if(script.id!="golfsnippet"){originalCode=script.textContent.trim();return originalCode}}}function getNewCode(){return textbox.value.trim()}function getInput(){try{var inputText=inputbox.value.trim();var input=eval("["+inputText+"]");return input}catch(e){return null}}function setTextbox(s){textbox.value=s;onTextboxChange()}function setOutput(output,s){output.innerHTML=s}function addOutput(output,data){output.innerHTML+='<pre style="background-color:'+(data.type=="err"?"lightcoral":"lightgray")+'">'+escape(data.content)+"</pre>"}function getByteCount(s){return(new Blob([s],{encoding:"UTF-8",type:"text/plain;charset=UTF-8"})).size}function onTextboxChange(){var newLength=getByteCount(getNewCode());var oldLength=getByteCount(getOriginalCode());bytecount.innerHTML=newLength+" bytes";var diff=newLength-oldLength;if(diff>0){bytediff.innerHTML="(+"+diff+")";bytediff.style.color="lightcoral"}else if(diff<0){bytediff.innerHTML="("+diff+")";bytediff.style.color="lightgreen"}else{bytediff.innerHTML="("+diff+")";bytediff.style.color="lightgray"}}function onTestBtn(evt){origheader.style.display="inline";newheader.style.display="inline";setOutput(newoutput,"");setOutput(origoutput,"");var input=getInput();if(input===null){addOutput(origoutput,{type:"err",content:"Input is malformed. Using no input."});addOutput(newoutput,{type:"err",content:"Input is malformed. Using no input."});input=[]}doInterpret(getNewCode(),input,function(data){addOutput(newoutput,data)});doInterpret(getOriginalCode(),input,function(data){addOutput(origoutput,data)});evt.stopPropagation();return false}function onResetBtn(evt){setTextbox(getOriginalCode());origheader.style.display="none";newheader.style.display="none";setOutput(origoutput,"");setOutput(newoutput,"")}function escape(s){return s.toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}window.alert=function(){};window.prompt=function(){};function doInterpret(code,input,cb){var workerCode=interpret.toString()+";function stdout(s){ self.postMessage( {'type': 'out', 'content': s} ); }"+" function stderr(s){ self.postMessage( {'type': 'err', 'content': s} ); }"+" function kill(){ self.close(); }"+" self.addEventListener('message', function(msg){ interpret(msg.data.code, msg.data.input); });";var interpreter=new Worker(URL.createObjectURL(new Blob([workerCode])));interpreter.addEventListener("message",function(msg){cb(msg.data)});interpreter.postMessage({"code":code,"input":input});setTimeout(function(){interpreter.terminate()},1E4)}setTimeout(function(){getOriginalCode();textbox.addEventListener("input",onTextboxChange);testbtn.addEventListener("click",onTestBtn);resetbtn.addEventListener("click",onResetBtn);setTextbox(getOriginalCode())},100);function interpret(code,input){window={};alert=function(s){stdout(s)};window.alert=alert;console.log=alert;prompt=function(s){if(input.length<1)stderr("not enough input");else{var nextInput=input[0];input=input.slice(1);return nextInput.toString()}};window.prompt=prompt;(function(){try{var evalResult=eval(code);if(typeof evalResult=="function"){var callResult=evalResult.apply(this,input);if(typeof callResult!="undefined")stdout(callResult)}}catch(e){stderr(e.message)}})()};</script>

jrich

Posted 2015-11-06T01:11:40.090

Reputation: 3 898

I think you could save a 4 bytes replacing the parseInt with +('0b'+<code>) as described here and another one by using w^1 instead of +!+w

– Downgoat – 2015-11-06T02:12:49.130

I wanted to post this, but decided it was too similar to be a separate answer: An advantage of String.fromCharCode is that you can pass in multiple numbers, and it will automatically convert all of them and join them. So here's 110: x=>String.fromCharCode(...[...x].map(y=>+('0b'+[...y.charCodeAt().toString(2)].reverse().map(z=>z^1).join``))) – ETHproductions – 2015-11-06T03:48:33.137

Or an ES7 variant, 108 bytes: x=>String.fromCharCode(...[for(y of x)+('0b'+[...y.charCodeAt().toString(2)].reverse().map(z=>z^1).join``)]) – ETHproductions – 2015-11-06T04:00:02.460

@ETHproductions wow, never knew that String.fromCharCode could do that! Thanks! – jrich – 2015-11-06T04:09:15.653

2I never would have thought this possible, but I just golfed off .05 bytes: x=>String.fromCharCode(...[for(y of x)if((c=+('0b'+[...y.charCodeAt().toString(2)].reverse().map(z=>z^1).join``)) >31||c==10)c]) (127 - 15% = 107.95) Maybe this isn't legal, though; it only handles 10 == \n, not 13 == \r. @Vɪʜᴀɴ what is your opinion? – ETHproductions – 2015-11-06T04:14:19.860

@ETHproductions That's fine :) – Downgoat – 2015-11-06T04:15:13.270

@ETHproductions wow, I think that's sufficiently different to warrant its own post! However, couldn't you just call alert in the function instead since it automatically won't show unprintables? – jrich – 2015-11-06T04:19:25.037

@UndefinedFunction It does on my browser :( – ETHproductions – 2015-11-06T04:20:30.667

@ETHproductions whoops, my bad! the test case I was using didn't have any unprintables.... Sorry, spoke too quickly :( – jrich – 2015-11-06T04:22:37.980

1I get Unexpected token '>' when I try to run the snippet. – Paul R – 2015-11-06T11:46:54.467

@PaulR Try with an ES6-compliant browser, such as one of the newer versions of Firefox. – ETHproductions – 2015-11-06T13:04:31.863

1@ETHproductions: thanks - I only have Safari and Chrome to hand and I guess neither of these is "ES6-compliant". – Paul R – 2015-11-06T14:07:21.117

1

@PaulR ES6, or ECMAScript 6, is one of the latest sets of new features for JavaScript. See this site for more info. There's also a compatability table that shows which features are supported by which browsers (and other programs). This answer in particular requires "arrow functions", the "spread operator", and ES7's "array comprehensions".

– ETHproductions – 2015-11-06T15:26:12.170

Thanks for the explanation - I got curious and downloaded the latest Firefox for Mac and now the above snippet does indeed run correctly. – Paul R – 2015-11-06T16:55:20.080

@ETHproductions you can save .85 bytes using | instead of || in your condition – edc65 – 2015-11-07T10:06:47.113

Just in case you missed it, I posted my answer here. @edc65: Thanks, I always forget that usually works. :)

– ETHproductions – 2015-11-08T02:40:29.547

5

JavaScript (ES7), 126 bytes - 15% = 107.1

I was playing around with this answer to see if the bonus was worth it. Apparently, it is. The test suite was stolen from the same answer, but I added my own twist: full support of the 15% bonus! :)

x=>String.fromCharCode(...[for(y of x)if((c=+('0b'+[...y.charCodeAt().toString(2)].reverse().map(z=>z^1).join``))>31|c==10)c])
<!--                               Try the test suite below!                              --><strong id="bytecount" style="display:inline; font-size:32px; font-family:Helvetica"></strong><strong id="bytediff" style="display:inline; margin-left:10px; font-size:32px; font-family:Helvetica; color:lightgray"></strong><br><strong id="score" style="display:inline; font-size:32px; font-family:Helvetica">Score:</strong><strong id="scorediff" style="display:inline; margin-left:10px; font-size:32px; font-family:Helvetica; color:lightgray"></strong><br><br><pre style="margin:0">Code:</pre><textarea id="textbox" style="margin-top:5px; margin-bottom:5px"></textarea><br><pre style="margin:0">Input:</pre><textarea id="inputbox" style="margin-top:5px; margin-bottom:5px">"Hello, World!"</textarea><br><button id="testbtn">Test!</button><button id="resetbtn">Reset</button><br><p><strong id="origheader" style="font-family:Helvetica; display:none">Original Code Output:</strong><p><div id="origoutput" style="margin-left:15px"></div><p><strong id="newheader" style="font-family:Helvetica; display:none">New Code Output:</strong><p><div id="newoutput" style="margin-left:15px"></div><script type="text/javascript" id="golfsnippet">var bytecount=document.getElementById("bytecount");var bytediff=document.getElementById("bytediff");var score=document.getElementById("score");var scorediff=document.getElementById("scorediff");var textbox=document.getElementById("textbox");var inputbox=document.getElementById("inputbox");var testbtn=document.getElementById("testbtn");var resetbtn=document.getElementById("resetbtn");var origheader=document.getElementById("origheader");var newheader=document.getElementById("newheader");var origoutput=document.getElementById("origoutput");var newoutput=document.getElementById("newoutput");textbox.style.width=inputbox.style.width=window.innerWidth-50+"px";var _originalCode=null;function getOriginalCode(){if(_originalCode!=null)return _originalCode;var allScripts=document.getElementsByTagName("script");for(var i=0;i<allScripts.length;i++){var script=allScripts[i];if(script.id!="golfsnippet"){originalCode=script.textContent.trim();return originalCode}}}function getNewCode(){return textbox.value.trim()}function getInput(){try{var inputText=inputbox.value.trim();var input=eval("["+inputText+"]");return input}catch(e){return null}}function setTextbox(s){textbox.value=s;onTextboxChange()}function setOutput(output,s){output.innerHTML=s}function addOutput(output,data){output.innerHTML+='<pre style="background-color:'+(data.type=="err"?"lightcoral":"lightgray")+'">'+escape(data.content)+"</pre>"}function getByteCount(s){return(new Blob([s],{encoding:"UTF-8",type:"text/plain;charset=UTF-8"})).size}function getScore(s){var a=1;try{b=eval('('+s+')("Hello, World!")');if(b=="v,dd2>\nXdl")a=.85}catch(e){};return getByteCount(s)*a}function onTextboxChange(){var newLength=getByteCount(getNewCode());var oldLength=getByteCount(getOriginalCode());bytecount.innerHTML=newLength+" bytes";var diff=newLength-oldLength;if(diff>0){bytediff.innerHTML="(+"+diff+")";bytediff.style.color="lightcoral"}else if(diff<0){bytediff.innerHTML="("+diff+")";bytediff.style.color="lightgreen"}else{bytediff.innerHTML="("+diff+")";bytediff.style.color="lightgray"}newLength=getScore(getNewCode());var oldLength=getScore(getOriginalCode());score.innerHTML="Score: "+newLength;var diff=Math.round((newLength-oldLength)*100)/100;if(diff>0){scorediff.innerHTML="(+"+diff+")";scorediff.style.color="lightcoral"}else if(diff<0){scorediff.innerHTML="("+diff+")";scorediff.style.color="lightgreen"}else{scorediff.innerHTML="("+diff+")";scorediff.style.color="lightgray"}}function onTestBtn(evt){origheader.style.display="inline";newheader.style.display="inline";setOutput(newoutput,"");setOutput(origoutput,"");var input=getInput();if(input===null){addOutput(origoutput,{type:"err",content:"Input is malformed. Using no input."});addOutput(newoutput,{type:"err",content:"Input is malformed. Using no input."});input=[]}doInterpret(getNewCode(),input,function(data){addOutput(newoutput,data)});doInterpret(getOriginalCode(),input,function(data){addOutput(origoutput,data)});evt.stopPropagation();return false}function onResetBtn(evt){setTextbox(getOriginalCode());origheader.style.display="none";newheader.style.display="none";setOutput(origoutput,"");setOutput(newoutput,"")}function escape(s){return s.toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}window.alert=function(){};window.prompt=function(){};function doInterpret(code,input,cb){var workerCode=interpret.toString()+";function stdout(s){ self.postMessage( {'type': 'out', 'content': s} ); }"+" function stderr(s){ self.postMessage( {'type': 'err', 'content': s} ); }"+" function kill(){ self.close(); }"+" self.addEventListener('message', function(msg){ interpret(msg.data.code, msg.data.input); });";var interpreter=new Worker(URL.createObjectURL(new Blob([workerCode])));interpreter.addEventListener("message",function(msg){cb(msg.data)});interpreter.postMessage({"code":code,"input":input});setTimeout(function(){interpreter.terminate()},1E4)}setTimeout(function(){getOriginalCode();textbox.addEventListener("input",onTextboxChange);testbtn.addEventListener("click",onTestBtn);resetbtn.addEventListener("click",onResetBtn);setTextbox(getOriginalCode())},100);function interpret(code,input){window={};alert=function(s){stdout(s)};window.alert=alert;console.log=alert;prompt=function(s){if(input.length<1)stderr("not enough input");else{var nextInput=input[0];input=input.slice(1);return nextInput.toString()}};window.prompt=prompt;(function(){try{var evalResult=eval(code);if(typeof evalResult=="function"){var callResult=evalResult.apply(this,input);if(typeof callResult!="undefined")stdout(callResult)}}catch(e){stderr(e.message)}})()};</script>

ETHproductions

Posted 2015-11-06T01:11:40.090

Reputation: 47 880

Awesome modification of the test snippet! It seems the snippet now automatically checks for the bonus, may I ask how you did that? (p.s. if you want the original (not on one line) source, feel free to ask, might be easier to modify that way) – jrich – 2015-11-08T23:09:06.543

@UndefinedFunction Oh, sorry for not replying right away! I added a getScore() function which checks the test-case Hello, World! for compliance (it conveniently contains both a newline and unprintable chars), and returns the score multiplied by .85 or 1, depending on the result. And yes, access to the un-minified snippet would be great. :) – ETHproductions – 2015-11-14T23:27:36.867

I've made the original snippet code available here. Have fun!

– jrich – 2015-11-15T22:02:18.637

4

PHP - 187 182 163 bytes

<?php $s=$_GET["s"];$m="array_map";echo join($m("chr",$m("bindec",$m(function($v){return strtr($v,[1,0]);},$m("strrev",$m("decbin",$m("ord",str_split($s))))))));?>

Pass the value as GET["s"].

array_map returns an array with all the elements of the second parameter (an array) after applying the callback function (first parameter) to all of them.

Not sure if I should take the 15% off, since echo doesn't output unprintable characters, but I didn't remove them.

Just glad I finished, since this is the first challenge I take part.

undefined

Posted 2015-11-06T01:11:40.090

Reputation: 211

1Is shorter if you not declare those functions: $m='array_map';echo join($m("chr",$m("bindec",$m(function($v){return strtr($v,[1,0]);},$m("strrev",$m("decbin",$m("ord",str_split($s))))))));. – manatwork – 2015-11-06T16:24:56.000

@manatwork completely forgot about that. Thanks. – undefined – 2015-11-06T16:27:36.147

You can't just put the input in a variable. You should create a function or read the input from STDIN. By the way, you don't need to use quotes around string ("chr", "bindec", …) since we don't care about warnings. That should save you 12 bytes. – Blackhole – 2015-11-07T11:50:14.993

@Blackhole thanks for the infos, I'll be aware of that next time. – undefined – 2015-11-07T13:40:27.083

You'd better do the modification on this answer, which is otherwise invalid :) . It will cost you almost no bytes, just replace str_split($s) with str_split(fgets(STDIN)) for instance. – Blackhole – 2015-11-07T17:57:35.827

You may want to have a look at some simple golfing tips to take your code down to 132 bytes. By the way, echo does print the control characters; it´s the browsers that ignore them. Oh and strtr(...,[1,0]) only translates 1 to 0, not vice versa; better use strtr(...,10,"01") (+2 bytes).

– Titus – 2017-04-15T22:54:26.093

3

Julia, 77 bytes - 15% = 65.45

s->join(filter(isprint,[Char(parse(Int,join(1-digits(Int(c),2)),2))for c=s]))

This creates an unnamed functon that accepts a string and returns a string. Unprintable characters are removed, which qualifies this for the bonus.

Ungolfed:

function f(s::AbstractString)
    # digits() returns the digits in reverse order, so no explicit
    # reverse() is needed
    x = [Char(parse(Int, join(1 - digits(Int(c), 2)), 2)) for c = s]

    # Remove unprintables, join into a string
    return join(filter(isprint, x))
end

Alex A.

Posted 2015-11-06T01:11:40.090

Reputation: 23 761

While it certainly qualifies it for the bonus, it also costs more than the bonus saves. 16 bytes to filter(isprint,) and only 11.55 bytes saved through the bonus. – Glen O – 2015-11-06T15:03:38.323

And if you give up the filter step, you can avoid the comprehension and join by using map directly on the string. s->map(c->Char(parse(Int,join(1-digits(Int(c),2)),2)),s) (for 56 bytes) – Glen O – 2015-11-06T15:06:54.403

@GlenO Thanks for the suggestions, but that approach leaves in unprintables as hex codes, which the OP said is not allowed. Using filter(isprint,) both qualifies it for the bonus and makes it compliant with the rules. – Alex A. – 2015-11-06T21:03:24.360

"If an character results to be an unprintable, you may optionally output it but they do not have to be removed." – Glen O – 2015-11-07T03:50:30.653

And if the concern is on the other side of it (that it displays as \x04 and the like), then print() costs seven, which would bring 56 up to 63. – Glen O – 2015-11-07T03:53:05.790

3

K5, 28 bytes

`c${b/~|{x@&|\x}@(b:8#2)\x}'

This is a bit inconvenient because K5's decode operator performs a fixed-width base conversion, so to comply with the problem statement I have to trim leading zeroes. The lambda {x@&|\x} accomplishes this step.

Smear:

  |\0 0 1 0 1 1 0 1
0 0 1 1 1 1 1 1

Gather:

  &|\0 0 1 0 1 1 0 1
2 3 4 5 6 7

Select:

  {x@&|\x}0 0 1 0 1 1 0 1
1 0 1 1 0 1

The whole program in action:

  `c${b/~|{x@&|\x}@(b:8#2)\x}'"Hello, World"
"v,dd2>\nXdl"

I believe oK's natural behavior with unprintables makes this eligible for -15%, giving this a score of 28*0.85 = 23.8.

JohnE

Posted 2015-11-06T01:11:40.090

Reputation: 4 632

+1 because I tried but couldn't figure out a short way to get rid of the leading zeros! – kirbyfan64sos – 2015-11-08T03:20:27.567

A few related constructions can be found here.

– JohnE – 2015-11-08T04:39:29.043

3

C function, 63

o;f(o,char *s){for(;*s;*s=o,s++)for(o=0;*s;*s/=2)o+=o+!(*s%2);}

Digital Trauma

Posted 2015-11-06T01:11:40.090

Reputation: 64 644

3

PowerShell, 199 175 (171 - 15%)=145.35

param([char[]]$a)($a|%{$b=[convert]::ToString(+$_,2);$c=[convert]::ToInt32("$((-join$b[$b.Length..0])-split0-replace1,0-join1)",2);if($c-gt31-or$c-eq10){[char]$c}})-join''

Uses an unfortunate amount of some .NET calls/built-ins, which significantly bloats the code.

Explained:

Takes the input param(..) and casts it as a char[] so we can work through it appropriately.

The next bit (..)-join'' collects and joins our output together.

Inside those parens, we iterate with $a|%{..} as a foreach loop.

Inside the loop:

  • We create a new string $b, which is our input letter cast as an int +$_ and [convert]ed to base 2
  • This next bit, setting $c, is tricky, so let's start inside and work our way out
  • We reverse the string $b with (-join$b[$b.length..0])
  • We leverage my previous code for inverting a binary string and recast the result as a string with "$(..)"
  • We feed that string into a different .NET call that [convert]s ToInt32 from base 2, which is finally stored that into $c
  • If $c is greater than 31, or equal to 10, we cast it as a char and that value is left on the pipeline for output (which is what gets collected and -join''ed together, above), else nothing gets left on this particular iteration

Phew.

Qualifies for the -15% bonus, as well.

Example

PS C:\Tools\Scripts\golfing> .\reverse-and-invert-a-string.ps1 "Hello, World!"
v,dd2>
Xdl

AdmBorkBork

Posted 2015-11-06T01:11:40.090

Reputation: 41 581

2

, 39 chars / 73 bytes

ô⟦ï]ć⇝ϚĎ(+“0b`+⟦a.ü⬮ߧ2]ù⬮ć⇀$^1)ø`”⸩ø⬯⦆

Try it here (Firefox only).

Mama Fun Roll

Posted 2015-11-06T01:11:40.090

Reputation: 7 234

1

Minkolang 0.11, 26 bytes

od?.(d2%,$r2:d)xrI1-[2*+]O

Try it here.

Explanation

od?.            Takes input as character, halting if empty
(d2%,$r2:d)x    Converts to binary, inverting digits on the way
r               Reverses stack
I1-[2*+]        Converts to decimal
O               Outputs as character (if printable)

El'endia Starman

Posted 2015-11-06T01:11:40.090

Reputation: 14 504

1

MATLAB, 60 bytes

@(y)[arrayfun(@(x)bin2dec([97-fliplr(dec2bin(x)) '']),y) '']

Basically each character in turn is converted into a binary string (with no leading zeros). The array is flipped and is subtracted from 97 ('0'+'1') which inverts the character. This is converted back to decimal. After all characters have been processed, the whole array is then converted back to characters before being returned.

Tom Carpenter

Posted 2015-11-06T01:11:40.090

Reputation: 3 990

1

Japt, 25 bytes

Want to create a golfy JavaScript program, but the shortest method involves lots of long function names? That's what Japt was made for. :)

UmX=>Xc s2 w mY=>Y^1 n2 d

Try it in the online interpreter!

How it works

         // Implicit: U = first item in input
UmX=>    // for each character X in U:
Xc s2 w  //  take the char-code of X, convert to binary, and reverse
mY=>     //  for each character Y in this:
Y^1      //   take Y XOR 1 (converts 1 to 0 and 0 to 1)
n2 d     //  convert the result back to decimal, then to a character
         // Implicit: output last expression

Using the current version of Japt (as of v1.4.4), the byte count can be cut to 14:

®c ¤w m^1 n2 d

Test it online!

ETHproductions

Posted 2015-11-06T01:11:40.090

Reputation: 47 880

1

Python 3, 95 91

Straightforward implementation.

print(''.join(chr(int(''.join('10'[j>'0']for j in bin(ord(i))[:1:-1]),2))for i in input()))

Ungolfed:

inp = input()
ints = (ord(i) for i in inp)
bins = (bin(i) for i in ints)
revs = (i[2:][::-1] for i in bins) #without leading '0b'
invs = (''.join('0' if j == '1' else '1' for j in i) for i in revs)
newInts = (int(i, 2) for i in invs)
newChars = (chr(i) for i in newInts)
newStr = ''.join(newChars)
print(newStr)

Trang Oul

Posted 2015-11-06T01:11:40.090

Reputation: 656

1

Ruby, 62 characters

gets.bytes{|b|$><<b.to_s(2).reverse.tr("01","10").to_i(2).chr}

Samply run:

bash-4.3$ ruby -e 'gets.bytes{|b|$><<b.to_s(2).reverse.tr("01","10").to_i(2).chr}' <<< 'Hello, World!'
v,dd2>
Xdl

bash-4.3$ ruby -e 'gets.bytes{|b|$><<b.to_s(2).reverse.tr("01","10").to_i(2).chr}' <<< 'Hello, World!' | od -tad1
0000000    v    ,    d    d  eot    2    >   nl  eot    X    d    l   rs   nl
         118   44  100  100    4   50   62   10    4   88  100  108   30   10
0000016

manatwork

Posted 2015-11-06T01:11:40.090

Reputation: 17 865

1

Javascript 123 bytes

s=>[].map.call(s,s=>String.fromCharCode("0b"+s.charCodeAt().toString(2).split('').reverse().map(s=>s^1).join(''))).join('')

Naouak

Posted 2015-11-06T01:11:40.090

Reputation: 349

1

C#, 156 bytes - 15% = 132.6

class P{static void Main(string[]a){try{for(int i=0,b,c;;){for(b=a[0][i++],c=0;b>0;b/=2)c=c<<1|1-b%2;if(c==10|c>31)System.Console.Write((char)c);}}catch{}}}

Indentation and new lines for clarity:

class P{
    static void Main(string[]a){
        try{
            for(int i=0,b,c;;){
                for(b=a[0][i++],c=0;b>0;b/=2)
                    c=c<<1|1-b%2;
                if(c==10|c>31)
                    System.Console.Write((char)c);
            }
        }
        catch{}
    }
}

Hand-E-Food

Posted 2015-11-06T01:11:40.090

Reputation: 7 912

1

Retina, 1107 629 bytes - 15% = 534.65 (non-competing)

Uses features added after challenge date. (Implicit behavior of $*, , Sorting)

Retina doesn't have a built-in to convert a character to its ASCII ordinal or back... so behold its lustrous length. This handles printable ASCII, and removes unprintables as well as newlines. Byte count assumes ISO 8859-1 encoding.

The code contains unprintable characters.


¶
±
S_`
%(S`±
{2`
$`
}T01`-`_o
)Ms`.
\d+
$*
+`(1+)\1
${1}0
01
1
%O$^`.

T`01`10
1
01
+`10
011
0

m`^1{1,31}$

M%`1
m`^0¶?

126
~
125
}
124
|
123
{
122
z
121
y
120
x
119
w
118
v
117
u
116
t
115
s
114
r
113
q
112
p
111
o
110
n
109
m
108
l
107
k
106
j
105
i
104
h
103
g
102
f
101
e
100
d
99
c
98
b
97
a
96
`
95
_
94
^
93
]
92
\
91
[
90
Z
89
Y
88
X
87
W
86
V
85
U
84
T
83
S
82
R
81
Q
80
P
79
O
78
N
77
M
76
L
75
K
74
J
73
I
72
H
71
G
70
F
69
E
68
D
67
C
66
B
65
A
64
@
63
?
62
>
61
=
60
<
59
;
58
:
57
9
56
8
55
7
54
6
32

33
!
34
"
35
#
36
$
37
%
38
&
39
'
40
(
41
)
42
*
43
+
44
,
45
-
46
.
47
/
48
0
49
1
50
2
51
3
52
4
53
5
¶

Try it online

If you check out the Retina tutorial for unary arithmetic, you'll recognize several different pieces of my code as coming from there.

Thanks to Martin for golfing off hundred of bytes

mbomb007

Posted 2015-11-06T01:11:40.090

Reputation: 21 944

1

Java, 205 - 15% = 174.2

interface F{static void main(String[]a){for(int i=0,c,s;i<a[0].length();++i){c=a[0].charAt(i);s=Integer.numberOfLeadingZeros(c);c=~(Integer.reverse(c)>>s)&-1>>>s;if(c==10|c>31)System.out.print((char)c);}}}

Ungolfed:

interface F {
    static void main(String[] a) {
        for (int i = 0, c, s; i < a[0].length(); ++i) {
            c = a[0].charAt(i);
            s = Integer.numberOfLeadingZeros(c);
            c = ~(Integer.reverse(c) >> s) & -1 >>> s;
            if (c == 10 | c > 31) System.out.print((char)c);
        }
    }
}

I think this solution is a bit interesting in its use of the Integer methods Integer.reverse and Integer.numberOfLeadingZeros which do what they sound like, and the shift of -1 >>> s where s is the number of leading zeroes, to get the mask to mask off high bits that we don't want. I just regret that the name of the latter method is so damn verbose, but that's what I get for golfing in Java.

Output:

v,dd2>
Xdl

David Conrad

Posted 2015-11-06T01:11:40.090

Reputation: 1 037

0

PHP, 80 bytes

while(a&$c=$argn[$i++])echo chr(bindec(strtr(strrev(decbin(ord($c))),10,"01")));

takes input from STDIN; run with -R.

bonus version, 97 110 bytes -> 93.5 score

while(a&$c=$argn[$i++])ctype_print($c=chr(bindec(strtr(strrev(decbin(ord($c))),10,"01"))))||"
"==$c?print$c:0;

prints ASCII 10 and 32 to 126 (newline and printables)


breakdown, TiO and if possible some golfing will follow; I´m tired right now.

Titus

Posted 2015-11-06T01:11:40.090

Reputation: 13 814

0

Haskell, 167 bytes

import Data.Char
import Numeric
r '0'='1'
r '1'='0'
s=map (chr.fst.head.(readInt 2 (`elem` "01") digitToInt).(map r).reverse.flip (showIntAtBase 2 intToDigit . ord)"")

Unfortunately Haskell gets quite verbose when needing to read/print in another base…

arjanen

Posted 2015-11-06T01:11:40.090

Reputation: 151

0

Perl 6,  66 bytes

Going all-out by removing the non-printing control characters gets me to (83+1)-15% = 71.4

perl6 -ne 'print grep /<-:C+[\n]>/,.ords».base(2)».flip.map({chr :2([~] $_.comb.map(+!+*))})'

If I remove the code that strips out the control characters I save quite a bit 65+1 = 66

perl6 -ne 'print .ords».base(2)».flip.map({chr :2([~] $_.comb.map(+!+*))})'

( I used » instead of >> for clarity )

Brad Gilbert b2gills

Posted 2015-11-06T01:11:40.090

Reputation: 12 713

0

Jelly, 6 bytes (non-competing)

OBU¬ḄỌ

Try it online!

Explanation:

OBU¬ḄỌ Main link: z
O      Convert z to a list of character codes.
 B     Convert the codes to bit lists.
  U    Reverse the bits (not the lists).
   ¬   Invert the bits.
    Ḅ  Convert back to decimal.
     Ọ Convert back to string.

Erik the Outgolfer

Posted 2015-11-06T01:11:40.090

Reputation: 38 134

0

Racket 250 15% bonus = 212 bytes

(λ(s)(list->string(map integer->char(filter(λ(x)(or(> x 31)(= x 10)))(for/list((i(string->list s)))(string->number(string-append
"#b"(list->string(map(λ(i)(if(equal? #\0 i)#\1 #\0))(reverse(string->list(number->string(char->integer i)2))))))))))))

Ungolfed:

(define (f s)
    (list->string 
     (map 
      integer->char
      (filter
       (λ(x)(or(> x 31)(= x 10)))

       (for/list ((i (string->list s)))
         (string->number
          (string-append
           "#b"
           (list->string
            (map
             (λ(i)(if(equal? #\0 i) #\1 #\0))
             (reverse
              (string->list
               (number->string
                (char->integer i) 2)
               )))))))))))

Testing:

(f "Hello, World!")

Output:

"v,dd2>\nXdl"

rnso

Posted 2015-11-06T01:11:40.090

Reputation: 1 635