5
\$\begingroup\$

Your dumb teacher is always wrong, so flip his answer.

Change these responses

True -> False
Yes -> No 
0 -> 1
Yeah -> Nope

and vice versa. Keeping punctuation and the first letter's capitalization counts. Only one condition will be passed to the program.

You only need to accept the following test cases:

True <-> False
True. <-> False.
true <-> false
true. <-> false.
Yes <-> No
Yes. <-> No.
yes <-> no
yes. <-> no.
0 <-> 1  
Yeah <-> Nope
yeah <-> nope
Yeah. <-> Nope.
yeah. <-> nope.

This is , so the shortest answer in bytes wins.

\$\endgroup\$

12 Answers 12

8
\$\begingroup\$

05AB1E, 45 41 40 39 38 bytes

Code:

“‰¦žä¥æ€¸pe…Ü€¸“ð¡2ôDí«D€™«vy`:D_D¹Êiq

Explanation:

It first starts with the compression:

“‰¦žä¥æ€¸pe…Ü€¸“

This is a compressed version of "true false yeah nope yes no". After that, we split on the spaces with ð¡, resulting into:

['true', 'false', 'yeah', 'nope', 'yes', 'no']

We slice this array into pieces of 2 with , resulting into:

[['true', 'false'], ['yeah', 'nope'], ['yes', 'no']]

We duplicate this array and reverse each element of it with . Then we append this to our first array with «, resulting into:

[['true', 'false'], ['yeah', 'nope'], ['yes', 'no'], ['false', 'true'], ['nope', 'yeah'], ['no', 'yes']]

To get the titlecased words, we duplicate the array again and convert each word to titlecase with €™. Finally, this is appended to the initial array. So, the final list is:

[['true', 'false'],
 ['yeah', 'nope'],
 ['yes', 'no'],
 ['false', 'true'],
 ['nope', 'yeah'],
 ['no', 'yes'],
 ['True', 'False'],
 ['Yeah', 'Nope'],
 ['Yes', 'No'],
 ['False', 'True'],
 ['Nope', 'Yeah'],
 ['No', 'Yes']]

The imporant part here is that Nope comes before No. Eventually, we get to this part of the code:

vy`:D_D¹Êiq

Explanation:

v            # For each in the array...
 y`          #   Push the array (containing 2 words) and flatten.
   :         #   Replace the first word with the second.
    D_       #   Duplicate and negate (resulting 0 into 1 and 1 into 0).
                 If this is not possible, this results into nothing.
      D      #   Duplicate the top of the stack again.
       ¹Ê    #   If it's not equal to the inital input string...
         iq  #     Quit the program and implicitly print the processed string.

Uses the CP-1252 encoding. Try it online!.

\$\endgroup\$
2
\$\begingroup\$

Pyth, 44 bytes

j\.XXXcz\.`M2)Jc."<unprintables>"\b)rR3J

Hexdump:

00: 6A5C 2E58 5858 637A 5C2E 604D 3229 4A63
10: 2E22 6179 0862 3C40 D23B 9A54 C2B4 A20A
20: FEE7 8701 225C 6229 7252 334A

Test suite.

\$\endgroup\$
  • \$\begingroup\$ updated question. \$\endgroup\$ – noɥʇʎԀʎzɐɹƆ Jun 11 '16 at 15:55
  • \$\begingroup\$ What are "<unprintables>"? \$\endgroup\$ – noɥʇʎԀʎzɐɹƆ Jun 11 '16 at 17:52
  • \$\begingroup\$ See hexdump or test-suite. \$\endgroup\$ – Leaky Nun Jun 12 '16 at 1:44
2
\$\begingroup\$

TI-Basic, 217 bytes

Longer than I thought it would be...

