# Determining Yes or No?

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.

• Congratulations on the first challenge with clear specification! (although it's unfortunate that some community members don't like "too trivial" challenges....) May 22, 2018 at 15:30
• May 22, 2018 at 15:40
• Can we output an alternative pair? Say 1 for y, and 0 for n. May 22, 2018 at 17:27
• Can we take input as a list of characters ie ["y", "n", "n"]
– Okx
May 22, 2018 at 17:34
• 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 Jun 1, 2018 at 19:51

# 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'.
• 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! May 23, 2018 at 12:17
• @DomHastings Fails for yynynynny given in the OP, should return y, but returns n May 23, 2018 at 13:14

# Charcoal, 6 bytes

§yn№Ｓn


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

    Ｓ   Input string
№ n  Count number of ns
§yn     Circularly index into string yn
Implicitly print appropriate character

• Explain how it works, please? May 23, 2018 at 4:42
• @Malandy Link is to verbose version of code.
May 23, 2018 at 5:24
• @Adám Actually I usually add one, however I had just dashed this off in a work break and forgotten to edit one in.
– Neil
May 23, 2018 at 7:51

# JavaScript (ES6), 28 bytes

Takes input as a string.

s=>'ny'[s.splitn.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!

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

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! • Your previous approach can be done in 30 bytes. Try it online! May 23, 2018 at 15:14 # 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. • It was my bogus solution confusing me! May 22, 2018 at 15:51 # 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'  • While XOR ignores 0s and flips on 1s, XNOR ignores 1s and flips on 0s, “initially” being at 1 instead of 0 like XOR. May 23, 2018 at 11:56 • @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. May 23, 2018 at 11:58 # 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".  # Python 2, 29 bytes lambda s:'yn'[s.count('n')%2]  Try it online! # 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! # 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". • Identical to what I had. May 22, 2018 at 17:34 # 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 ] }  # 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 # J, 10 9 bytes {&'ny'@=/  Try it online! • Very clever usage of reduction! – Adám May 23, 2018 at 5:22 • Really nice solution(s)! May 23, 2018 at 6:18 • 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.) May 24, 2018 at 11:29 • {&'ny'@=/ saves a byte. Jul 7, 2018 at 19:07 • @algorithmshark ohhh thanks! Jul 8, 2018 at 3:00 # Jelly, 8 7 bytes O‘Sị⁾ny  Try it online! • Ẉ can be used instead of L€. May 22, 2018 at 15:46 # 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. • 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. May 22, 2018 at 17:24 • sum(utf8ToInt(scan(,""))%%2)%%2 saves one byte. – ngm May 22, 2018 at 17:54 • @ngm @Giuseppe sadly n is even so have to add+1 first.. May 22, 2018 at 18:21 # 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  # Perl 5, 19 18 bytes s/y|ny*n//g;s;^$;y


Try it online!

Similar to the Retina solution.

# ///, 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.

# 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.

• 'yn'3) gives y...? Now that's clever design Luis =) Thanks for the tips! :) May 24, 2018 at 11:58
• It was Dennis' suggestion :-) May 24, 2018 at 12:06

# Python 2, 26 bytes

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


Try it online!

# Retina, 11 bytes

y

nn

^\$
y


Try it online!

# 05AB1E, 8 bytes

'n¢„ynsè


Try it online!

# 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
}

• 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. :) May 23, 2018 at 8:25

# Ruby, 24 bytes

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


Try it online!

A lambda taking a string and returning a string.

# 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).

# 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

# Scala, 50 Bytes

def?(b:String)=b.reduce((r,l)=>if(r==l)'y'else'n')


# CJam, 20 11 bytes

q~'ne="yn"=


Try it online!

# 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


# Clean, 26 23 bytes

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


Try it online!

• 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.)
– user42682
May 23, 2018 at 12:32