Created
September 29, 2015 17:47
-
-
Save mcalavera81/688d40d66cbabe78367b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Implement a function, routine or method (and the support code you might need) that simulates 6 tosses of 3 coins | |
each and outputs a string according to the Value Rules below. | |
Value Rules | |
a) Tails has a value of 2, and heads a value of 3. | |
b) Getthetotalvalueofthethreecoinspertoss. | |
c) Compose a string with these values in reverse order, from toss 6 to 1. Example output: “767777” | |
val r = scala.util.Random | |
val tosses = (1 to 3).par.map(i=> (1 to 6).map(j=> if(r.nextInt(2)==0) (s"Coin $i","Heads") else (s"Coin $i","Tails"))) | |
val zipped =tosses(0) zip tosses(1) zip tosses(2) | |
val getValue =(str:String)=> {if (str=="Heads") 3 else 2} | |
val values= zipped.map(toss => getValue(toss._1._1._2)+getValue(toss._1._2._2)+getValue(toss._2._2)) | |
println (values.reverse.mkString("")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment