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>
true true false false
menjaditrue true false false
atautrue eslaf false eurt
?eurt
menjadifalse
atautrue
?eurt
->true
1 3 2 4 5
trufalse -> trueurt
?Jawaban:
C # 6, 144 byte
Itu menggunakan ekspresi reguler untuk mencocokkan
true|false
, dan jika cocoktrue
itu akan digantikan oleheslaf
, jika tidak oleheurt
.m.Value[0]<'g'
adalah cara yang lebih singkat untuk mengatakanm.Value[0]=="false"
, karena satu-satunya nilai yang mungkin untukm.Value
are"true"
atau"false"
, jadi jika kode char dari char pertama lebih kecil dari kode char'g'
, itu"false"
.Versi lama, 95 byte
Yang ini memiliki bug, tidak mengembalikan output yang benar untuk
falstrue
.sumber
falstrue
.falstrue
diperbaiki, itu tidak lagi <100 byte ... :(TeaScript ,
362524 byteTeaScript adalah JavaScript untuk bermain golf.
Suntingan: Disimpan 11 byte berkat @ Vɪʜᴀɴ. Memperbaiki input
falstrue
dan menyimpan satu byte.Versi lama (Tidak Valid):
Penjelasan:
sumber
l
dani
bukannyat
danf
, Anda dapat mengabaikan.
. f juga sudah ditentukan sebelumnyafalse
sehingga Anda bisa mendapatkan:xv¡g(l="eurt",i=f+¢)g(iv¡,lv¡)
xv¡g("eurt",f)g(f.T¡v¡,t)
(
setelah metode? Sukareplace(/(\.[BcCdeE...])/g,"$1(")
atau serupa setelah memasukkan periode.falstrue
.Bash+GNU,
453873 bytesEdit: works with both
trufalse
andfalstrue
Old version, 38 bytes (shortened, thanks to Digital Trauma):
sumber
rev|sed s/eurt/false/g\;s/eslaf/true/g
falstrue
.trufalse
.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).
Test running the snippet below in an EcmaScript 6 compliant browser.
sumber
Windows Batch,
184213 bytesFixed the bug,
falstrue -> falseslaf
andtrufalse -> trueurt
Probably one of the less popular languages:
sumber
falstrue
.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.
sumber
f[]=[]
line and instead putf x=x
at the bottom to save a byte.JavaScript ES6,
9593 bytesUnnamed function. Add
f=
to the beginning to use it. Thanks Ismael! Also assumes that the input does not contain tabs.sumber
.replace(/eurt/g,false).replace(/eslaf/g,true)
, since they will be converted to string. Trytrue + ''
(should return'true'
)false
with!1
andtrue
with!0
. There, a few bytes shorter[...x]
instead of x.split''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.Pyth, 30 bytes
This reverses the input (
_z
), substitutes"eurt"
for"false"
and"eslaf"
for"true"
. Replacement is done using:
.Try it online
sumber
"true"
and"false"
as a variable:=d"true"=k"false"::_z_dk_kd
, and use the reverse of it. It will save 3 bytes.::_zJ"eurt"K"false"_K_J
is 23 bytes.falstrue
.rs, 56 bytes
Live demo and test cases.
sumber
falstrue
.Julia,
595546 bytesThis 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 expressioneurt|eslaf
which matchestrue
orfalse
backwards. To the match we apply a function that returnstrue
if the match is lexicographically smaller thanet
(i.e.eslaf
) andfalse
otherwise. The boolean literals are converted to string in the output.Saved 9 bytes and fixed an issue thanks to Glen O!
sumber
falstrue
.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 totrue
if it was "elsaf" and tofalse
if it was "eurt". 46 bytes.falstrue
issue.Javascript, 135 Bytes
Test:
=>
"false is the opposite of true"<=
"true fo etisoppo eht si false"Thanks ProgramFOX and edc65 for pointing out a bug!
sumber
s
exists. Also, your code does not work forfalstrue
: it should outputfalseslaf
, nottrueslaf
. Do you want to fix these issues? Thanks! :)f=s=>s.split(""). ...
.Java,
1629892 bytesThanks (and sorry!~) to @DanielM. for telling me about StringBuffer and the fact that we can use functions!
Because, you know, Java.
Returns the correct, reversed string.
Ungolfed Version:
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.
sumber
return
is implicit when dealing with one-line lambdasMathematica, 64 bytes
sumber
Python 3,
68100 bytesI'm still golfing it, but it's fixed to the bug, so
falstrue -> falselsaf
andtrufalse -> trueurt
Pretty straightforward:
sumber
falstrue
.print(input()[::-1].replace("eslaf","true").replace("eurt","false"))
)trufalse
becomestrufalse
, while it needs to betrueurt
Japt, 26 bytes
Note: This may be invalid, as it requires bug fixes made after this challenge was posted.
Try it in the online interpreter! (Arrow function requires ES6-compliant browser, such as Firefox.)
How it works
Here's a version that worked before the bug fixes: (38 bytes)
sumber
Gema, 43
(Gema is an obscure macro language.)
sumber
Pyth,
28226 bytes thanks to Jakube
Works correctly for
falstrue
, as shown in the suite below.Test suite
sumber
Haskell, 102 bytes
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.
sumber
Python 3 -
10892 bytesUses 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.
sumber
[::-1]
)print(re.compile("eslaf|eurt").sub(lambda m:repr(m.group(0)!="eurt").lower(),input()[::-1]))
print(re.sub("eslaf|eurt",lambda m:repr(m.group(0)!="eurt").lower(),input()[::-1]))
Changem.group(0)!="eurt"
tolen(m.group(0))>4
(for 1 more)𝔼𝕊𝕄𝕚𝕟, 40 chars / 65 bytes
Try it here (Firefox only).
Thanks to @feersum for pointing out a bug!
21 chars / 43 bytes, non-competitive
Try it here (Firefox only).
sumber
falstrue
.Prolog, 225 bytes
Try it out online here
Run by querying in the following way:
sumber
Ruby, 55 bytes
Test:
sumber
Perl 5, 68 bytes
67 plus 1 for
-E
instead of-e
sumber
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.
sumber
C#, 260 bytes
sumber
PHP, 60 bytes
Simple, reverses the string first, then replaces the reversed versions with their respective swaps.
"falstrue" becomes "eurtslaf" becomes "falseslaf".
sumber
Perl 5.10, 54 bytes
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.
sumber
Pyth - 18 bytes
Can be golfed a lot more.
Test Suite.
sumber
falstrue
.