-2

I am a beginner in Scala and in programming as such. Trying to shift to Data Science and am learning stuff on my own. I apologize if this is a very trivial question. Can someone please tell me what is wrong with the main function here? I am trying to pass arguments in the countChange function but the code doesn't print anything. I have tried to look it up and understand, but I am totally lost and any help would be much appreciated! Thank you!

object main {
  def countChange(money: Int, change: List[Int]): Int = {
    def totalWays(sum: Int, denomination: List[Int]): Int = {
      if (sum < 0) 0
      else
        if (sum == 0) 1
        else
          if (denomination.isEmpty && sum >= 0) 0
          else
           countChange(sum, denomination.tail) + countChange(sum-denomination.head,denomination) 
    }
    countChange(money, change.sortWith(_.compareTo(_) < 0))

  }


  def main(args: Array[String]) {
    val l = List(1,2)
    println(countChange(0,l))

}
}
Gingerbread
  • 1,938
  • 8
  • 22
  • 36
  • Does it compile? This part doesn't seem right `change.sortWith(_.compareTo(_) < 0)`. – pedrofurla May 26 '16 at 04:14
  • @pedrofurla : No, it doesn't! Oh! Can you please tell me what is wrong about it? Infact, I referenced this code from the following link: http://stackoverflow.com/questions/12629721/coin-change-algorithm-in-scala-using-recursion – Gingerbread May 26 '16 at 05:20
  • 2
    I plugged the code into my IDE (IntelliJ) and it seems to compile, but I noticed two things: 1) the `totalWays()` function is never called, and 2) the `countChange()` function is endlessly recursive; there is no bailout condition. – jwvh May 26 '16 at 05:33
  • @jwvh : Ohh! Can you suggest how else I can go about this please? – Gingerbread May 26 '16 at 06:18

1 Answers1

2

The details pointed in the comments by @jwvh are on track. It seems you copied some stuff from somewhere and you got lost in the way. You don't need the totalWays inner function, since it implements already what you intended to do when calling countChange. So, you can simply unwrap it and rename the variables accordingly:

  def countChange(money: Int, denomination: List[Int]): Int = {
    if (money < 0) 0
    else if (money == 0) 1
    else if (denomination.isEmpty && money >= 0) 0
    else
      countChange(money, denomination.tail) + countChange(money - denomination.head, denomination)
  }

Also, you should be calling it with the initial amount and the list of denominations:

  def main(args: Array[String]): Unit = {
    val l = List(1,2)
    println(countChange(3,l))
  } 
  // prints: 2
ale64bit
  • 6,232
  • 3
  • 24
  • 44
  • 1
    this is a good solution. 2 notes: if 'denomination' is empty this it does not matter what 'money' equals as you can't make change. so the and statement isn't entirely correct. also you have 2 statements both returning 0, they can be combined with an or statement. the resulting code would be if-else if-else structure. – Max Wen May 26 '16 at 06:33
  • @ale64bit : Thank you so much! I see what you mean and it works perfectly! Thank you again! Also, even if I call it with money=0, it will still work as according to the code, I should get a 1. Thank you again! – Gingerbread May 26 '16 at 06:40
  • @Max Wen : Yes, you are correct! I will try that! Thank you! – Gingerbread May 26 '16 at 06:40
  • @MaxWen Valid advices :) Many things can be improved for sure; the point was to show the OP that the inner function wasn't necessary. – ale64bit May 26 '16 at 06:41
  • @ale64bit Sorry if that sounded nitpicky, I just did that same assignment yesterday so I couldn't help myself : D – Max Wen May 26 '16 at 07:24