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

48 Answers 48

1
2
2
\$\begingroup\$

JavaScript, 3937 Bytes

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

Simple reduce function after splitting the input string.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – dzaima
    May 23 '18 at 11:16
  • \$\begingroup\$ golfing suggestion: replace s.split('') with [...s] for 37 bytes: s=>[...s].reduce((x,y)=>x==y?'y':'n') \$\endgroup\$
    – dzaima
    May 23 '18 at 11:18
2
\$\begingroup\$

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!

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

Kotlin, 32 Bytes

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

Try it online!

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

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.

\$\endgroup\$
4
  • \$\begingroup\$ It looks like the space after 'y' can be removed. \$\endgroup\$
    – Laikoni
    May 23 '18 at 6:46
  • \$\begingroup\$ 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. \$\endgroup\$
    – Laikoni
    May 23 '18 at 6:53
  • \$\begingroup\$ 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. \$\endgroup\$ May 28 '18 at 10:07
  • 1
    \$\begingroup\$ The meta consensus is here: codegolf.meta.stackexchange.com/a/1503/56433 \$\endgroup\$
    – Laikoni
    May 28 '18 at 17:24
1
\$\begingroup\$

JavaScript, 44 bytes

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

Try it out on developer console.

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

Husk, 8 bytes

!"ny"#'n

Try it online!

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

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.

\$\endgroup\$
1
\$\begingroup\$

Stax, 9 7 bytes

Å8Dq↨W<

Run and debug it

\$\endgroup\$
3
  • 1
    \$\begingroup\$ If I'm understanding right, you can use modular indexing to get to 7. \$\endgroup\$
    – recursive
    May 22 '18 at 20:56
  • \$\begingroup\$ @recursive what is modular indexing? \$\endgroup\$
    – wastl
    May 22 '18 at 21:02
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – recursive
    May 22 '18 at 21:03
1
\$\begingroup\$

GolfScript, 14 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Save 5 bytes by using "yn" instead of ('y','n'). \$\endgroup\$
    – Neil
    May 23 '18 at 8:01
  • \$\begingroup\$ 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. \$\endgroup\$
    – Neil
    May 23 '18 at 8:02
  • \$\begingroup\$ @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! \$\endgroup\$
    – briantist
    May 23 '18 at 20:26
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Clojure, 40 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 71 bytes

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

Prolog (SWI), 56 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Common Lisp, 52 bytes

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

Try it online!

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

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
\$\endgroup\$
1
2

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.