Find the N happy number in Scala - scala

Hi everyone I have been working on this problem which is to find the n-th happy number. But I am struggling to make it work here as it is showing
Found: Unit
Required: Int
Maybe you are missing an else part for the conditional?
Here is how it works:
nThHappy(1) returns 1
• nThHappy(3) returns 10
• nThHappy(11) returns 49
• nThHappy(19) returns 97
def nThHappy(k: Int): Int = {
def isHappy(n: Int): Boolean = {
def sumOfDigitsSquared(n: Int): Int = n.toString.foldLeft(0) { (product, num) => product + num.asDigit * num.asDigit }
def loop(seen: Set[Int], x: Int): Boolean = {
x match {
case 1 => true
case _ if seen.contains(x) => false
case _ => loop(seen + x, sumOfDigitsSquared(x))
}
}
loop(Set(),n)
}
def helper(countN: Int,countK: Int, hNum:Int): Int =
countK match {
case hNum => countN
case _ => {
if (isHappy(countN) == false) {
helper(countN+1, countK, hNum)
}
else if(isHappy(countN) == true) { // <-- Here is the problem
helper(countN+1, countK+1, hNum)
}
}
}
helper(0,0,k)
}

You need an else part for that last if, or just remove the test (which is pointless), or just use match:
case _ if isHappy(countN) =>
helper(countN+1, countK+1, hNum)
case _ =>
helper(countN+1, countK, hNum)

Related

Problem in scala for-comprehension de-sugar?

I have the following for-comprehension which I de-sugared using command scala -Xprint:parser ForComprehensionDemo.scala. When I copy the de-sugared method and call it, it produces different result than the for-comprehension. Any idea why?
For comprehension:
object ForComprehensionDemo {
def main(args: Array[String]): Unit = {
forWithIf(true)
}
def forWithIf(condition: Boolean) = {
val x = for {
a <- name(0)
b <- if (condition) {
name(1)
name(2)
} else {
name(100)
}
c <- name(2)
} yield {
a + b + c
}
println(x)
}
def name(x: Int): Option[String] = {
println("called for :" + x)
x match {
case 0 => Some(" aa ")
case 1 => Some(" bb ")
case 2 => Some(" cc ")
case _ => Some(" not identified ")
}
}
}
Produces result:
called for :0
called for :1
called for :2
called for :2
Some( aa cc cc )
De-sugared code
def forWithIf(condition: Boolean) = {
val x = name(0).flatMap(((a) => if (condition)
{
name(1);
name(2)
}
else
name(100).flatMap(((b) => name(2).map(((c) => a.$plus(b).$plus(c)))))));
println(x)
};
Produces result:
called for :0
called for :1
called for :2
Some( cc )
Just a bug in the pretty printer. It's missing parens around the if-else.
In general, scala 2 doesn't represent parens very faithfully, but scala 3 is much better.
package desugar {
object ForComprehensionDemo extends scala.AnyRef {
def main(args: Array[String]): Unit = forWithIf(true);
def forWithIf(condition: Boolean) = {
val x = name(0).flatMap(((a) => (if (condition)
{
name(1);
name(2)
}
else
name(100)).flatMap(((b) => name(2).map(((c) => a.$plus(b).$plus(c)))))));
println(x)
};
def name(x: Int): Option[String] = {
println("called for :".$plus(x));
x match {
case 0 => Some(" aa ")
case 1 => Some(" bb ")
case 2 => Some(" cc ")
case _ => Some(" not identified ")
}
}
}
}
I was curious to see what Scala 3 says. -Xprint:all says after typer:
sugar.ForComprehensionDemo.name(0).flatMap[String](
{
def $anonfun(a: String): Option[String] =
(if condition then
{
sugar.ForComprehensionDemo.name(1)
sugar.ForComprehensionDemo.name(2)
}
else
{
sugar.ForComprehensionDemo.name(100)
}
).flatMap[String](
{
def $anonfun(b: String): Option[String] =
sugar.ForComprehensionDemo.name(2).map[String](
{
def $anonfun(c: String): String =
{
a.+(b).+(c)
}
closure($anonfun)
}
)
closure($anonfun)
}
)
closure($anonfun)
}
)
That's more inscrutible with the closures, but it has parens. Printing after parser isn't useful.
Just for comparison here is IntelliJ Desugar Scala code... output (with FQNs manually abbreviated)
def forWithIf(condition: Boolean) = {
val x = name(0)
.flatMap(((a) =>
(if (condition) {
name(1);
name(2)
} else {
name(100)
})
.flatMap(((b) =>
name(2)
.map(((c) => a.$plus(b).$plus(c)))))
));
println(x)
};
confirming som-snytt's answer.

How to create an Akka Stream Source that generates items recursively

