We’re rewarding the question askers & reputations are being recalculated! Read more.

3 contains => endsWith. It should be more effective

A book on probability says that a sequence of heads and tails is generated by a fair coin asking what is the probability that the pattern T, H, H, H occurs before the pattern H, H, H, H. I have made a generator of such strings. Is it right (I have heard that return statement is not appreciated in Scala)?

def l(s: String): Boolean = {
if (s.containsendsWith("hhhh")) return true;
if (s.containsendsWith("thhh")) return false;
l(s + (if (Math.random > .5) "h" else "t"))
}


A book on probability says that a sequence of heads and tails is generated by a fair coin asking what is the probability that the pattern T, H, H, H occurs before the pattern H, H, H, H. I have made a generator of such strings. Is it right (I have heard that return statement is not appreciated in Scala)?

def l(s: String): Boolean = {
if (s.contains("hhhh")) return true;
if (s.contains("thhh")) return false;
l(s + (if (Math.random > .5) "h" else "t"))
}


A book on probability says that a sequence of heads and tails is generated by a fair coin asking what is the probability that the pattern T, H, H, H occurs before the pattern H, H, H, H. I have made a generator of such strings. Is it right (I have heard that return statement is not appreciated in Scala)?

def l(s: String): Boolean = {
if (s.endsWith("hhhh")) return true;
if (s.endsWith("thhh")) return false;
l(s + (if (Math.random > .5) "h" else "t"))
}

2 edited tags; edited title

1

# Generate a sequence until pattern is hit

A book on probability says that a sequence of heads and tails is generated by a fair coin asking what is the probability that the pattern T, H, H, H occurs before the pattern H, H, H, H. I have made a generator of such strings. Is it right (I have heard that return statement is not appreciated in Scala)?

def l(s: String): Boolean = {
if (s.contains("hhhh")) return true;
if (s.contains("thhh")) return false;
l(s + (if (Math.random > .5) "h" else "t"))
}