21
\$\begingroup\$

After inputting a string [length 1-20], only containing the chars y for yes and n for no, your program should output the result (y or n). Example input: yynynynny would output y.

The result is determined by combining the y's and n's in the following way:

  • yes and no equals no

  • yes and yes equals yes

  • no and no equals yes

If the string contains more than 2 characters (likely...), the calculation would look the same. Examples:

  • yes and yes and no equals no (because the no merges with the first yes to no. then there are no and yes left and the same thing happens again)

  • no and no and no equals no (the first two no's merge to yes, then there are yes and no left, which emerge to no)

Example input with output:

  • yynynynynyyn = n

Tip: have in mind that the order of the chars your program works off doesn't care. (for example you can read the input from back, or from behind, mix the letters, sort it, whatever. What counts is the correct ouptput) have fun!

Winning criteria: this is , so shortest code in byte wins.

\$\endgroup\$
21
  • 4
    \$\begingroup\$ Congratulations on the first challenge with clear specification! (although it's unfortunate that some community members don't like "too trivial" challenges....) \$\endgroup\$
    – user202729
    Commented May 22, 2018 at 15:30
  • 4
    \$\begingroup\$ Very similar. \$\endgroup\$
    – user202729
    Commented May 22, 2018 at 15:40
  • 8
    \$\begingroup\$ Can we output an alternative pair? Say 1 for y, and 0 for n. \$\endgroup\$
    – Oliver
    Commented May 22, 2018 at 17:27
  • 6
    \$\begingroup\$ Can we take input as a list of characters ie ["y", "n", "n"] \$\endgroup\$
    – Okx
    Commented May 22, 2018 at 17:34
  • 4
    \$\begingroup\$ Since the duplicate of this challenge was heavily downvoted, I don't think it's very helpful to close this as a duplicate. If anything, the older challenge should be a duplicate of this one since it's policy to leave the better challenge open I've reopened this challenge \$\endgroup\$
    – DJMcMayhem
    Commented Jun 1, 2018 at 19:51

49 Answers 49

15
\$\begingroup\$

Octave, 29 27 bytes

Thanks to @RickHithcock for pointing out a mistake, now corrected. Also, 2 bytes off thanks to @StewieGriffin!

@(s)'yn'(mod(sum(s+1),2)+1)

Try it online!

Explanation

The ASCII code point of 'y' is odd, and that of 'n' is even. The code

  1. adds 1 to each char in the input string to make 'y' even and 'n' odd;
  2. computes the sum;
  3. reduces the result to 1 if even, 2 if odd;
  4. indexes (1-based) into the string 'yn'.
\$\endgroup\$
2
  • \$\begingroup\$ I'm probably missing something obvious, but this seems to function the same way in a few limited test cases for -4 bytes. It's probably wildly wrong because I don't know Octave! \$\endgroup\$ Commented May 23, 2018 at 12:17
  • 2
    \$\begingroup\$ @DomHastings Fails for yynynynny given in the OP, should return y, but returns n \$\endgroup\$
    – Mayube
    Commented May 23, 2018 at 13:14
10
\$\begingroup\$

Charcoal, 6 bytes

§yn№Sn

Try it online! Link is to verbose version of code. Explanation:

    S   Input string
   № n  Count number of `n`s
§yn     Circularly index into string `yn`
        Implicitly print appropriate character
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Explain how it works, please? \$\endgroup\$
    – Malady
    Commented May 23, 2018 at 4:42
  • \$\begingroup\$ @Malandy Link is to verbose version of code. \$\endgroup\$
    – Adám
    Commented May 23, 2018 at 5:24
  • 1
    \$\begingroup\$ @Adám Actually I usually add one, however I had just dashed this off in a work break and forgotten to edit one in. \$\endgroup\$
    – Neil
    Commented May 23, 2018 at 7:51
10
\$\begingroup\$

JavaScript (ES6), 28 bytes

Takes input as a string.

s=>'ny'[s.split`n`.length&1]

Try it online!


JavaScript (ES6), 30 bytes

Takes input as an array of characters.

y=>'yn'[n=1,~~eval(y.join`^`)]

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ 31: s=>'yn'[s.match(/n/g).length&1] :P \$\endgroup\$
    – ASCII-only
    Commented May 23, 2018 at 22:07
  • \$\begingroup\$ @ASCII-only This would fail on strings that do not contain at least one n. \$\endgroup\$
    – Arnauld
    Commented May 23, 2018 at 22:47
  • \$\begingroup\$ Oh, it would. Oops >_> \$\endgroup\$
    – ASCII-only
    Commented May 23, 2018 at 22:54
9
\$\begingroup\$

Haskell, 33 28 bytes

f a=cycle"yn"!!sum[1|'n'<-a]

Indexes the count of n's into the infinite list "ynynynyn…". Previous approach (33 bytes) was folding pairs of different elements to n, otherwise y:

f=foldl1(\a b->last$'y':['n'|a/=b])

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Your previous approach can be done in 30 bytes. Try it online! \$\endgroup\$
    – Wheat Wizard
    Commented May 23, 2018 at 15:14
8
\$\begingroup\$

Jelly, 7 bytes

ċ”nị⁾ny

Try it online!

ċount number of ”n, ndex into the string ⁾ny. (with modulo 2)


ċḢịɗ⁾ny

Try it online!

{ċount number of, take the ead, then ndex into} string ⁾ny.


OCSị⁾ny

Try it online!

Similar to the Octave answer above. Calculate Ord value, take the Complement (for each ord value x calculate 1-x), Sum, then ndex into string ⁾ny.

\$\endgroup\$
1
  • \$\begingroup\$ It was my bogus solution confusing me! \$\endgroup\$ Commented May 22, 2018 at 15:51
8
\$\begingroup\$

APL (Dyalog Unicode), 15 bytes

'ny'[1+=/'y'=⍞]

Try it online!

Note: TIO defaults to ⎕IO = 1. If run with ⎕IO←0,

APL (Dyalog Unicode), 13 bytes

'ny'[=/'y'=⍞]

Try it online!

This is the XNOR function (sometimes called EQV, especially in old BASICs.

Decomposition/Analysis:

             ⍞  - Accept string input  
         'y'=   - Compare it to the letter `y`. This "converts" the input 
                  string into a vector of 1s and 0s where the 1s correspond 
                  to 'y' and the 0s to 'n'.  
       =/       - XNOR/EQV/equality reduction - converts the vector into a 
                  single boolean value by evaluating e.g., 1 xnor 0 xnor 0 
                  xnor 1 ...  
     1+         - adds one for subscripting in ⎕IO = 1 environment. In 
                  ⎕IO = 0, should be omitted (save 2 bytes)  
    [         ] - subscript indicator - the expression, which should be 
                  either a 1 or 2 (0 or 1 in `⎕IO = 0`), is now going to be 
                  interpreted as a subscript of...  
'ny'            - The string of possible results - a 0/1 is 'n', a 1/2 is 'y'
\$\endgroup\$
2
  • \$\begingroup\$ While XOR ignores 0s and flips on 1s, XNOR ignores 1s and flips on 0s, “initially” being at 1 instead of 0 like XOR. \$\endgroup\$
    – FrownyFrog
    Commented May 23, 2018 at 11:56
  • \$\begingroup\$ @FrownyFrog - I suppose you could look at it that way... or you could look at it as being a check to see if both of its input values are the same. \$\endgroup\$ Commented May 23, 2018 at 11:58
7
\$\begingroup\$

Python 2, 29 bytes

lambda s:'yn'[s.count('n')%2]

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Pyth, 9 bytes

@"yn"l@\n

Try it here

Explanation

@"yn"l@\n
     l@\nQ   Get the length of the intersection of the (implicit) input and "n".
@"yn"        Modular index into "yn".
\$\endgroup\$
0
6
\$\begingroup\$

dc, 39

?dsiZdsl[[]r1-d0<m]dsmxklixzll-2%B*C1+P

Input string is read from STDIN and should be in the form [yynynynynyyn].

dc is not known for its string handling, but we have just enough here to get this to work. The approach here is to count the ns, and output y if even or n if odd. This is done by executing the input string as a macro. dc will output 'y' (0171) unimplemented errors for all the ys and attempt to pop strings and print them for all the ns. So first we make sure we have plenty (total input string length) of empty strings [] on the stack to pop. Then we execute the input string and see how many [] are left on the stack. The original string length is subtracted from this to give the (-ve) total number of ns. The rest is arithmetic to do mod 2 and have the output come out right as ASCII y or n.

?dsi                                    # Read input string, duplicate, store in register i
    Zdsl                                # Get input length, duplicate, store in register l
        [         ]                     # define macro to:
         []                             #   push empty string
           r                            #   swap empty string and remaining length 
            1-                          #   subtract 1 from length
              d0                        #   duplicate and compare with 0
                <m                      #   if >0 recursively call this macro again
                   dsmx                 # duplicate macro, store in register m and execute
                       k                # discard left-over 0
                        lix             # load input string and execute as macro
                           z            # get stack length
                            ll-         # load string length and subract
                               2%       # mod 2 (result is -ve because difference is -ve)
                                 B*     # multiply by 11 ('y' - 'n')
                                   C1+  # add 121 ('y')
                                      P # print result as ASCII char

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Japt, 8 bytes

"yn"gUèn

Try it online!

Explanation:

"yn"gUèn
"yn"       String literal - "yn"
    g      Return the char at index:   
      è      Number of matches where:
       n       "n" is found in
     U         Input

Japt uses index-wrapping, so if Uèn returns 2, it will return y when getting the char from "yn".

\$\endgroup\$
1
  • \$\begingroup\$ Identical to what I had. \$\endgroup\$
    – Shaggy
    Commented May 22, 2018 at 17:34
5
\$\begingroup\$

Perl 6, 21 bytes

{<y n>[.comb('n')%2]}

Try it

Expanded:

{  # bare block lambda with implicit parameter $_

  # index into the list ('y', 'n')
  <y n>[

    .comb('n') # get every instance of 'n' (implicit method call on $_)
    % 2        # turn it into a count and get the modulus

  ]
}
\$\endgroup\$
5
\$\begingroup\$

Java 8, 35 bytes

A decider for a regular language! I can do that.

s->s.matches("y*(ny*ny*)*")?'y':'n'

Try It Online

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

J, 10 9 bytes

{&'ny'@=/

Try it online!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Very clever usage of reduction! \$\endgroup\$
    – Adám
    Commented May 23, 2018 at 5:22
  • \$\begingroup\$ Really nice solution(s)! \$\endgroup\$ Commented May 23, 2018 at 6:18
  • \$\begingroup\$ Would you please provide a decomposition of the/both solutions (as I did with my APL solution)? (Incidentally, you should really post the APL solution as a separate solution from the J solution, even if the algorithm is the same.) \$\endgroup\$ Commented May 24, 2018 at 11:29
  • \$\begingroup\$ {&'ny'@=/ saves a byte. \$\endgroup\$ Commented Jul 7, 2018 at 19:07
  • \$\begingroup\$ @algorithmshark ohhh thanks! \$\endgroup\$
    – FrownyFrog
    Commented Jul 8, 2018 at 3:00
3
\$\begingroup\$

Jelly,  8  7 bytes

O‘Sị⁾ny

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ can be used instead of L€. \$\endgroup\$
    – user202729
    Commented May 22, 2018 at 15:46
3
\$\begingroup\$

R, 46 44 bytes

"if"(sum(1+utf8ToInt(scan(,"")))%%2,"n","y")

Try it online!

Down 2 bytes thanks to Giuseppe and ngm. Port of the Octave answer by Luis Mendo.

\$\endgroup\$
3
  • \$\begingroup\$ It's easiest to get inspired by the Octave answer; while Octave has the advantage that strings are converted to their code points more easily, I think you can port the approach there for a couple bytes down. \$\endgroup\$
    – Giuseppe
    Commented May 22, 2018 at 17:24
  • \$\begingroup\$ sum(utf8ToInt(scan(,""))%%2)%%2 saves one byte. \$\endgroup\$
    – ngm
    Commented May 22, 2018 at 17:54
  • \$\begingroup\$ @ngm @Giuseppe sadly n is even so have to add+1 first.. \$\endgroup\$
    – JayCe
    Commented May 22, 2018 at 18:21
3
\$\begingroup\$

Japt, 9 bytes

Oliver beat me to the shortest solution so here are a couple that are just a byte longer.

B*aUèÍu¹d

Try it

#ndB*UèÍv

Try it


Explanations

              :Implicit input of string U
B             :11
 *            :Mutiplied by
  a           :  The absolute difference of 11 and
   UèÍ        :    The count of "n" in U
      u       :    Mod 2
       ¹d     :Get the character at that codepoint
              :Implicit input of string U
#n            :110
   B*         :Add 11 multiplied by
        v     :  The parity of
     UèÍ      :    The count of "n" in U
  d           :Get the character at that codepoint
\$\endgroup\$
3
\$\begingroup\$

Perl 5, 19 18 bytes

s/y|ny*n//g;s;^$;y

Try it online!

Similar to the Retina solution.

\$\endgroup\$
3
\$\begingroup\$

///, 24 bytes

/ny/n//nn/y//yy/y//yn/n/<input>

Try it online!

I believe this is the shortest possible /// program, as making a one character substitution either is useless (if you insert something in its place) or prevents it from being an output (if you insert nothing). However, since the program must deal with the two character cases, this should be minimal.

First removes all ys right of an n. Then replaces double ns with ys, taking advantage of LTR substitution. At this stage there are many ys followed by at most one n; we deduplicate the ys and if there is an n use it to mop the last y up.

\$\endgroup\$
3
\$\begingroup\$

MATL, 8 bytes

Qs'ny'w)

Try it online!

Saved 2 bytes thanks to Luis Mendo! I previously used the explicit modulus command to get the index into the range 1,2.

Explanation

This uses the fact that MATL have modular indexing, which means that the 1st, 3rd, 5th ... element of the string ny are the same (n). So are the 2nd, 4th, 6th ... element of the string (y).

Q          % Grab input implicitly, and increment each ASCII-value by 1
           % This makes 'n' odd, and 'y' even
 s         % Take the sum of all elements
  'ny'     % Push the string `ny`
      w    % Swap the stack to facilitate the indexing
       )   % Take the n'th element of 'yn' and output it.
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 'yn'3) gives y...? Now that's clever design Luis =) Thanks for the tips! :) \$\endgroup\$ Commented May 24, 2018 at 11:58
  • \$\begingroup\$ It was Dennis' suggestion :-) \$\endgroup\$
    – Luis Mendo
    Commented May 24, 2018 at 12:06
3
\$\begingroup\$

Python 2, 26 bytes

lambda s:'yn'[int(s,35)%2]

Try it online!

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

Retina, 11 bytes

y

nn

^$
y

Try it online!

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

05AB1E, 8 bytes

'n¢„ynsè

Try it online!

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

Java (OpenJDK 8), 143 bytes

a->{char[] u=a.toCharArray();if(u.length==1)return u[0];else{char b=(u[0]==u[1])?'y':'n',i=2;for(;i<u.length;b=(b==u[i++])?'y':'n');return b;}}

Try it online!

And if we take the input as a list:

Java (OpenJDK 8), 118 bytes

u->{if(u.length==1)return u[0];else{char b=(u[0]==u[1])?'y':'n',i=2;for(;i<u.length;b=(b==u[i++])?'y':'n');return b;}}

Try it online!

Explanation:

(input as string)

char[] u=a.toCharArray();  //turn string into char array
if(u.length==1){    
    return u[0];      //if single letter, return it
}else{
    char b=(u[0]==u[1])?'y':'n';     //first two XNOR
    for(char i=2;i<u.length;b=(b==u[i++])?'y':'n');   //XNOR each remaining character
return b;    //return final result
}
\$\endgroup\$
1
  • \$\begingroup\$ You don't need the parenthesis at your ternary-ifs (-4 bytes), you can remove the space at char[]u (-1 byte); and if(u.length==1) can be if(u.length<2) (-1 byte). There is probably more to golf, but I don't really have the time right now. :) \$\endgroup\$ Commented May 23, 2018 at 8:25
