fork download
  1. object ContigiousNumber {
  2.  
  3.  
  4. //Scala Proejct will start from main
  5. def main(args: Array[String]){
  6. println(getLongestLists(getPossibleSequenceLists(List(19,3,11,7,15,12,4,12,8,16))));
  7.  
  8.  
  9. }
  10. // return all of possible sequence lists which have any length from input numbers
  11. // it uses dynamic programming ways which means that it starts to big problem from small one to solve the problem
  12. def getPossibleSequenceLists(xs:List[Int]) : List[List[Int]] = {
  13. if(xs.size == 1)
  14. List(xs)
  15. getPossibleSequenceLists(xs.init)++getAddedLists(getPossibleSequenceLists(xs.init), xs.last)
  16. /* Last result have all possible result except next inputs,
  17. the next input comes in last result,
  18. then some lists should be added from getAddedLists method
  19.  
  20. */
  21. }
  22.  
  23.  
  24. // return max size from list of lists
  25. def max(xs: List[List[Int]]): Int = xs match {
  26. case Nil => -1
  27. case List(x: List[Int]) => x.size
  28. case x :: y :: rest => max( (if (x.size > y.size) x else y) :: rest )
  29. }
  30.  
  31.  
  32. /* There are two input values in this function
  33. xs = list of the possible sequence lists
  34. i = next continuous value
  35. For example,
  36. x =
  37. (1,2)
  38. (3)
  39. (6),
  40. i = 4,
  41. then output is (1,2,4), (3,4), (4)
  42.  
  43. */
  44. def getAddedLists(xs:List[List[Int]], i:Int) : List[List[Int]] = {
  45. if(xs.size == 0)
  46. List(List(i))
  47. if( xs.last.last < i)
  48. getAddedLists(xs.init, i)++List((xs.last++List(i)))
  49. getAddedLists(xs.init, i)
  50.  
  51. }
  52.  
  53. /*
  54. * return list of sequences which have the longest length
  55. * it performs after the possible list of possible sequences which have any length of it.
  56. */
  57. def getLongestLists(xs:List[List[Int]]) : List[List[Int]] = {
  58. if(xs.size == 1)
  59. List(xs.head)
  60. if(xs.last.size > max(xs.init))
  61. List(xs.last)
  62. if(xs.last.size == max(xs.init))
  63. List(xs.last)++getLongestLists(xs.init)
  64. getLongestLists(xs.init)
  65. }
  66. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/opt/scala/bin/scalac: line 50: /dev/null: Permission denied
spoj: The program compiled successfully, but Main.class was not found.
      Class Main should contain method: def main(args: Array[String]).
stdout
Standard output is empty