21
\$\begingroup\$

Write a program or function that:

  1. takes in a string from stdio or arguments
  2. replaces all occurrences of true with false and false with true
  3. reverses it, but does not reverse true and false
  4. returns or prints result

Examples (left side is input):

"true"                     "false"
"2false"                   "true2"
"true is false"            "true si false"
"false,true,undefined"     "denifednu,false,true"
"stressed-false"           "true-desserts"
"falstrue"                 "falseslaf"
"true false true x"        "x false true false"

Standard loopholes apply. This is , so shortest code in bytes wins.

Leaderboard

var QUESTION_ID=63256,OVERRIDE_USER=20569;function answersUrl(e){return"http://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"http://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
9
  • \$\begingroup\$ In Step 3, only one replacement is required, or all occurrences should be replaced? e.g.true true false false becomes true true false false or true eslaf false eurt? \$\endgroup\$
    – gaborsch
    Nov 7, 2015 at 23:01
  • \$\begingroup\$ Should eurt became false or true? \$\endgroup\$
    – Zereges
    Nov 7, 2015 at 23:02
  • \$\begingroup\$ @Zereges It is not false or true before it's reversed, so: eurt->true \$\endgroup\$ Nov 7, 2015 at 23:20
  • \$\begingroup\$ @HannesKarppila So, the rules are applied in order: 1 3 2 4 5 \$\endgroup\$
    – Zereges
    Nov 7, 2015 at 23:24
  • 3
    \$\begingroup\$ Does this also mean that trufalse -> trueurt? \$\endgroup\$
    – Adnan
    Nov 8, 2015 at 10:03

29 Answers 29

12
\$\begingroup\$

C# 6, 144 bytes

string R(string t)=>string.Concat(new System.Text.RegularExpressions.Regex("true|false").Replace(t,m=>m.Value[0]<'g'?"eurt":"eslaf").Reverse());

It uses a regular expression to match true|false, and if it matches true it will be replaced by eslaf, otherwise by eurt. m.Value[0]<'g' is a shorter way to say m.Value[0]=="false", because the only possible values for m.Value are "true" or "false", so if the char code of the first char is smaller than the char code of 'g', it's "false".

Old version, 95 bytes

This one had a bug, it didn't return correct output for falstrue.

string R(string t)=>string.Concat(t.Reverse()).Replace("eurt","false").Replace("eslaf","true");
\$\endgroup\$
7
  • 15
    \$\begingroup\$ A C# solution under 100 bytes? Surely the end of days is upon us. \$\endgroup\$
    – Alex A.
    Nov 7, 2015 at 22:48
  • \$\begingroup\$ @AlexA. Haha, all thanks to version 6 ;P \$\endgroup\$
    – ProgramFOX
    Nov 7, 2015 at 22:50
  • 1
    \$\begingroup\$ This does not work correctly for the input falstrue. \$\endgroup\$
    – feersum
    Nov 8, 2015 at 2:12
  • 2
    \$\begingroup\$ @AlexA. Well, with the incorrect output for falstrue fixed, it's no longer <100 bytes... :( \$\endgroup\$
    – ProgramFOX
    Nov 8, 2015 at 7:44
  • 14
    \$\begingroup\$ While that is sad indeed, it means we no longer have to worry about the apocalypse. Software bugs have saved us all. \$\endgroup\$
    – Alex A.
    Nov 8, 2015 at 7:49
7
\$\begingroup\$

TeaScript, 36 25 24 bytes

xv¡g("eurt|eslaf",#ln>4)   

TeaScript is JavaScript for golfing.

Edits: Saved 11 bytes thanks to @Vɪʜᴀɴ. Fixed for input falstrue and saved a byte.

Old version (Invalid):

xv¡g("eurt",f)g(f.T¡v¡,t)

Explanation:

x    // Input
 v¡    // Reverse
   g("eurt",    // Global replace "eurt" with "false".
            f)    // f is predefined to false.
              g(f.T¡v¡, // Convert false to string, then reverse.
                       t) // t is predefined to true.
\$\endgroup\$
5
  • \$\begingroup\$ If you use l and i instead of t and f, you can ommit the .s. f is also predefined to false so you can get: xv¡g(l="eurt",i=f+¢)g(iv¡,lv¡) \$\endgroup\$
    – Downgoat
    Nov 7, 2015 at 23:45
  • \$\begingroup\$ Actually even better: xv¡g("eurt",f)g(f.T¡v¡,t) \$\endgroup\$
    – Downgoat
    Nov 7, 2015 at 23:51
  • \$\begingroup\$ @Vɪʜᴀɴ Thanks for the help. didn't see that in the docs. Would it be possible to auto insert ( after methods? Like replace(/(\.[BcCdeE...])/g,"$1(") or similar after inserting periods. \$\endgroup\$ Nov 8, 2015 at 0:39
  • 1
    \$\begingroup\$ This does not work correctly for the input falstrue. \$\endgroup\$
    – feersum
    Nov 8, 2015 at 2:04
  • \$\begingroup\$ @feersum Fixed. Thanks for pointing that out. That was tricky. \$\endgroup\$ Nov 8, 2015 at 3:40
7
\$\begingroup\$

Bash+GNU, 45 38 73 bytes

Edit: works with both trufalse and falstrue

sed s/false/%eurt%/g\;s/true/%eslaf%/g|rev|sed "s/%\(true\|false\)%/\1/g"

Old version, 38 bytes (shortened, thanks to Digital Trauma):

rev|sed s/eurt/false/g\;s/eslaf/true/g
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Combine the sed expressions into one, and remove the "-e" and quotes: rev|sed s/eurt/false/g\;s/eslaf/true/g \$\endgroup\$ Nov 7, 2015 at 23:47
  • 2
    \$\begingroup\$ This does not work correctly for the input falstrue. \$\endgroup\$
    – feersum
    Nov 8, 2015 at 2:06
  • \$\begingroup\$ @feersum Good point, fixed. Also checked for trufalse. \$\endgroup\$
    – gaborsch
    Nov 9, 2015 at 7:27
6
\$\begingroup\$

JavaScript ES6, 59

As an anonymous function.

Note, replace is used just as a shorthand for match().map(). The replaced string is discarded, and the output string is made piece by piece backwards (so no need to reverse).

s=>s.replace(/false|true|./g,x=>s=(x[1]?x<'t':x)+s,s='')&&s

Test running the snippet below in an EcmaScript 6 compliant browser.

f=s=>s.replace(/false|true|./g,x=>s=(x[1]?x<'t':x)+s,s='')&&s

//test

console.log=x=>O.innerHTML+=x+'\n'

;[
 ["true","false"]
,["falstrue","falseslaf"]  
,["1false","true1"]
,["true is false","true si false"]
,["false,true,undefined","denifednu,false,true"]
,["stressed-false","true-desserts"]
,["true false true x","x false true false"]
].forEach(t=>console.log(t[0]+' -> '+f(t[0])))
<pre id=O></pre>

\$\endgroup\$
1
  • \$\begingroup\$ Came on here to post amazing 62-byte solution...found incredible 59-byte solution. +1 \$\endgroup\$ Nov 8, 2015 at 17:10
5
\$\begingroup\$

Windows Batch, 184 213 bytes

Fixed the bug, falstrue -> falseslaf and trufalse -> trueurt

Probably one of the less popular languages:

setlocal enabledelayedexpansion
set /p Q=
set N=0
:L
call set T=%%Q:~%N%,1%%%
set /a N+=1
if not "%T%" equ "" (
set R=%T%%R%
goto L
)
set R=%R:eurt=false%
set R=%R:eslaf=true%
set R=%R:falstrue=falseslaf%
echo %R%
\$\endgroup\$
2
  • 1
    \$\begingroup\$ This does not work correctly for the input falstrue. \$\endgroup\$
    – feersum
    Nov 8, 2015 at 2:15
  • \$\begingroup\$ @feersum, fixed it :) \$\endgroup\$
    – Adnan
    Nov 8, 2015 at 10:24
5
\$\begingroup\$

Haskell, 94 bytes

Performs pattern matching on the input string, looking for "false" or "true" and appends the opposite to the result of applying the function on the remainder of the string. If true or false isn't found, it uses recursion to reverse the string in the same fashion.

f[]=[]
f('t':'r':'u':'e':s)=f s++"false"
f('f':'a':'l':'s':'e':s)=f s++"true"
f(x:s)=f s++[x]
\$\endgroup\$
3
  • \$\begingroup\$ Added now, sorry about that \$\endgroup\$
    – Craig Roy
    Nov 8, 2015 at 14:44
  • \$\begingroup\$ Never mind. It seems I made some mistake when trying to test it. Maybe I simply printed out the string without managing to apply the function to it. \$\endgroup\$
    – feersum
    Nov 8, 2015 at 18:11
  • \$\begingroup\$ I believe you can take the top f[]=[] line and instead put f x=x at the bottom to save a byte. \$\endgroup\$ Jun 8, 2016 at 17:17
4
\$\begingroup\$

JavaScript ES6, 95 93 bytes

Unnamed function. Add f= to the beginning to use it. Thanks Ismael! Also assumes that the input does not contain tabs.

x=>[...x[r="replace"](/false/g,"\teslaf")[r](/(\t)*true/g,"eurt")[r](/\t/g,"")].reverse().join``
\$\endgroup\$
10
  • \$\begingroup\$ You can use .replace(/eurt/g,false).replace(/eslaf/g,true), since they will be converted to string. Try true + '' (should return 'true') \$\endgroup\$ Nov 7, 2015 at 23:41
  • 3
    \$\begingroup\$ WAIT!!! Change false with !1 and true with !0. There, a few bytes shorter \$\endgroup\$ Nov 7, 2015 at 23:45
  • 3
    \$\begingroup\$ @IsmaelMiguel whoa thanks!!!! \$\endgroup\$ Nov 7, 2015 at 23:46
  • 1
    \$\begingroup\$ I think you should also be able to use [...x] instead of x.split'' \$\endgroup\$
    – Downgoat
    Nov 7, 2015 at 23:51
  • 2
    \$\begingroup\$ 67: x=>[...x].reverse().join``[r='replace'](/eurt/g,!1)[r](/eslaf/g,!0). I also changed the join param to an empty template string to make strip out array commas. \$\endgroup\$ Nov 8, 2015 at 0:45
2
\$\begingroup\$

Pyth, 30 bytes

::_z"eurt""false""eslaf""true"

This reverses the input (_z), substitutes "eurt" for "false" and "eslaf" for "true". Replacement is done using :.

Try it online

\$\endgroup\$
3
  • \$\begingroup\$ You can declare "true" and "false" as a variable: =d"true"=k"false"::_z_dk_kd, and use the reverse of it. It will save 3 bytes. \$\endgroup\$
    – Adnan
    Nov 7, 2015 at 23:49
  • \$\begingroup\$ ::_zJ"eurt"K"false"_K_J is 23 bytes. \$\endgroup\$ Nov 8, 2015 at 0:34
  • 5
    \$\begingroup\$ This does not work correctly for the input falstrue. \$\endgroup\$
    – feersum
    Nov 8, 2015 at 2:03
2
\$\begingroup\$

rs, 56 bytes

\t
+(.*\t)(.)(.*)/\2\1\3
\t/
eurt/fals\t
eslaf/true
\t/e

Live demo and test cases.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ This does not work correctly for the input falstrue. \$\endgroup\$
    – feersum
    Nov 8, 2015 at 2:00
  • \$\begingroup\$ @feersum Ugh...neither does the Pyth answer, FYI... \$\endgroup\$ Nov 8, 2015 at 2:05
  • \$\begingroup\$ At least 7 answers have this problem that I have tested \$\endgroup\$
    – feersum
    Nov 8, 2015 at 2:10
  • \$\begingroup\$ @feersum Ok; I fixed it now. \$\endgroup\$ Nov 8, 2015 at 2:10
2
\$\begingroup\$

Julia, 59 55 46 bytes

s->replace(reverse(s),r"eurt|eslaf",i->i<"et")

This creates an unnamed function that accepts a string and returns a string. To call it, give it a name, e.g. f=s->....

The input is reversed using reverse. We match on the regular expression eurt|eslaf which matches true or false backwards. To the match we apply a function that returns true if the match is lexicographically smaller than et (i.e. eslaf) and false otherwise. The boolean literals are converted to string in the output.

Saved 9 bytes and fixed an issue thanks to Glen O!

\$\endgroup\$
5
  • 2
    \$\begingroup\$ I don't believe this works correctly for the input falstrue. \$\endgroup\$
    – feersum
    Nov 8, 2015 at 2:13
  • \$\begingroup\$ It's using the same basic reasoning, so I'll offer it to you: s->replace(reverse(s),r"eurt|eslaf",i->i<"et") - uses a regex rather than doing it twice, and a function for the replace that evaluates to true if it was "elsaf" and to false if it was "eurt". 46 bytes. \$\endgroup\$
    – Glen O
    Nov 8, 2015 at 5:29
  • \$\begingroup\$ Oh, and it also fixes the falstrue issue. \$\endgroup\$
    – Glen O
    Nov 8, 2015 at 5:35
  • \$\begingroup\$ @GlenO That's great, thanks \$\endgroup\$
    – Alex A.
    Nov 8, 2015 at 6:12
  • \$\begingroup\$ @feersum Fixed now \$\endgroup\$
    – Alex A.
    Nov 8, 2015 at 6:12
2
\$\begingroup\$

Javascript, 135 Bytes

function(s){return s.split("").reverse().join("").replace(/eslaf/i,'☺').replace(/eurt/i,'☻').replace(/☻/g,!1).replace(/☺/g,!1)}

Test:

=>"false is the opposite of true"

<="true fo etisoppo eht si false"

Thanks ProgramFOX and edc65 for pointing out a bug!

\$\endgroup\$
6
  • \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! The question requests a full program or a function, so not just a code snippet that assumes that a variable s exists. Also, your code does not work for falstrue: it should output falseslaf, not trueslaf. Do you want to fix these issues? Thanks! :) \$\endgroup\$
    – ProgramFOX
    Nov 8, 2015 at 7:58
  • \$\begingroup\$ @ProgramFOX Thank you! I'll get right on that! \$\endgroup\$
    – Fuzzyzilla
    Nov 8, 2015 at 8:00
  • 1
    \$\begingroup\$ You can save a few bytes if you use ES6, it has this arrow-function syntax: f=s=>s.split(""). .... \$\endgroup\$
    – ProgramFOX
    Nov 8, 2015 at 8:12
  • \$\begingroup\$ This is both overly complex and wrong. Test with 'false1' or 'true0' or 'true1' \$\endgroup\$
    – edc65
    Nov 8, 2015 at 8:14
  • \$\begingroup\$ @edc65 Why thank you, kind sir! \$\endgroup\$
    – Fuzzyzilla
    Nov 8, 2015 at 8:20