2
\$\begingroup\$

Ruby, 24 bytes

->s{"yn"[s.count(?n)%2]}

Try it online!

A lambda taking a string and returning a string.

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

Chip -z, 24 bytes

B}Zvv~vedSD~t
 `'bc af*g

Try it online!

Explanation

This prints 'h', which is 'n' & 'y':

        d    
       f*g

This converts the 'h' to either an 'n' or a 'y', according to whether the top-left wire is powered:

   vv~ve     
   bc a   

This is the xor counter, it powers the part described above as necessary:

B}Z          
 `'       

Finally, this causes the program to only print the last output and terminate when the input is exhausted (the -z flag adds a null terminator for this purpose):

         SD~t
          

Try replacing the S with a space to see the running result (the first 'y' is extraneous, the second char matches the first input, and the third is the result of the first nontrivial calculation).

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

Cubix, 24 20 bytes

Been a while since I played with Cubix, so ...

i;iwW-?;o@..!'yv.n'|

Try it online!

Fairly naive implementation that steps through the string and compares the character against current result.

Interactive Demo

This unwraps onto the cube as follows

    i ;
    i w
W - ? ; o @ . .
! ' y v . n ' |
    . .
    . .
  • W shift ip left
  • i get the initial character
  • i? get character and test for EOI (-1), also start of the loop
    • if EOI ;o@ remove TOS, output TOS as character and exit.
  • else -W! subtract, shift ip left, test for truthy
    • if truthy 'n push character n to TOS
    • if falsey |!'y reflect, test and push character y to TOS
  • v'.;w redirect around the cube pushing and removing a . character and shifting right back into the loop
\$\endgroup\$
2
\$\begingroup\$

Scala, 50 Bytes

def?(b:String)=b.reduce((r,l)=>if(r==l)'y'else'n')
\$\endgroup\$
2
\$\begingroup\$

CJam, 20 11 bytes

q~'ne="yn"=

Try it online!

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

Befunge-98, 13 bytes

~k!aj@,+n'*b!

Try it online!

Basically inverts a 0 for every n in the input, and once more for good measure, then outputs y for 1 and n for 0

~     Get inputted character
 k!   Invert the current value 110 (n) or 121 (y) + 1 times
   aj Jump past the rest of the code
~     Get input again. If no more input, reverse direction
            ! Invert the value once again
       +n'*b  Convert 0/1 to n/y
     @,       Output letter
\$\endgroup\$
2
\$\begingroup\$

Clean, 26 23 bytes

foldr1\a b|a==b='y'='n'

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can save 3 bytes using lambda guards: foldr1\a b|a==b='y'='n'. (By the way, unfortunately usually imports are part of the bytecode.) \$\endgroup\$
    – user42682
    Commented May 23, 2018 at 12:32

Your Answer

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

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