| Bytes | Lang | Time | Link |
|---|---|---|---|
| 030 | Janet | 250905T141340Z | Adam |
| 006 | Nibbles | 250905T135527Z | Adam |
| 024 | AWK | 250905T132529Z | xrs |
| 008 | Thunno 2 | 230714T085015Z | The Thon |
| 007 | 05AB1E | 210316T164245Z | Makonede |
| 009 | J | 180522T164312Z | FrownyFr |
| 008 | Japt | 180522T171933Z | Oliver |
| 052 | Common Lisp | 180601T120647Z | ASCII-on |
| 056 | Prolog SWI | 180530T031537Z | ASCII-on |
| 050 | F# | 180522T212241Z | Ciaran_M |
| 026 | Python 2 | 180525T150944Z | lynn |
| 071 | JavaScript ES6 | 180525T145828Z | user3604 |
| 042 | C GCC | 180525T143314Z | Jasmes |
| 032 | Kotlin | 180525T125101Z | Kromzem |
| 040 | Clojure | 180524T225203Z | Gabe Lau |
| 008 | MATL | 180524T082124Z | Stewie G |
| 091 | Go | 180523T205056Z | lukass |
| 043 | PowerShell Core | 180522T224533Z | briantis |
| 050 | C gcc | 180523T040600Z | ErikF |
| 037 | JavaScript | 180523T105846Z | kanine |
| 035 | Java 8 | 180523T032210Z | Jakob |
| 027 | Octave | 180522T161321Z | Luis Men |
| 070 | PHP | 180522T160933Z | Francisc |
| 023 | Clean | 180523T122547Z | user8059 |
| 015 | APL Dyalog Unicode | 180522T165316Z | Jeff Zei |
| 013 | Befunge98 | 180523T062853Z | Jo King |
| 011 | CJam | 180522T155853Z | maxb |
| 006 | Charcoal | 180522T164542Z | Neil |
| 007 | Jelly | 180522T154509Z | user2027 |
| 050 | Scala | 180523T003051Z | Jared K |
| 020 | Cubix | 180522T222419Z | MickyT |
| 024 | Chip z | 180522T221418Z | Phlarx |
| 024 | /// | 180522T214029Z | boboquac |
| 014 | GolfScript | 180522T213530Z | wastl |
| 024 | Ruby | 180522T212957Z | benj2240 |
| 018 | Perl 5 | 180522T212040Z | wastl |
| 007 | Stax | 180522T200319Z | wastl |
| 059 | Mathematica | 180522T210448Z | MannyC |
| 008 | Husk | 180522T195834Z | user4854 |
| 039 | dc | 180522T184043Z | Digital |
| 143 | Java OpenJDK 8 | 180522T183958Z | X1M4L |
| 009 | Japt | 180522T174744Z | Shaggy |
| 044 | R | 180522T162058Z | JayCe |
| 044 | JavaScript | 180522T161108Z | Algirdas |
| 008 | 05AB1E | 180522T171524Z | Mr. Xcod |
| 029 | Python 2 | 180522T165904Z | Mr. Xcod |
| 028 | JavaScript ES6 | 180522T162052Z | Arnauld |
| 009 | Pyth | 180522T162333Z | user4854 |
| 028 | Haskell | 180522T155124Z | Angs |
| nan | 180522T160817Z | Brad Gil | |
| 007 | Jelly | 180522T154437Z | Jonathan |
| 011 | Retina | 180522T155608Z | user4854 |
Janet, 30 bytes
|(["y""n"](%(count even? $)2))
Nibbles, 6.5 6 bytes
=`/+~@+"ny"
=`/+~@+"ny"
+~@ Add 1 to each character,
`/ + sum their codepoints
= "ny" and cyclically index into "ny".
Thunno 2, 8 bytes
'nc`ynsi
Same as all the other answers
'nc`ynsi # Implicit input
'nc # Count of "n"s
si # modularly indexed
`yn # into the string "yn"
# Implicit output
05AB1E, 7 bytes
'n¢„ynè
'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
Japt, 8 bytes
"yn"gUèn
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".
F#, 54 50 bytes
-4 bytes thanks to Laikoni.
let c=Seq.reduce(fun a x->if a=x then 'y'else 'n')
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.
JavaScript (ES6), 71 bytes
f=a=>(a.split("").map(b=>(b=="y")?1:-1).reduce((b,c)=>b*c)==1)?"y":"n";
C (GCC) 42 bytes
g(char*a){return*a?"yn"[(g(a+1)^*a)&1]:1;}
Solution is simply based on XNOR operation.
MATL, 8 bytes
Qs'ny'w)
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.
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.
PowerShell Core, 48 43 bytes
"$args"|% t*y|%{$z=$z-xor$_-eq'n'};'yn'[$z]
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.
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;}
JavaScript, 3937 Bytes
s=>[...s].reduce((x,y)=>x==y?'y':'n')
Simple reduce function after splitting the input string.
Java 8, 35 bytes
A decider for a regular language! I can do that.
s->s.matches("y*(ny*ny*)*")?'y':'n'
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)
Explanation
The ASCII code point of 'y' is odd, and that of 'n' is even. The code
- adds
1to each char in the input string to make'y'even and'n'odd; - computes the sum;
- reduces the result to
1if even,2if odd; - indexes (1-based) into the string
'yn'.
PHP, 77 70 Bytes
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
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.
APL (Dyalog Unicode), 15 bytes
'ny'[1+=/'y'=⍞]
Note: TIO defaults to ⎕IO = 1. If run with ⎕IO←0,
APL (Dyalog Unicode), 13 bytes
'ny'[=/'y'=⍞]
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'
Befunge-98, 13 bytes
~k!aj@,+n'*b!
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
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
Jelly, 7 bytes
ċ”nị⁾ny
ċount number of ”n, ịndex into the string ⁾ny. (with modulo 2)
ċḢịɗ⁾ny
{ċount number of, take the Ḣead, then ịndex into} string ⁾ny.
OCSị⁾ny
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.
Scala, 50 Bytes
def?(b:String)=b.reduce((r,l)=>if(r==l)'y'else'n')
Cubix, 24 20 bytes
Been a while since I played with Cubix, so ...
i;iwW-?;o@..!'yv.n'|
Fairly naive implementation that steps through the string and compares the character against current result.
This unwraps onto the cube as follows
i ;
i w
W - ? ; o @ . .
! ' y v . n ' |
. .
. .
Wshift ip leftiget the initial characteri?get character and test for EOI (-1), also start of the loop- if EOI
;o@remove TOS, output TOS as character and exit.
- if EOI
- else
-W!subtract, shift ip left, test for truthy- if truthy
'npush character n to TOS - if falsey
|!'yreflect, test and push character y to TOS
- if truthy
v'.;wredirect around the cube pushing and removing a . character and shifting right back into the loop
Chip -z, 24 bytes
B}Zvv~vedSD~t
`'bc af*g
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).
///, 24 bytes
/ny/n//nn/y//yy/y//yn/n/<input>
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.
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.
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
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;}}
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;}}
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
}
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
#ndB*UèÍv
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
R, 46 44 bytes
"if"(sum(1+utf8ToInt(scan(,"")))%%2,"n","y")
Down 2 bytes thanks to Giuseppe and ngm. Port of the Octave answer by Luis Mendo.
JavaScript, 44 bytes
(prompt()+"n").match(/n/gi).length%2?"y":"n"
Try it out on developer console.
JavaScript (ES6), 28 bytes
Takes input as a string.
s=>'ny'[s.split`n`.length&1]
JavaScript (ES6), 30 bytes
Takes input as an array of characters.
y=>'yn'[n=1,~~eval(y.join`^`)]
Pyth, 9 bytes
@"yn"l@\n
Explanation
@"yn"l@\n
l@\nQ Get the length of the intersection of the (implicit) input and "n".
@"yn" Modular index into "yn".
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])
Perl 6, 21 bytes
{<y n>[.comb('n')%2]}
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
]
}