Input Str1
If Str1="0
Then
Disp 1
Stop
End
"e"=sub(Str1,2,1
sub("FalsefalseNopenope",1+5not(Ans)(sub(Str1,1,1)="t")+10Ans+4Ans(sub(Str1,1,1)="y"),5-Ans-2(sub(Str1,3,1)="s"))+sub(" .",1+(sub(Str1,length(Str1),1)="."),1
\$\endgroup\$
  • \$\begingroup\$ Can’t you save a byte by using ‘Return’ instead of ‘Stop’, since Return is a 1-byte token? \$\endgroup\$ – Hello Goodbye Nov 22 '19 at 16:45
  • \$\begingroup\$ @HelloGoodbye Good question. "Return" could be used instead of "Stop" here; since there are no subprograms, the behavior would be identical. However, Stop is also a 1-byte token (0xD9), so there is no compelling reason to use one over the other. \$\endgroup\$ – Timtech Dec 2 '19 at 14:59
  • \$\begingroup\$ Hmm. My brain was for some reason telling me that Stop was 2 bytes. I must have been thinking of something else. Sorry! \$\endgroup\$ – Hello Goodbye Dec 2 '19 at 16:14
  • \$\begingroup\$ @HelloGoodbye No problem! Here's a useful page (which you may already know about) that I refer to frequently to check if tokens are 1 byte: tibasicdev.wikidot.com/one-byte-tokens \$\endgroup\$ – Timtech Dec 2 '19 at 18:09
  • \$\begingroup\$ Yep, that’s the one I use! \$\endgroup\$ – Hello Goodbye Dec 3 '19 at 16:19
2
\$\begingroup\$

Python 3, 227 bytes

def f(x):l=["True","False","True.","False.","true","false","true.","false.","Yes","No","yes","no","Yes.","No.","yes.","no.","0","1","Yeah","Nope","Yeah.","Nope.","yeah","nope","yeah.","nope."];r=l.index(x);return l[r+1-2*(r%2)]

Ungolfed form:

def f(x):
    l=["True","False","True.","False.","true","false","true.","false.","Yes","No","yes","no","Yes.","No.","yes.","no.","0","1","Yeah","Nope","Yeah.","Nope.","yeah","nope","yeah.","nope."]
    r=l.index(x)
    return l[r + 1 -2*(r % 2)]
\$\endgroup\$
  • \$\begingroup\$ Hello, and welcome to PPCG! \$\endgroup\$ – NoOneIsHere Jun 18 '16 at 3:52
1
\$\begingroup\$

Pyke, 51 44 bytes

.d̾ॗǣ/ற㻯dc2cDMl4+D.F\.+)+T`]1+Dm_+Y@

Try it here!

.d̾ॗǣ/ற㻯dc2c                         - {'yeah': 'nope', 'true': 'false', 'yes': 'no'}
             DMl4+                    - ^ += map(^, str.title())
                  D.F\.+)+            - ^ += map(^+".")
                          T`]1+       - ^ += [repr(10)]
                               Dm_+   - ^ += map(^, reversed)
                                   Y@ - dict(^)[input]
\$\endgroup\$
  • \$\begingroup\$ Hexdump: 2E64 CCBE E0A5 97C7 A32F E0AE B1E3 BBAF 6463 3263 444D 6C34 2B44 2E46 5C2E 2B29 2B54 605D 312B 446D 5F2B 5940 \$\endgroup\$ – Leaky Nun Jun 12 '16 at 13:04
  • \$\begingroup\$ It is actually 44 bytes, unless there's a null byte somewhere. \$\endgroup\$ – Leaky Nun Jun 12 '16 at 13:05
  • \$\begingroup\$ @LeakyNun I'm still getting 45 bytes... It doesn't have a null byte in it \$\endgroup\$ – Blue Jun 12 '16 at 14:57
  • \$\begingroup\$ Mind putting down your hexdump? \$\endgroup\$ – Leaky Nun Jun 12 '16 at 23:43
  • \$\begingroup\$ 2e 64 06 CC BE E0 A5 97 C7 A3 2F E0 AE B1 E3 BB AF 64 63 32 63 44 4d 6c 34 2B 44 2e 46 5C 2E 2B 29 2B T 60 5D 31 2B 44 6d 5f 2B 59 40 - Ok, 44 then \$\endgroup\$ – Blue Jun 13 '16 at 7:42
1
\$\begingroup\$

Perl 6,  86  81 bytes

{my@a=<true yes yeah 1 0 nope no false>;my%h=@a Z=> reverse @a;S:ii/(@a)/{%h{lc $0}}/}
{my@a=<true yes yeah 1 0 nope no false>;S:ii/(@a)/{%(@a Z=> reverse @a){lc $0}}/}