I'm trying to figure out how to create an Akka Streams source that generates many Seq[Int].
Basically, given an int n I want to generate all of the Seq[Int] of 1 to n
Here's some code that does this:
def combinations(n: Int): Seq[Seq[Int]] = {
def loop(acc: (Seq[Int], Seq[Seq[Int]]),
remaining: Seq[Int]): Seq[Seq[Int]] = {
remaining match {
case s if s.size == 1 => {
val total: Seq[Seq[Int]] = acc._2
val current: Seq[Int] = acc._1
total :+ (current :+ s.head)
}
case _ => {
for {
x <- remaining
comb <- loop((acc._1 :+ x, acc._2), remaining.filter(_ != x))
} yield comb
}
}
}
loop((Seq(), Seq()), (1 to n))
}
This works fine up to 10... then it blows up because it runs out of memory. Since I just want to process each of them and don't need to keep them all in memory, I thought... Akka Streams. But I'm at a loss for how to turn this into a Source that produces each combination so I can process them. Basically there where it's appending to total I would produce another item onto the stream.
Here is a solution that uses the Johnson-Trotter algorithm for permutations. tcopermutations creates a LazyList that can be evaluated as needed. For more permutations, just pass a different value to printNIterations.
The reason for using the Johnson-Trotter algorithm is that it breaks the recursive structure of the permutation finding algorithm. That's important for being able to evaluate successive instances of the permutation and storing them in some kind of lazy list or stream.
object PermutationsTest {
def main(args: Array[String]) = {
printNIterations(50, tcopermutations(5).iterator)
}
def printNIterations(n: Int, it: Iterator[Seq[Int]]): Unit = {
if (n<=0) ()
else {
if (it.hasNext) {
println(it.next())
printNIterations(n - 1, it)
} else ()
}
}
def naivepermutations(n: Int): Seq[Seq[Int]] = {
def loop(acc: Seq[Int], remaining: Seq[Int]): Seq[Seq[Int]] = {
remaining match {
case s if s.size == 1 => {
val current: Seq[Int] = acc
Seq((current :+ s.head))
}
case _ => {
for {
x <- remaining
comb <- loop(acc :+ x, remaining.filter(_ != x))
} yield comb
}
}
}
loop(Seq(), (1 to n))
}
def tcopermutations(n: Int): LazyList[Seq[Int]] = {
val start = (1 to n).map(Element(_, Left))
def loop(v: Seq[Element]): LazyList[Seq[Element]] = {
johnsonTrotter(v) match {
case Some(s) => v #:: loop(s)
case None => LazyList(v)
}
}
loop(start).map(_.map(_.i))
}
def checkIfMobile(seq: Seq[Element], i: Int): Boolean = {
val e = seq(i)
def getAdjacent(s: Seq[Element], d: Direction, j: Int): Int = {
val adjacentIndex = d match {
case Left => j - 1
case Right => j + 1
}
s(adjacentIndex).i
}
if (e.direction == Left && i == 0) false
else if (e.direction == Right && i == seq.size - 1) false
else if (getAdjacent(seq, e.direction, i) < e.i) true
else false
}
def findLargestMobile(seq: Seq[Element]): Option[Int] = {
val mobiles = (0 until seq.size).filter{j => checkIfMobile(seq, j)}
if (mobiles.isEmpty) None
else {
val folded = mobiles.map(x=>(x,seq(x).i)).foldLeft(None: Option[(Int, Int)]){ case (acc, elem) =>
acc match {
case None => Some(elem)
case Some((i, value)) => if (value > elem._2) Some((i, value)) else Some(elem)
}
}
folded.map(_._1)
}
}
def swapLargestMobile(seq: Seq[Element], index: Int): (Seq[Element], Int) = {
val dir = seq(index).direction
val value = seq(index).i
dir match {
case Right =>
val folded = seq.foldLeft((None, Seq()): (Option[Element], Seq[Element])){(acc, elem) =>
val matched = elem.i == value
val newAccOpt = if (matched) Some(elem) else None
val newAccSeq = acc._1 match {
case Some(swapMe) => acc._2 :+ elem :+ swapMe
case None => if (matched) acc._2 else acc._2 :+ elem
}
(newAccOpt, newAccSeq)
}
(folded._2, index + 1)
case Left =>
val folded = seq.foldRight((None, Seq()): (Option[Element], Seq[Element])){(elem, acc) =>
val matched = elem.i == value
val newAccOpt = if (matched) Some(elem) else None
val newAccSeq = acc._1 match {
case Some(swapMe) => swapMe +: elem +: acc._2
case None => if (matched) acc._2 else elem +: acc._2
}
(newAccOpt, newAccSeq)
}
(folded._2, index - 1)
}
}
def revDirLargerThanMobile(seq: Seq[Element], mobile: Int) = {
def reverse(e: Element) = {
e.direction match {
case Left => Element(e.i, Right)
case Right => Element(e.i, Left)
}
}
seq.map{ elem =>
if (elem.i > seq(mobile).i) reverse(elem)
else elem
}
}
def johnsonTrotter(curr: Seq[Element]): Option[Seq[Element]] = {
findLargestMobile(curr).map { m =>
val (swapped, newMobile) = swapLargestMobile(curr, m)
revDirLargerThanMobile(swapped, newMobile)
}
}
trait Direction
case object Left extends Direction
case object Right extends Direction
case class Element(i: Int, direction: Direction)
}

