Advertisement
valtih1978

Empty list as identity eleent in collector of lists

Jun 27th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.07 KB | None | 0 0
  1.   def listWithoutRepetitions(s: Int, used: List[Int], available: List[Int], acc: List[List[Int]]): List[List[Int]] = {
  2.     if (s == 0) used :: acc else
  3.        if (s < 0 || available.isEmpty) acc
  4.         else available match {
  5.           case d :: ds => {
  6.             val left = listWithoutRepetitions(s, used, ds, acc)
  7.             listWithoutRepetitions(s-d, d :: used, ds, left)
  8.           }
  9.         }
  10.   }                                               //> listWithoutRepetitions: (s: Int, used: List[Int], available: List[Int], acc
  11.                                                   //| : List[List[Int]])List[List[Int]]
  12.  
  13.   countWithoutRepetitions(10, List(1, 2, 3, 5))   //> res1: Int = 1
  14.   listWithoutRepetitions(10, List(), List(1, 2, 3, 5), Nil : List[List[Int]])
  15.  
  16. The problem is that I am getting empty list in the output always, in addition to the items collected. Assignment https://class.coursera.org/progfun-004/assignment/view?assignment_id=17 misteriously says "Note that the anagram of the empty sentence is the empty sentence itself." I do not know what does it mean.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement