Determining Yes or No?

19

2

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.

Squareoot

Posted 2018-05-22T15:06:16.940

Reputation: 317

3Congratulations on the first challenge with clear specification! (although it's unfortunate that some community members don't like "too trivial" challenges....) – user202729 – 2018-05-22T15:30:25.730

3Very similar. – user202729 – 2018-05-22T15:40:20.053

The boolean relationship defined is XNOR, sometimes called EQV in older BASICs. It is the inverse of XOR, and can be achieved as NOT(XOR...) if using boolean values directly; the additional complexity in this challenge is strictly from the requirement to deal with strings of 'yn' in both input and output. – Jeff Zeitlin – 2018-05-22T17:06:37.997

7Can we output an alternative pair? Say 1 for y, and 0 for n. – Oliver – 2018-05-22T17:27:36.487

5Can we take input as a list of characters ie ["y", "n", "n"] – Okx – 2018-05-22T17:34:24.390

1Can we take input as an alternative pair? Say 1 for y, and 0 for n. – Adám – 2018-05-23T05:19:55.247

2@Adám Then this would be an exact duplicate of the linked challenge, I guess not :P – Mr. Xcoder – 2018-05-23T06:56:16.827

1@Mr.Xcoder No, XNOR vs XOR. But why the unnecessary complication of I/O formats here, when the much related challenge doesn't need it? – Adám – 2018-05-23T08:12:19.563

2XNOR is just NOT XOR so this would be too closely related to be kept open. – Mr. Xcoder – 2018-05-23T08:23:01.177

@Mr.Xcoder It's only a duplicate if it's "1 for n, 0 for y"... – user202729 – 2018-05-23T12:03:17.923

1@Mr.Xcoder If the only thing keeping this question from being a duplicate is different characters for input format, how is it not already a dupe? – jpmc26 – 2018-05-23T18:56:21.987

Why not close the old one as a dupe of the new one? The old one was poorly received, and I think this one is more interesting – Conor O'Brien – 2018-05-23T22:43:34.823

@ConorO'Brien What's wrong with doing this? HNQ questions rarely "have the correct votes". – user202729 – 2018-05-24T02:47:21.840

@ConorO'Brien Some discussion in chat.

– user202729 – 2018-05-24T02:51:09.570

2The output format makes this more interesting than the problem it's marked a duplicate of. That one is just sum of elements, reduce modulo 2. Many of those answers would not be optimal here. – Angs – 2018-05-24T06:36:09.683

1Your first example doesn't make sense to me. How does the no merge with the first yes? Isn't evaluation left-to-right? Do you mean the two yeses merge, then the resulting yes merges with the no to make no? – David Conrad – 2018-05-24T12:42:18.787

1Could we have a few more examples? – L3viathan – 2018-05-24T17:23:57.827

2Why close this as a duplicate of a heavily downvoted challenge? I voted to leave this open in the review queue. – Stewie Griffin – 2018-06-01T13:17:09.910

@davidconrad you're right, but as I said, the order doesn't matter – Squareoot – 2018-06-01T18:31:34.033

3

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

– James – 2018-06-01T19:51:25.100

Can we output truthy or falsy values? – MilkyWay90 – 2018-11-09T16:38:37.880

Answers

9

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

Neil

Posted 2018-05-22T15:06:16.940

Reputation: 95 035

1Explain how it works, please? – Malady – 2018-05-23T04:42:29.060

@Malandy Link is to verbose version of code. – Adám – 2018-05-23T05:24:10.413

1@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 – 2018-05-23T07:51:54.687

14

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

Luis Mendo

Posted 2018-05-22T15:06:16.940

Reputation: 87 464

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!

– Dom Hastings – 2018-05-23T12:17:52.397

2@DomHastings Fails for yynynynny given in the OP, should return y, but returns n – Skidsdev – 2018-05-23T13:14:37.187

9

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!

Arnauld

Posted 2018-05-22T15:06:16.940

Reputation: 111 334

31: s=>'yn'[s.match(/n/g).length&1] :P – ASCII-only – 2018-05-23T22:07:14.197

@ASCII-only This would fail on strings that do not contain at least one n. – Arnauld – 2018-05-23T22:47:22.197

Oh, it would. Oops >_> – ASCII-only – 2018-05-23T22:54:00.767

8

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!

Angs

Posted 2018-05-22T15:06:16.940

Reputation: 4 825

1

Your previous approach can be done in 30 bytes. Try it online!

– Post Rock Garf Hunter – 2018-05-23T15:14:40.000

7

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.

user202729

Posted 2018-05-22T15:06:16.940

Reputation: 14 620

It was my bogus solution confusing me! – Jonathan Allan – 2018-05-22T15:51:43.213

7

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'

Jeff Zeitlin

Posted 2018-05-22T15:06:16.940

Reputation: 213

While XOR ignores 0s and flips on 1s, XNOR ignores 1s and flips on 0s, “initially” being at 1 instead of 0 like XOR. – FrownyFrog – 2018-05-23T11:56:30.350

@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. – Jeff Zeitlin – 2018-05-23T11:58:48.043

6

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

user48543

Posted 2018-05-22T15:06:16.940

Reputation:

6

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

Oliver

Posted 2018-05-22T15:06:16.940

Reputation: 7 160

Identical to what I had. – Shaggy – 2018-05-22T17:34:19.317

6

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!

Digital Trauma

Posted 2018-05-22T15:06:16.940

Reputation: 64 644

5

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

  ]
}

Brad Gilbert b2gills

Posted 2018-05-22T15:06:16.940

Reputation: 12 713

5

J, 10 9 bytes

{&'ny'@=/

Try it online!

FrownyFrog

Posted 2018-05-22T15:06:16.940

Reputation: 3 112

1Very clever usage of reduction! – Adám – 2018-05-23T05:22:47.113

Really nice solution(s)! – Galen Ivanov – 2018-05-23T06:18:22.533

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.) – Jeff Zeitlin – 2018-05-24T11:29:55.993

{&'ny'@=/ saves a byte. – algorithmshark – 2018-07-07T19:07:27.543

@algorithmshark ohhh thanks! – FrownyFrog – 2018-07-08T03:00:17.003

5

Python 2, 29 bytes

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

Try it online!

Mr. Xcoder

Posted 2018-05-22T15:06:16.940

Reputation: 39 774

5

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

Jakob

Posted 2018-05-22T15:06:16.940

Reputation: 2 428

3

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.

JayCe

Posted 2018-05-22T15:06:16.940

Reputation: 2 655

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. – Giuseppe – 2018-05-22T17:24:36.517

sum(utf8ToInt(scan(,""))%%2)%%2 saves one byte. – ngm – 2018-05-22T17:54:40.320

@ngm @Giuseppe sadly n is even so have to add+1 first.. – JayCe – 2018-05-22T18:21:44.993

3

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

Shaggy

Posted 2018-05-22T15:06:16.940

Reputation: 24 623

3

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

boboquack

Posted 2018-05-22T15:06:16.940

Reputation: 2 017

3

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.

Stewie Griffin

Posted 2018-05-22T15:06:16.940

Reputation: 43 471

1'yn'3) gives y...? Now that's clever design Luis =) Thanks for the tips! :) – Stewie Griffin – 2018-05-24T11:58:26.617

It was Dennis' suggestion :-)

– Luis Mendo – 2018-05-24T12:06:56.803

3

Python 2, 26 bytes

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

Try it online!

Lynn

Posted 2018-05-22T15:06:16.940

Reputation: 55 648

2

Jelly,  8  7 bytes

O‘Sị⁾ny

Try it online!

Jonathan Allan

Posted 2018-05-22T15:06:16.940

Reputation: 67 804

2 can be used instead of L€. – user202729 – 2018-05-22T15:46:12.187

2

Retina, 11 bytes

y

nn

^$
y

Try it online!

user48543

Posted 2018-05-22T15:06:16.940

Reputation:

2

05AB1E, 8 bytes

'n¢„ynsè

Try it online!

Mr. Xcoder

Posted 2018-05-22T15:06:16.940

Reputation: 39 774

2

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
}

X1M4L

Posted 2018-05-22T15:06:16.940

Reputation: 1 586

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. :) – Kevin Cruijssen – 2018-05-23T08:25:08.013

2

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.

Ciaran_McCarthy

Posted 2018-05-22T15:06:16.940

Reputation: 689

It looks like the space after 'y' can be removed. – Laikoni – 2018-05-23T06:46:48.180

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. – Laikoni – 2018-05-23T06:53:42.293

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. – Ciaran_McCarthy – 2018-05-28T10:07:42.840

1

The meta consensus is here: https://codegolf.meta.stackexchange.com/a/1503/56433

– Laikoni – 2018-05-28T17:24:52.333

2

Ruby, 24 bytes

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

Try it online!

A lambda taking a string and returning a string.

benj2240

Posted 2018-05-22T15:06:16.940

Reputation: 801

2

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

MickyT

Posted 2018-05-22T15:06:16.940

Reputation: 11 735

2

Scala, 50 Bytes

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

Jared K

Posted 2018-05-22T15:06:16.940

Reputation: 893

2

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!

ErikF

Posted 2018-05-22T15:06:16.940

Reputation: 2 149

*a&1?0:23 saves a byte and return i saves another. – Neil – 2018-05-23T08:10:25.593

Suggest i;f(char*a){for(i=*a;*++a;i^=*a&1?:23);a=i;} – ceilingcat – 2018-06-26T20:23:15.097

2

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

Jo King

Posted 2018-05-22T15:06:16.940

Reputation: 38 234

2

JavaScript, 3937 Bytes

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

Simple reduce function after splitting the input string.

user3335941

Posted 2018-05-22T15:06:16.940

Reputation: 41

1

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.

– dzaima – 2018-05-23T11:16:19.663

golfing suggestion: replace s.split('') with [...s] for 37 bytes: s=>[...s].reduce((x,y)=>x==y?'y':'n') – dzaima – 2018-05-23T11:18:40.900

2

Clean, 26 23 bytes

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

Try it online!

user80597

Posted 2018-05-22T15:06:16.940

Reputation:

2

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

– None – 2018-05-23T12:32:57.973

2

Kotlin, 32 Bytes

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

Try it online!

Kromzem

Posted 2018-05-22T15:06:16.940

Reputation: 31

1

CJam, 20 11 bytes

q~'ne="yn"=

Try it online!

maxb

Posted 2018-05-22T15:06:16.940

Reputation: 5 754

1

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.

Francisco Hahn

Posted 2018-05-22T15:06:16.940

Reputation: 591

1

JavaScript, 44 bytes

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

Try it out on developer console.

Algirdas Butkus

Posted 2018-05-22T15:06:16.940

Reputation: 31

1

Husk, 8 bytes

!"ny"#'n

Try it online!

user48543

Posted 2018-05-22T15:06:16.940

Reputation:

1

Stax, 9 7 bytes

Å8Dq↨W<

Run and debug it

wastl

Posted 2018-05-22T15:06:16.940

Reputation: 3 089

1

If I'm understanding right, you can use modular indexing to get to 7.

– recursive – 2018-05-22T20:56:21.080

@recursive what is modular indexing? – wastl – 2018-05-22T21:02:25.563

2When 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. – recursive – 2018-05-22T21:03:53.850

1

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.

MannyC

Posted 2018-05-22T15:06:16.940

Reputation: 131

1

Perl 5, 19 18 bytes

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

Try it online!

Similar to the Retina solution.

wastl

Posted 2018-05-22T15:06:16.940

Reputation: 3 089

1

GolfScript, 14 bytes

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

Try it online!

wastl

Posted 2018-05-22T15:06:16.940

Reputation: 3 089

1

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

Phlarx

Posted 2018-05-22T15:06:16.940

Reputation: 1 366

1

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.

briantist

Posted 2018-05-22T15:06:16.940

Reputation: 3 110

Save 5 bytes by using "yn" instead of ('y','n'). – Neil – 2018-05-23T08:01:08.970

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 – 2018-05-23T08:02:48.703

@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! – briantist – 2018-05-23T20:26:32.950

1

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.

lukass

Posted 2018-05-22T15:06:16.940

Reputation: 71

1

Clojure, 40 bytes

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

Try it online!

Gabe Laughlin

Posted 2018-05-22T15:06:16.940

Reputation: 21

1

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.

Jasmes

Posted 2018-05-22T15:06:16.940

Reputation: 131

1

JavaScript (ES6), 71 bytes

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

Logern

Posted 2018-05-22T15:06:16.940

Reputation: 845

1

Prolog (SWI), 56 bytes

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

Try it online!

ASCII-only

Posted 2018-05-22T15:06:16.940

Reputation: 4 687

1

Common Lisp, 52 bytes

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

Try it online!

ASCII-only

Posted 2018-05-22T15:06:16.940

Reputation: 4 687