2
\$\begingroup\$

Java, 162 98 92 bytes

Thanks (and sorry!~) to @DanielM. for telling me about StringBuffer and the fact that we can use functions!

Because, you know, Java.

s->(""+new StringBuffer(s.replaceAll("false","eurt")).reverse()).replaceAll("eurt","false");

Returns the correct, reversed string.

Ungolfed Version:

s->new StringBuilder(
    s.replaceAll("false","eurt"))
    .reverse().toString().replaceAll("eurt","false");

Basically, I replace all instances of "false" with a backwards "true", then reverse the entire string, and then replace the now backwards versions of "true" (not the ones I just replaced) with "false". Easy peasy.

\$\endgroup\$
7
  • \$\begingroup\$ StringBuffer is a byte shorter. Also, functions are allowed. \$\endgroup\$
    – Daniel M.
    Nov 8, 2015 at 13:07
  • \$\begingroup\$ I'm preparing another answer in a different language, so you can keep this \$\endgroup\$
    – Daniel M.
    Nov 8, 2015 at 13:33
  • \$\begingroup\$ The return is implicit when dealing with one-line lambdas \$\endgroup\$
    – Daniel M.
    Nov 8, 2015 at 13:39
  • 4
    \$\begingroup\$ Java beat Python? Now surely the end is upon us \$\endgroup\$
    – Downgoat
    Nov 8, 2015 at 17:40
  • 1
    \$\begingroup\$ @GaborSch Nifty. :P Thanks! \$\endgroup\$ Jun 8, 2016 at 16:50
