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\$
  • 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\$ – user202729 May 22 '18 at 15:30
  • 3
    \$\begingroup\$ Very similar. \$\endgroup\$ – user202729 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

47 Answers 47

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\$
  • \$\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\$ – Ciaran_McCarthy 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\$
1
\$\begingroup\$

Husk, 8 bytes

!"ny"#'n

Try it online!

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

Perl 5, 19 18 bytes

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

Try it online!

Similar to the Retina solution.

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

GolfScript, 14 bytes

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

Try it online!

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

CJam, 20 11 bytes

q~'ne="yn"=

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\$
  • \$\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\$

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.