找出数字是否是scala中的一个好数字 [英] find out if a number is a good number in scala

查看:102
本文介绍了找出数字是否是scala中的一个好数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是scala函数编程方法的新手.我想在函数中输入一个数字,然后检查它是否是一个好数字. 如果数字的每个数字都大于该数字右侧的数字总和,那么它就是一个好数字. 例如: 9620等于(2> 0,6> 2 + 0,9> 6 + 2 + 0) 我用来解决此问题的步骤是

Hi I am new to scala functional programming methodology. I want to input a number to my function and check if it is a good number or not. A number is a good number if its every digit is larger than the sum of digits which are on the right side of that digit.  For example: 9620  is good as (2 > 0, 6 > 2+0, 9 > 6+2+0) steps I am using to solve this is

1. converting a number to string and reversing it
2. storing all digits of the reversed number as elements of a list
3. applying for loop from  i equals 1 to length of number - 1
4. calculating sum of first i digits as num2
5. extracting ith digit from the list as digit1 which is one digit ahead of the first i numbers for which we calculated sum because list starts from zero.
6. comparing output of 4th and 5th step. if num1 is greater than num2 then we will break the for loop and come out of the loop to print it is not a good number.

请在下面找到我的代码

val num1 = 9521.toString.reverse
val list1 = num1.map(_.todigit).toList
for (i <- 1 to num1.length - 1) {
  val num2 = num1.take(i).map(_.toDigits) sum
  val digit1 = list1(i)
  if (num2 > digit1) {
    print("number is not a good number")
    break
  }
}

我知道这不是解决此问题的最优化方法.我也在寻找一种使用尾部递归进行编码的方法,在该方法中我传递了两个数字,并使所有好的数字都落在这两个数字之间. 可以以更优化的方式完成此操作吗? 预先感谢!

I know this is not the most optimized way to solve this problem. Also I am looking for a way to code this using tail recursion where I pass two numbers and get all the good numbers falling in between those two numbers. Can this be done in more optimized way? Thanks in advance!

推荐答案

使用此功能:(这将是一种有效的方法,因为功能forall不会遍历整个数字列表.从向量v的左到右遍历时,立即发现错误条件(即 v(i)>v.drop(i+1).sum 变为 false 时) >

Using this function:(This will be the efficient way as the function forall will not traverse the entire list of digits. it stops when it finds the false condition immediately ( ie., when v(i)>v.drop(i+1).sum becomes false) while traversing from left to right of the vector v. )

  def isGood(n: Int)= {
   val v1 = n.toString.map(_.asDigit)
   val v = if(v1.last!=0) v1 else v1.dropRight(1)
   (0 to v.size-1).forall(i=>v(i)>v.drop(i+1).sum)
  }

如果要以整数范围(n1 to n2)查找良好的数字,可以使用此功能:

If we want to find good numbers in an interval of integers ranging from n1 to n2 we can use this function:

def goodNums(n1:Int,n2:Int) = (n1 to n2).filter(isGood(_))

在Scala REPL中:

In Scala REPL:

scala> isGood(9620)
res51: Boolean = true

scala> isGood(9600)
res52: Boolean = false

scala> isGood(9641)
res53: Boolean = false

scala> isGood(9521)
res54: Boolean = true

scala> goodNums(412,534)
res66: scala.collection.immutable.IndexedSeq[Int] = Vector(420, 421, 430, 510, 520, 521, 530, 531)

scala> goodNums(3412,5334)
res67: scala.collection.immutable.IndexedSeq[Int] = Vector(4210, 5210, 5310)

这篇关于找出数字是否是scala中的一个好数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