# 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 '18 at 15:30
• May 22 '18 at 15:40
• Can we output an alternative pair? Say 1 for y, and 0 for n. May 22 '18 at 17:27
• Can we take input as a list of characters ie ["y", "n", "n"]
– Okx
May 22 '18 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 '18 at 19:51

# JavaScript, 3937 Bytes

s=>[...s].reduce((x,y)=>x==y?'y':'n')


Simple reduce function after splitting the input string.

• Welcome to PPCG! Your code assumes the input is in the variable s, which is not a valid input method here. You may instead make your answer a lambda function taking the input as an argument by prepending s=> to your answer for 42 bytes. May 23 '18 at 11:16
• golfing suggestion: replace s.split('') with [...s] for 37 bytes: s=>[...s].reduce((x,y)=>x==y?'y':'n') May 23 '18 at 11:18

# C (gcc), 52 50 bytes

Thanks to @Neil for the suggestions.

I borrowed the solution of counting ns, but instead of keeping a count, I just flip between the initial state and its inverse on an n.

i;f(char*a){for(i=*a;*++a;i^=*a&1?0:23);return i;}


Try it online!

• *a&1?0:23 saves a byte and return i saves another.
– Neil
May 23 '18 at 8:10
• Suggest i;f(char*a){for(i=*a;*++a;i^=*a&1?:23);a=i;} Jun 26 '18 at 20:23

# Kotlin, 32 Bytes

"yn"[s.filter{it=='n'}.length%2]


Try it online!

## F#, 54 50 bytes

-4 bytes thanks to Laikoni.

let c=Seq.reduce(fun a x->if a=x then 'y'else 'n')


Try it online!

Seq.reduce applies a function with an accumulator (a) to each element (x) in the string. For the first call, a is the first element in the string and x is the second element.

• It looks like the space after 'y' can be removed. May 23 '18 at 6:46
• i can also be removed by Eta-conversion: let c=Seq.reduce(fun a x->if a=x then 'y'else 'n'). And as our general rules allow anonymous functions, you can drop the let c= part as well. May 23 '18 at 6:53
• I like the Eta-conversion! But can you link to that rule about anonymous functions please? It seems a little strange not to have the function definition in the submitted code, even for anonymous functions. May 28 '18 at 10:07
• The meta consensus is here: codegolf.meta.stackexchange.com/a/1503/56433 May 28 '18 at 17:24

# JavaScript, 44 bytes

(prompt()+"n").match(/n/gi).length%2?"y":"n"


Try it out on developer console.

# Husk, 8 bytes

!"ny"#'n


Try it online!

# Mathematica, 59 bytes

Not the shortest but anyway

f[s_]:=Times@@StringCases[s,a={"y"->1,"n"->-1}]/.Reverse/@a


Call if s is your string of y and n (included between ".."), call f[s].

All it does is to replace y by 1 and n by -1, then multiply them and replace the numbers back to strings.

# Stax, 9 7 bytes

Å8Dq↨W<


Run and debug it

• If I'm understanding right, you can use modular indexing to get to 7. May 22 '18 at 20:56
• @recursive what is modular indexing? May 22 '18 at 21:02
• When you use @ to get an element from an array, it "wraps around". So you can use @ to get an element from a 2-length array, and even indices return the first element, and odd ones return the last. Basically |e (which determines parity of a number) is unnecessary. May 22 '18 at 21:03

# GolfScript, 14 bytes

'n'/,2%'ny'1/=


Try it online!

## PHP, 77 70 Bytes

Try it online

Code, recursive function

function f($p){echo($p[1])?f(strtr($p,[yy=>y,yn=>n,ny=>n,nn=>y])):$p;}


Explanation

Why use strtr with a replace array like [yy=>y,yn=>n,ny=>n,nn=>y], as the string the function takes can be shown like (example with test case "yynynynynyyn")

  (y and y) and (n and y) and (n and y) and (n and y) and (n and y) and (y and n)


What does the function do?

  function f($p){ echo($p[1])
//The function will replace yy yn ny nn while strlen>1
?f(strtr($p,[yy=>y,yn=>n,ny=>n,nn=>y])) //Length greater than one, apply strtr :$p;
//ok, just one letter left, "echo" it
}


With a loop, 83 Bytes

Try it online

Simulates "run as pipe"

Code

<?php $p=$argv; while(strlen($p)>1){$p=strtr($p,[yy=>y,yn=>n,ny=>n,nn=>y]);}echo$p;


Explanation

It does the exact same thing as the recursive function

$p=$argv; //Accepting value from the script variable
while(strlen($p)>1){ //the loop ends when string length is one$p=strtr($p,[yy=>y,yn=>n,ny=>n,nn=>y]); //replace } echo$p; //echo the result.


# PowerShell Core, 48 43 bytes

"$args"|% t*y|%{$z=$z-xor$_-eq'n'};'yn'[\$z]


Try it online!

Not the best method. Split into char array, use -xor, invert it implicitly at the end with the array lookup to get back a char.

• Save 5 bytes by using "yn" instead of ('y','n').
– Neil
May 23 '18 at 8:01
• Also, you need to compare against 'n' and not 'y' - you don't see this in your demo because there are equal numbers of each letter.
– Neil
May 23 '18 at 8:02
• @Neil wow I can't believe I didn't use 'yn', I'm crazy rusty. I originally was comparing with 'n' and now I can't remember why I changed it. Thanks! May 23 '18 at 20:26

# Go, 91 bytes

package main;import ."strings";type s=string;func f(S s)(s){return s("yn"[Count(S,"n")%2])}


105 bytes if ;func main(){} is added; unsure of Gode Golf golang norms.

# Clojure, 40 bytes

(if(even?(count(filter #(= %\n)s)))\y\n)


Try it online!

C (GCC) 42 bytes

g(char*a){return*a?"yn"[(g(a+1)^*a)&1]:1;}


Try in online!

Solution is simply based on XNOR operation.

# JavaScript (ES6), 71 bytes

f=a=>(a.split("").map(b=>(b=="y")?1:-1).reduce((b,c)=>b*c)==1)?"y":"n";


# Prolog (SWI), 56 bytes

[H,H]-121.
[_,_]-110.
[A]-A.
[A,B|T]-S:-[A,B]-C,[C|T]-S.


Try it online!

# Common Lisp, 52 bytes

(lambda(s)(reduce(lambda(a b)(if(eq a b)#\y #\n))s))


Try it online!

# 05AB1E, 7 bytes

'n¢„ynè


Try it online!

'n¢„ynè  # full program
è  # character in...
# (implicit) commutative indèx...
„yn   # literal...
è  # at 0-based index...
# (implicit) commutative, modular indèx...
¢      # number of...
'n       # literal...
¢      # s in...
# implicit input
# implicit output