Jump to content

Bulls and cows: Difference between revisions

no edit summary
(Updated D entry)
No edit summary
Line 2,101:
end
until quit</lang>
 
=={{header|Mathematica}}==
<lang Mathematica>digits=Last@FixedPointList[If[Length@Union@#==4,#,Table[Random[Integer,{1,9}],{4}]]&,{}]
codes=ToCharacterCode[StringJoin[ToString/@digits]];
Module[{r},
While[True,
r=InputString[];
If[r===$Canceled,Break[],
With[{userCodes=ToCharacterCode@r},
If[userCodes===codes,Print["You got it!"];Break[],
If[Length@userCodes==Length@codes,Print[r<>": "<>ToString[Count[userCodes-codes,0]]<>" bull(s), "<>ToString@Length@Intersection[codes,userCodes]<>" cow(s)."],
Print["Guess four digits."]]]]]]]
</lang>
Output:
<pre>
{8, 2, 6, 1}
3432: 0 bull(s), 1 cow(s).
Illegal input.
8261: You got it!
</pre>
 
=={{header|MUMPS}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.