I am new to Scala have to solve this problem:
If you have number "xy" should equal sum of consecutive powers of its digits
xy = x^1 + y^2
For example, take the number 89 and you should have:
89 = 8^1+ 9^2
And if we have a list we should filter with the matched numbers based on the above role.
For example:
Consider these range of numbers:
(1, 100) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 89]
(90, 100) == []
This code works fine, but is this a pure functional programming style or are there some best practices in terms of performance and functionality?
def myFun(str: String):Double ={
val myDigits=str.toList.map(_.asDigit)
(myDigits.indices zip myDigits).toMap.map({case(x , y) => pow(y,x+1)}).reduce(_+_)
}
(1 to 100 ) .toList.filter({ case(x) => x == myFun(x.toString) })
17
have to do with anything? \$\endgroup\$pow
each iteration. \$\endgroup\$