Nested function skews return type of its parent

I'm writing a function and having a strange issue. I'm using pattern matching, and then an internal function which uses a slightly changed but almost identical pattern and it isn't compiling:
def isTriangular(n: Int): Boolean = {
n match {
case n if n < 1 => false
case _ => triangularMaths(n, 1)
}
def triangularMaths(j:Int, counter: Int): Boolean = (j, counter) match {
case _ if j-counter == 0 => true
case _ if j-counter < 0 => false
case _ => triangularMaths(j-counter, counter+1)
}
}
The fix for this is I simply make them two seperate methods, and they work as long as triangularMaths isn't nested. However, since triangularMaths is specific to triangular only, I'd like it to be nested. However, when I do this, my compiler complains, telling me I am returning Unit, rather than the expected Boolean . This doesn't quite make sense, as once the original case brackets is resolved, returning true or false, it should go to the end of the method, and complete, correct? What's the fix?
This happens because your method is the last declaration in scope, which makes the compiler emit the Unit value as the return type. Decompiled code looks like this:
def main(args: Array[String]): Unit = {
def isTriangular(n: Int): Boolean = {
n match {
case (n # _) if n.<(1) => false
case _ => triangularMaths(n, 1)
};
def triangularMaths(j: Int, counter: Int): Boolean = scala.Tuple2.apply[Int, Int](j, counter) match {
case _ if j.-(counter).==(0) => true
case _ if j.-(counter).<(0) => false
case _ => triangularMaths(j.-(counter), counter.+(1))
};
()
};
First define triangularMaths, and then invoke it:
def isTriangular(n: Int): Boolean = {
def triangularMaths(j: Int, counter: Int): Boolean = (j, counter) match {
case _ if j - counter == 0 => true
case _ if j - counter < 0 => false
case _ => triangularMaths(j - counter, counter + 1)
}
n match {
case n if n < 1 => false
case _ => triangularMaths(n, 1)
}
}
Another possibility would be to assign the pattern match to a value, and then return that value as the last expression of the method. But, that would make the compiler complain about forward reference, which you could fix by making it a lazy val instead. I would stick the the re-ordering approach.

How Does One Make Scala Control Abstraction in Repeat Until?

I am Peter Pilgrim. I watched Martin Odersky create a control abstraction in Scala. However I can not yet seem to repeat it inside IntelliJ IDEA 9. Is it the IDE?
package demo
class Control {
def repeatLoop ( body: => Unit ) = new Until( body )
class Until( body: => Unit ) {
def until( cond: => Boolean ) {
body;
val value: Boolean = cond;
println("value="+value)
if ( value ) repeatLoop(body).until(cond)
// if (cond) until(cond)
}
}
def doTest2(): Unit = {
var y: Int = 1
println("testing ... repeatUntil() control structure")
repeatLoop {
println("found y="+y)
y = y + 1
}
{ until ( y < 10 ) }
}
}
The error message reads:
Information:Compilation completed with 1 error and 0 warnings
Information:1 error
Information:0 warnings
C:\Users\Peter\IdeaProjects\HelloWord\src\demo\Control.scala
Error:Error:line (57)error: Control.this.repeatLoop({
scala.this.Predef.println("found y=".+(y));
y = y.+(1)
}) of type Control.this.Until does not take parameters
repeatLoop {
In the curried function the body can be thought to return an expression (the value of y+1) however the declaration body parameter of repeatUntil clearly says this can be ignored or not?
What does the error mean?
Here is a solution without the StackOverflowError.
scala> class ConditionIsTrueException extends RuntimeException
defined class ConditionIsTrueException
scala> def repeat(body: => Unit) = new {
| def until(condition: => Boolean) = {
| try {
| while(true) {
| body
| if (condition) throw new ConditionIsTrueException
| }
| } catch {
| case e: ConditionIsTrueException =>
| }
|
| }
| }
repeat: (body: => Unit)java.lang.Object{def until(condition: => Boolean): Unit}
scala> var i = 0
i: Int = 0
scala> repeat { println(i); i += 1 } until(i == 3)
0
1
2
scala> repeat { i += 1 } until(i == 100000)
scala> repeat { i += 1 } until(i == 1000000)
scala> repeat { i += 1 } until(i == 10000000)
scala> repeat { i += 1 } until(i == 100000000)
scala>
According to Jesper and Rex Kerr here is a solution without the Exception.
def repeat(body: => Unit) = new {
def until(condition: => Boolean) = {
do {
body
} while (!condition)
}
}
You don't need the 2nd pair of braces, the usage should be:
repeatLoop (x) until (cond) //or...
repeatLoop {x} until {cond}
And not:
repeatLoop {x} { until(cond) } //EXTRA PAIR OF BRACES
The error means that Scala thinks you are trying to call a method with a signature something like:
def repeatLoop(x: => Unit)(something: X) //2 parameter lists
And can find no such method. It is saying "repeatLoop(body)" does not take parameters. A full code listing for the solution probably looks something a bit more like:
object Control0 {
def repeatLoop(body: => Unit) = new Until(body)
class Until(body: => Unit) {
def until(cond: => Boolean) {
body;
val value: Boolean = cond;
if (value) repeatLoop(body).until(cond)
}
}
def main(args: Array[String]) {
var y: Int = 1
println("testing ... repeatUntil() control structure")
repeatLoop {
println("found y=" + y)
y += 1
}.until(y < 10)
}
}
There are two useful observations to make here:
The solution is not tail-recursive and will result in a StackOverflowError for long iterations (try while (y < 10000))
The until seems the wrong way round to me (it would be more natural to stop when the condition becomes true, not carry on while it is true).
How about a one liner for repeat until.
def repeat(b: => Unit) = new AnyRef {def until(c: => Boolean) {b; while (! c) b}}
Which, for example, gives:-
scala> repeat {
| println("i = "+i)
| i+=1
| } until (i >= 10)
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
As above yet recursive :)
def repeat(b: => Unit) = new {def until(c: => Boolean) = { b; if (c) until(c) }}
var i = 0
repeat {
println(i)
i+=1
} until (i < 10)
It's #tailrec optimized too.
Llove scala :)

Scala: match and parse an integer string?

I'm looking for a way to matching a string that may contain an integer value. If so, parse it. I'd like to write code similar to the following:
def getValue(s: String): Int = s match {
case "inf" => Integer.MAX_VALUE
case Int(x) => x
case _ => throw ...
}
The goal is that if the string equals "inf", return Integer.MAX_VALUE. If the string is a parsable integer, return the integer value. Otherwise throw.
Define an extractor
object Int {
def unapply(s : String) : Option[Int] = try {
Some(s.toInt)
} catch {
case _ : java.lang.NumberFormatException => None
}
}
Your example method
def getValue(s: String): Int = s match {
case "inf" => Integer.MAX_VALUE
case Int(x) => x
case _ => error("not a number")
}
And using it
scala> getValue("4")
res5: Int = 4
scala> getValue("inf")
res6: Int = 2147483647
scala> getValue("helloworld")
java.lang.RuntimeException: not a number
at scala.Predef$.error(Predef.scala:76)
at .getValue(<console>:8)
at .<init>(<console>:7)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:4)
at RequestResult$.<clinit>(<console>)
at RequestResult$result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Na...
This is better IMHO:
val IntRegEx = "(\\d+)".r
def getValue(s: String): Option[Int] =
s match {
case "inf" => Some(Int.MaxValue)
case IntRegEx(num) => Some(num.toInt)
case _ => None
}
getValue("inf") // Some(2147483647)
getValue("123412") // Some(123412)
getValue("not-a-number") // None
Of course, it doesn't throw any exceptions, but if you really want it, you may use:
getValue(someStr).getOrElse(error("NaN"))
You could use a guard:
def getValue(s: String): Int = s match {
case "inf" => Integer.MAX_VALUE
case _ if s.matches("[+-]?\\d+") => Integer.parseInt(s)
}
How about:
def readIntOpt(x: String) =
if (x == "inf")
Some(Integer.MAX_VALUE)
else
scala.util.Try(x.toInt).toOption
an improved version of James Iry's extractor:
object Int {
def unapply(s: String) = scala.util.Try(s.toInt).toOption
}
Since Scala 2.13 introduced String::toIntOption:
"5".toIntOption // Option[Int] = Some(5)
"abc".toIntOption // Option[Int] = None
we can cast the String as an Option[Int] after checking if it's equal to "inf":
if (str == "inf") Some(Int.MaxValue) else str.toIntOption
// "inf" => Option[Int] = Some(2147483647)
// "347" => Option[Int] = Some(347)
// "ac4" => Option[Int] = None
def getValue(s: String): Int = s match {
case "inf" => Integer.MAX_VALUE
case _ => s.toInt
}
println(getValue("3"))
println(getValue("inf"))
try {
println(getValue("x"))
}
catch {
case e => println("got exception", e)
// throws a java.lang.NumberFormatException which seems appropriate
}