User:DanBron/Game of Nim: Difference between revisions

m
nowiki to make the page disappear from task space without changing the contents too much
(Added MATLAB example)
m (nowiki to make the page disappear from task space without changing the contents too much)
 
(5 intermediate revisions by 3 users not shown)
Line 1:
<nowiki>
{{draft task}}
'''The suitability of this task for inclusion on Rosetta Code is being discussed on the talk page. This task may disappear in future.'''<br>
'''You are welcome to join in the discussion on the [[Talk:Game of Nim|talk page]].'''<br>
Line 41:
'''Notes on execution''': Just define <tt>nim</tt> in the REPL and call it as per the example.<br/>
'''Golf score''': 23 bytes (including the assigment, i.e. naming the function; 18 bytes for the function definition).
 
=={{header|Javascript}}==
 
Translation of J:
 
<lang Javascript>function nim() {
var x=0, a=arguments, l=a.length, r=[];
for (var j=0; j<l; j++) x^=a[j];
for (var j=0; j<l; j++) r[j]= Math.max(0,a[j]-(x^a[j]));
return r;
}</lang>
 
Task example:
 
<lang Javascipt>nim(4,7,2,2,1)
[0, 2, 2, 2, 0]</lang>
 
Ugly golf variant:
 
<lang Javascript>function m(a){x=0
l=a.length
r=[]
for(j=0;j<l;j++)x^=a[j]
for(j=0;j<l;j++)r[j]=Math.max(0,a[j]-(x^a[j]))
return r}</lang>
 
Golf example:
 
<lang Javascript>m([4,7,2,2,1])
[0, 2, 2, 2, 0]</lang>
 
=={{header|MATLAB}}==
Line 219 ⟶ 249:
A wins!
</pre>
 
[[Category:Handicap]]
</nowiki>
1,336

edits