Explanation:

{
  # list of responses ordered in such a way that
  # reversing it will give you the opposite response
  my @a = <true yes yeah 1 0 nope no false>;

  # create a lookup table
  my %h = @a Z=> reverse @a;

  # the :samecase / :ii modifier causes the regex to call .samecase
  # on the replacement, and also turns on :ignorecase / :i
  S:samecase /
    (@a) # match against the values in @a
  /{
    %h{lc $0}
  }/
}

Test:

#! /usr/bin/env perl6

use v6.c;
use Test;

my @tests = (
  < True False >, < True. False. >, < true false >, < true. false. >,
  < Yes  No >, < Yes. No. >, < yes  no >, < yes. no. >,
  < 0 1 >,
  < Yeah Nope >, < yeah nope >, < Yeah. Nope. >, < yeah. nope. >,
);

plan @tests * 2;

my &answer-flip = {my@a=<true yes yeah 1 0 nope no false>;my%h=@a Z=> reverse @a;S:ii/(@a)/{%h{lc $0}}/}

for @tests -> ($a,$b) {
  is answer-flip($a), $b, "$a => $b";
  is answer-flip($b), $a, "$b => $a";
}

Which prints out 1..26 on a line, followed by 26 lines of ok ... messages.

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

Java, 332 274 205 bytes

String s(String p){String[]l={"True","true","Yes","yes","Yeah","yeah","0","1","nope","Nope","no","No","false","False"};for(int j=0;j<14;j++)if(p.matches(l[j]+".?"))return p.replace(l[j],l[13-j]);return"";}

Gotta love the uncreative Java golf! Credit to @Leaky Nun for helping!

\$\endgroup\$
  • 1
    \$\begingroup\$ for(int j=0;j<14;j++)if(p.matches(l[j]))return p.replace(l[j],l[14-j]); (I'll let you figures out which 14 strings to keep :p ) \$\endgroup\$ – Leaky Nun Jun 18 '16 at 4:00
0
\$\begingroup\$

Python 154 bytes

a=['True','True.','true','Yes','Yes.','yes','Yeah','Yeah.','yeah','0','False','False.','false','No','No.','no','Nope','Nope.','nope',1]
a[a.index(i)+10]
\$\endgroup\$
0
\$\begingroup\$

Emacs Lisp 221 Bytes

(defun g(a b)(when(string-match a s)(throw s(replace-match b'()'()s))))
(defun f(s)(catch s(dolist(l'(("true"."false")("yeah"."nope")("yes"."no")("0"."1")))(let((a(car l))(d(cdr l))(case-fold-search t))(g a d)(g d a)))))

Ungolfed:

(defun g (a b)
  ;; when a is in s (dynamical scope) throws the replacement string
  (when (string-match a s)
    (throw s (replace-match b nil nil s))))

(defun f (s)
  (catch s ; catch replacement
    (dolist (l'(("true"."false")("yeah"."nope")("yes"."no")("0"."1")))
      (let ((a(car l))
            (d(cdr l))
            ;; Emacs string-match ignores case, when case-fold-search is true.
            ;; The standard is t, but through customization/local variables
            ;; this can change. Therefore we set it explicitly.
            ;; We could have saved 20 bytes here...
            (case-fold-search t))
        (g a d)
        (g d a)))))

The main "feature" here, is that string-match can be made to ignore case and that replace-match is somewhat intelligent with replacing. For instance, this would also work with TRUE and FALSE. We also exploit dynamical scope to save typing.

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

PowerShell, 113 bytes

$x=($s=(echo True False Yes No 0 1 Yeah Nope)|%{$_;$_|% *wer}|%{$_;"$_."}).indexOf("$args")
$s[$x+4-8*($x%8-ge4)]

Try it online!

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

Python 3, 121 bytes

def f(i):a=[(w,w.title())[i<'a']+'.'[i[-1]>'.':]for w in'true yes yeah 1 false no nope 0'.split()];return a[a.index(i)-4]

Try it online!

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

05AB1E, 32 bytes

l'.¡“0žä€¸€¸pe¥æ…܉¦ 1“#‡'.ýs.Ï

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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