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

9
\$\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\$
  • 1
    \$\begingroup\$ Explain how it works, please? \$\endgroup\$ – Malady May 23 '18 at 4:42
  • \$\begingroup\$ @Malandy Link is to verbose version of code. \$\endgroup\$ – Adám May 23 '18 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 May 23 '18 at 7:51
14
\$\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\$
9
\$\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\$
  • \$\begingroup\$ 31: s=>'yn'[s.match(/n/g).length&1] :P \$\endgroup\$ – ASCII-only May 23 '18 at 22:07
  • \$\begingroup\$ @ASCII-only This would fail on strings that do not contain at least one n. \$\endgroup\$ – Arnauld May 23 '18 at 22:47
  • \$\begingroup\$ Oh, it would. Oops >_> \$\endgroup\$ – ASCII-only May 23 '18 at 22:54
8
\$\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\$
7
\$\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\$
  • \$\begingroup\$ It was my bogus solution confusing me! \$\endgroup\$ – Jonathan Allan May 22 '18 at 15:51
7
\$\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\$
  • \$\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 May 23 '18 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\$ – Jeff Zeitlin May 23 '18 at 11:58
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\$
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\$
  • \$\begingroup\$ Identical to what I had. \$\endgroup\$ – Shaggy May 22 '18 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\$

Python 2, 29 bytes

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

Try it online!

\$\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\$
5
\$\begingroup\$

J, 10 9 bytes

{&'ny'@=/

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Very clever usage of reduction! \$\endgroup\$ – Adám May 23 '18 at 5:22
  • \$\begingroup\$ Really nice solution(s)! \$\endgroup\$ – Galen Ivanov May 23 '18 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\$ – Jeff Zeitlin May 24 '18 at 11:29
  • \$\begingroup\$ {&'ny'@=/ saves a byte. \$\endgroup\$ – algorithmshark Jul 7 '18 at 19:07
  • \$\begingroup\$ @algorithmshark ohhh thanks! \$\endgroup\$ – FrownyFrog Jul 8 '18 at 3:00
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\$
  • \$\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 May 22 '18 at 17:24
  • \$\begingroup\$ sum(utf8ToInt(scan(,""))%%2)%%2 saves one byte. \$\endgroup\$ – ngm May 22 '18 at 17:54
  • \$\begingroup\$ @ngm @Giuseppe sadly n is even so have to add+1 first.. \$\endgroup\$ – JayCe May 22 '18 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\$

///, 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\$

Clean, 26 23 bytes

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

Try it online!

\$\endgroup\$
  • 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\$ – Keelan May 23 '18 at 12:32
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\$
  • 1
    \$\begingroup\$ 'yn'3) gives y...? Now that's clever design Luis =) Thanks for the tips! :) \$\endgroup\$ – Stewie Griffin May 24 '18 at 11:58
  • \$\begingroup\$ It was Dennis' suggestion :-) \$\endgroup\$ – Luis Mendo May 24 '18 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\$

Jelly,  8  7 bytes

O‘Sị⁾ny

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ can be used instead of L€. \$\endgroup\$ – user202729 May 22 '18 at 15:46
2
\$\begingroup\$

05AB1E, 8 bytes

'n¢„ynsè

Try it online!

\$\endgroup\$
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\$
  • \$\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\$ – Kevin Cruijssen May 23 '18 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\$

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

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

JavaScript, 3937 Bytes

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

Simple reduce function after splitting the input string.

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

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.