1
\$\begingroup\$

Mathematica, 64 bytes

StringReverse@#~StringReplace~{"eurt"->"false","eslaf"->"true"}&
\$\endgroup\$
1
\$\begingroup\$

Python 3, 68 100 bytes

I'm still golfing it, but it's fixed to the bug, so falstrue -> falselsaf and trufalse -> trueurt

Pretty straightforward:

print(input()[::-1].replace("eurt","false").replace("eslaf","true").replace("falstrue","falseslaf"))
\$\endgroup\$
5
  • 3
    \$\begingroup\$ This does not work correctly for the input falstrue. \$\endgroup\$
    – feersum
    Nov 8, 2015 at 2:04
  • \$\begingroup\$ This can be easily remedied by switching the replace statements (print(input()[::-1].replace("eslaf","true").replace("eurt","false"))) \$\endgroup\$
    – Beta Decay
    Nov 8, 2015 at 10:04
  • \$\begingroup\$ @BetaDecay, that will not work, because trufalse becomes trufalse, while it needs to be trueurt \$\endgroup\$
    – Adnan
    Nov 8, 2015 at 10:07
  • \$\begingroup\$ @Adriandmen However, that's not one of the example I/Os so it will suffice for now :) \$\endgroup\$
    – Beta Decay
    Nov 8, 2015 at 10:08
  • \$\begingroup\$ @BetaDecay Haha, they won't notice :) \$\endgroup\$
    – Adnan
    Nov 8, 2015 at 10:15
