fork download
  1. /* Solution to SPOJ problem "Complete the Sequence!"
  2.  * (https://w...content-available-to-author-only...j.pl/problems/CMPLS/) in Scala. Written in Scala 2.8.
  3.  */
  4. object Main {
  5.  
  6. def main(args: Array[String]) {
  7. val noOfTestCases: Int = readInt
  8. 1 to noOfTestCases foreach { i =>
  9. val input = readLine.trim.split(" ").map { _ toInt }
  10. val s = input(0)
  11. val c = input(1)
  12. val seq: List[Int] = readLine.trim.split(" ").map { _ toInt } toList
  13.  
  14. println(series2(seq).drop(s).take(c).toList.mkString(" "))
  15. }
  16. }
  17.  
  18. /**
  19.   * Takes a sequence of numbers as a list and returns a series representing the
  20.   * sequence as a stream.
  21.   *
  22.   * Non tail recursive version.
  23.   */
  24. def series(seq: List[Int]): Stream[Int] = if (constant(seq)) {
  25. Stream.const(seq.head)
  26. } else {
  27. val lastSeries = series((seq.tail zip seq.init).map { z => z._1 - z._2 } toList)
  28. lazy val thisSeries: Stream[Int] = Stream.cons(
  29. seq.head, (thisSeries zip lastSeries).map { z => z._1 + z._2 })
  30. thisSeries
  31. }
  32.  
  33. /**
  34.   * Takes a sequence of numbers as a list and returns a series representing the
  35.   * sequence as a stream.
  36.   *
  37.   * Tail recursive version.
  38.   */
  39. def series2(seq: List[Int]): Stream[Int] = {
  40. @scala.annotation.tailrec
  41. def inner(seq: List[Int], acc: List[List[Int]]): Stream[Int] = {
  42. if (constant(seq)) {
  43. val constStream = Stream.const(seq.head)
  44. acc.drop(1).foldLeft(constStream){ (lastSeries: Stream[Int], seq: List[Int]) =>
  45. lazy val thisSeries: Stream[Int] = Stream.cons(
  46. seq.head, (thisSeries zip lastSeries).map { z => z._1 + z._2 })
  47. thisSeries
  48. }
  49. } else {
  50. val diff: List[Int] = (seq.tail zip seq.init).map { z => z._1 - z._2 } toList;
  51. inner(diff, diff :: acc)
  52. }
  53. }
  54. inner(seq, List(seq))
  55. }
  56.  
  57. /*
  58.   * Checks if all numbers in list are same, i.e. if the list is a constant sequence.
  59.   */
  60. def constant(sequence: List[Int]) = sequence forall { _ == sequence.head }
  61.  
  62. }
  63.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
4
6 3
1 2 3 4 5 6
8 2
1 2 4 7 11 16 22 29
10 2
1 1 1 1 1 1 1 1 1 2
1 10
3
compilation info
Main.scala:25: error: value const is not a member of object scala.collection.immutable.Stream
    Stream.const(seq.head)
           ^
Main.scala:43: error: value const is not a member of object scala.collection.immutable.Stream
        val constStream = Stream.const(seq.head)
                                 ^
two errors found
Usage: scalac <options> <source files>
where possible standard options include:
  -Dproperty=value                Pass -Dproperty=value directly to the runtime system.
  -J<flag>                        Pass <flag> directly to the runtime system.
  -P:<plugin>:<opt>               Pass an option to a plugin
  -X                              Print a synopsis of advanced options.
  -bootclasspath <path>           Override location of bootstrap class files.
  -classpath <path>               Specify where to find user class files.
  -d <directory|jar>              destination for generated classfiles.
  -dependencyfile <file>          Set dependency tracking file.
  -deprecation                    Emit warning and location for usages of deprecated APIs.
  -encoding <encoding>            Specify character encoding used by source files.
  -explaintypes                   Explain type errors in more detail.
  -extdirs <path>                 Override location of installed extensions.
  -feature                        Emit warning and location for usages of features that should be imported explicitly.
  -g:<level>                      Set level of generated debugging info. (none,source,line,vars,notailcalls) default:vars
  -help                           Print a synopsis of standard options
  -javabootclasspath <path>       Override java boot classpath.
  -javaextdirs <path>             Override java extdirs classpath.
  -language:<_,feature,-feature>  Enable or disable language features: `_' for all, `-language:help' to list
  -no-specialization              Ignore @specialize annotations.
  -nobootcp                       Do not use the boot classpath for the scala jars.
  -nowarn                         Generate no warnings.
  -optimise                       Generates faster bytecode by applying optimisations to the program
  -print                          Print program with Scala-specific features removed.
  -sourcepath <path>              Specify location(s) of source files.
  -target:<target>                Target platform for object files. All JVM 1.5 targets are deprecated. (jvm-1.5,jvm-1.6,jvm-1.7,jvm-1.8) default:jvm-1.6
  -toolcp <path>                  Add to the runner classpath.
  -unchecked                      Enable additional warnings where generated code depends on assumptions.
  -uniqid                         Uniquely tag all identifiers in debugging output.
  -usejavacp                      Utilize the java.class.path in classpath resolution.
  -usemanifestcp                  Utilize the manifest in classpath resolution.
  -verbose                        Output messages about what the compiler is doing.
  -version                        Print product version and exit.
  @<file>                         A text file containing compiler arguments (options and source files)

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