1
\$\begingroup\$

Japt, 26 bytes

Note: This may be invalid, as it requires bug fixes made after this challenge was posted.

Uw r"eurt|eslaf",X=>X<"et"

Try it in the online interpreter! (Arrow function requires ES6-compliant browser, such as Firefox.)

How it works

             // Implicit: U = input string
Uw r         // reverse U, then replace:
"eurt|eslaf" // occurrences of either "eurt" or "eslaf"
X=>X<"et"    // with "false" or "true", respectively
             // Implicit: output last expression

Here's a version that worked before the bug fixes: (38 bytes)

Uw $.replace(/eurt|eslaf/g,X=>X<"et")$
\$\endgroup\$
0
1
\$\begingroup\$

Gema, 43

*=@a{@reverse{*}};a:eurt=false;a:eslaf=true

(Gema is an obscure macro language.)

\$\endgroup\$
1
\$\begingroup\$

Pyth, 28 22

Amr`!dZ2jHjLGcR_Hc_z_G

6 bytes thanks to Jakube

Works correctly for falstrue, as shown in the suite below.

Test suite

\$\endgroup\$
1
  • \$\begingroup\$ No, I think I made a mistake. You 22 byte solution is correct. \$\endgroup\$
    – Jakube
    Nov 9, 2015 at 5:52
1
\$\begingroup\$

Haskell, 102 bytes

h('t':'r':'u':'e':s)="eslaf"++h s
h('f':'a':'l':'s':'e':s)="eurt"++h s
h(x:s)=x:h s
h[]=""
r=reverse.h

The replacement of "true" by "false" and vice-versa is quite lengthy with the pattern-matching, but at least it deals correctly with "falstrue" and the like. And besides, I suspect that a correct regex-based version would be a bit longer.

\$\endgroup\$
1
\$\begingroup\$

Python 3 - 108 92 bytes

import re
print(re.sub("eslaf|eurt",lambda m:repr(len(m.group(0))>4).lower(),input()[::-1]))

Uses a regex to match on "true" or "false" and uses a lambda to process matches and choose what to use as a replacement string. Using repr gets the string representation of (len(match)>4) which gives "True" when "false" is matched and vice versa (and use .lower() because repr(bool) gives a capitalized string) to get the inverse of the match and finish up by reversing the replacement and then the processed input using [::-1]

Managed to get the length down 16 bytes from TFelds suggestions.

Edit: Python is back in front of java, no need for alarm.

\$\endgroup\$
3
  • 5
    \$\begingroup\$ We require here that programs work for all inputs, not just for the given test cases. \$\endgroup\$
    – lirtosiast
    Nov 8, 2015 at 17:30
  • \$\begingroup\$ You can save 6 bytes by reversing the string first, and then replacing (saving one [::-1]) print(re.compile("eslaf|eurt").sub(lambda m:repr(m.group(0)!="eurt").lower(),input()[::-1])) \$\endgroup\$
    – TFeld
    Nov 11, 2015 at 9:57
  • \$\begingroup\$ You can also get away with not using re.compile print(re.sub("eslaf|eurt",lambda m:repr(m.group(0)!="eurt").lower(),input()[::-1])) Change m.group(0)!="eurt" to len(m.group(0))>4 (for 1 more) \$\endgroup\$
    – TFeld
    Nov 11, 2015 at 10:21
1
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 40 chars / 65 bytes

ô[…ïċ/false|true/g,⇏[…⬯+!שúa]ù⬮ø⬯)]ù⬮ø⬯)

Try it here (Firefox only).

Thanks to @feersum for pointing out a bug!

21 chars / 43 bytes, non-competitive

ôᴙ(ïċ/⊭|⊨⌿,↪ᴙ(⬯+!ë$⸩⸩

Try it here (Firefox only).

\$\endgroup\$
1
  • 2
    \$\begingroup\$ This does not work correctly for the input falstrue. \$\endgroup\$
    – feersum
    Nov 8, 2015 at 2:02
1
\$\begingroup\$

Prolog, 225 bytes

p(X):-string_to_list(X,L),reverse(L,B),q(B,C),string_to_list(Z,C),write(Z),!.
q([],[]).
q([101,117,114,116|T],[102,97,108,115,101|L]):-q(T,L).
q([101,115,108,97,102|T],[116,114,117,101|L]):-q(T,L).
q([H|T],[H|L]):-q(T,L).

Try it out online here
Run by querying in the following way:

p("falstrue").
\$\endgroup\$
1
\$\begingroup\$

Japt, 15 bytes

Ôr`laf|e¨t`Èøf

Try it

\$\endgroup\$
0
\$\begingroup\$

Ruby, 55 bytes

->s{s.gsub(/true|false/){$&[?t]?:eslaf: :eurt}.reverse}

Test:

->s{s.gsub(/true|false/){$&[?t]?:eslaf: :eurt}.reverse}["false,true,undefined"]
=> "denifednu,false,true"
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 53 bytes \$\endgroup\$
    – Jordan
    Oct 18, 2022 at 0:30
0
\$\begingroup\$

Perl 5, 68 bytes

67 plus 1 for -E instead of -e

%a=(false,eurt,true,eslaf);say~~reverse<>=~s/false|true/$a{$&}/gr
\$\endgroup\$
0
\$\begingroup\$

OpenSCAD, 178 bytes

(Note that this uses the String Theory library, as OpenSCAD doesn't exactly have a standard library. Additionally, this is a function because the only allowed input is to hard-code it.

use <Strings.scad>;function f(a)=contains(g(a),"eurt")?replace(g(a),"eurt","false"):g(a);function g(a)=contains(reverse(a),"eslaf")?replace(reverse(a),"eslaf","true"):reverse(a);
\$\endgroup\$
0
\$\begingroup\$

C#, 260 bytes

using System;class P{static void Main(){var y=Console.ReadLine();char[] n=y.ToCharArray();Array.Reverse(n);var s=new string(n);if(s.Contains("eslaf")){s=s.Replace("eslaf","true");}if(s.Contains("eurt")){s=s.Replace("eurt","false");}Console.WriteLine(s);}}
\$\endgroup\$
1
  • \$\begingroup\$ This is my first (real) post - I've seen someone using c# above didn't include: using system; class P{ }, static void main(), Console.WriteLine(); or Console.ReadLine();. These obviously take on a lot of bytes for me - if there is a way of golfing that or if it is excluded from code-golf - let me know :) \$\endgroup\$
    – Belfield
    Nov 9, 2015 at 14:27
0
\$\begingroup\$

PHP, 60 bytes

Simple, reverses the string first, then replaces the reversed versions with their respective swaps.

"falstrue" becomes "eurtslaf" becomes "falseslaf".

<?=strtr(strrev($argv[1]),{'eurt'=>'false','eslaf'=>'true'})
\$\endgroup\$
0
\$\begingroup\$

Perl 5.10, 54 bytes

$_=reverse<>;s/(eurt)|(eslaf)/$1?"false":"true"/eg;say

Reverse, then replace. A different way of doing it besides the hash table used for the other Perl answer, which ends up being shorter!

Try it online.

\$\endgroup\$
-1
\$\begingroup\$

Pyth - 18 bytes

Can be golfed a lot more.

Amr`!dZ2::_z_GH_HG

Test Suite.

\$\endgroup\$
1
  • 6
    \$\begingroup\$ This does not work correctly for the input falstrue. \$\endgroup\$
    – feersum
    Nov 8, 2015 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.