Looking for an inspiration? Below you will find public programs submitted by other users.

Filter by: All Bash C C# C++ 4.8.1 C++11 Haskell Java Java7 Objective-C Pascal (fpc) Pascal (gpc) Perl PHP Python Python 3 Ruby SQL VB.NET Ada Assembler Assembler AWK (gawk) AWK (mawk) bc Brainf**k C++ 4.3.2 C99 strict CLIPS Clojure COBOL COBOL 85 Common Lisp (clisp) D (dmd) Erlang F# Factor Falcon Forth Fortran Go Groovy Icon Intercal JavaScript (rhino) JavaScript (spidermonkey) Lua Nemerle Nice Nimrod Node.js Ocaml Octave Oz PARI/GP Perl 6 Pike Prolog (gnu) Prolog (swi) R Scala Scheme (guile) Smalltalk Tcl Text Unlambda Whitespace

#b0wVpN Scala 51 minutes ago
Success
  1. object Main extends App {
  2. // your code goes here
  3. }
#fQff6M Scala 1 hour ago
Success
  1. object Main extends App {
  2. // I have just spent three days
  3. // trying to do in Scala
  4. // a simple matrix transposition script
  5. // I could have done in Python
  6. // in oh maybe 20 minutes....
  7. //
  8. // Stil don't have it working.
  9. }
#LS3vlg Scala 1 hour ago
Compilation error
  1. import Array._
  2.  
  3. object Main extends App {
  4.  
  5. val older = Array(Array(1,2,3), Array(4,5,6))
  6. val r = older.length
  7. for (i <- 0 until c){
  8. for (j <- 0 until r)
  9. newer(i)(j) = older(j)(i)
#hFFSEy Scala 6 hours ago
Success
  1.  
  2. object Main extends App {
  3. def xx(x:Double):Double = math.pow(x, x)
  4.  
  5. def trapezoid(f:Double=>Double, a:Double, b:Double):Double = {
  6. (b-a)*(f(a)+f(b))/2
  7. }
  8. def simpson(f:Double=>Double, a:Double, b:Double):Double = {
  9. (b-a)*(f(a) + 4*f((a+b)/2) + f(b))/6
#jcKnQC Scala 10 hours ago
Success
  1. object Main extends App {
  2. def makeRowSeq(row: Int) =
  3. for (col <- 1 to 10) yield {
  4.  
  5. val prod = (row * col).toString
  6.  
  7. val padding = " " * (4 - prod.length)
  8.  
  9. padding + prod
#7DBQSs Scala 10 hours ago
Compilation error
  1. def makeRowSeq(row: Int) =
  2. for (col <- 1 to 10) yield {
  3.  
  4. val prod = (row * col).toString
  5.  
  6. val padding = " " * (4 - prod.length)
  7.  
  8. padding + prod
#m9Ybns Scala 15 hours ago
Compilation error
  1. object MlfCal extends App {
  2. val numEx = 20
  3. val nChoices = 5;
  4. val numMcq = 2 // number of Multiple Choice Questions
  5. val numOpt = numMcq * 5 // number of options for all MC question
  6. val numScq = numEx - numMcq // number of Single Choice Questions
  7.  
  8. for (
  9. s <- 0 to numScq;
#PqKZ29 Scala 16 hours ago
Success
  1. import java.util.{Calendar => C}
  2. object Main extends App {
  3. val cal = C.getInstance
  4. (Seq.fill((7 - (cal.get(C.DATE) % 7) + cal.get(C.DAY_OF_WEEK)) % 7)(" ")
  5. ++ (1 to cal.getActualMaximum(C.DATE))).map({
  6. case x : Int => " %2d ".format(x)
  7. case x => x
  8. }).grouped(7).map(x => ("" /: x)(_ + _)).foreach(println)
  9. }
#rc2qcf Scala 16 hours ago
Compilation error
  1. import java.util.{Calendar => C}
  2. object PronamaCal {
  3. def main(args:Array[String]) : Unit = {
  4. val cal = C.getInstance
  5. (Seq.fill((7 - (cal.get(C.DATE) % 7) + cal.get(C.DAY_OF_WEEK)) % 7)(" ")
  6. ++ (1 to cal.getActualMaximum(C.DATE))).map({
  7. case x : Int => " %2d ".format(x)
  8. case x => x
  9. }).grouped(7).map(x => ("" /: x)(_ + _)).foreach(println)
#7Wzf8c Scala 16 hours ago
Success
  1. object Main extends App {
  2. def read(n: Int, nums: List[Int]):List[Int] = {
  3. if (n == 0) nums.reverse
  4. else read(n-1, readInt() :: nums)
  5. } //> read: (n: Int, nums: List[Int])List[Int]
  6.  
  7. def repeat(nums: List[Int], k: Int):List[Int] = {
  8. var counts = nums.groupBy(x=>x).map {
  9. case (x, ls) => (x, ls.size)
#lp9Zr2 Scala 16 hours ago
Success
  1. object Main extends App {
  2. def read(n: Int, nums: List[Int]):List[Int] = {
  3. if (n == 0) nums.reverse
  4. else read(n-1, readInt() :: nums)
  5. } //> read: (n: Int, nums: List[Int])List[Int]
  6.  
  7. def repeat(nums: List[Int], k: Int):List[Int] = {
  8. var counts = nums.groupBy(x=>x).map {
  9. case (x, ls) => (x, ls.size)
#8k7Xu7 Scala 22 hours ago
Success
  1. object Main extends App {
  2. val pattern = """((?=.*[1-9].*)(?=.*[a-z].*)[a-z0-9]{10})""".r
  3. readLine() match {
  4. case pattern(input) => println(input.sortWith((a, b) => {
  5. if (a.isDigit && b.isDigit) a > b
  6. else a < b
  7. }))
  8. case _ => println("입력오류")
  9. }
#W4QFAF Scala 22 hours ago
Success
  1. object Main extends App {
  2. val pattern = """((?=.*[1-9].*)[a-z0-9]{10})""".r
  3. readLine() match {
  4. case pattern(input) => println(input.sortWith((a, b) => {
  5. if (a.isDigit && b.isDigit) a > b
  6. else a < b
  7. }))
  8. case _ => println("입력오류")
  9. }
#vmPlFZ Scala 22 hours ago
Success
  1. object Main extends App {
  2. val pattern = """((?=.*[1-9].*)[a-z0-9]{10}+)""".r
  3. readLine() match {
  4. case pattern(input) => println(input.sortWith((a, b) => {
  5. if (a.isDigit && b.isDigit) a > b
  6. else a < b
  7. }))
  8. case _ => println("입력오류")
  9. }
#pgoVAY Scala 22 hours ago
Compilation error
  1. import Stream._ trait Stream[+A] { // The natural recursive solution def toListRecursive: List[A] = this match { case Cons(h,t) => h() :: t().toListRecursive case _ => List() } /* The above solution will stack overflow for large streams, since it's not tail-recursive. Here is a tail-recursive implementation. At each step we cons onto the front of the `acc` list, which will result in the reverse of the stream. Then at the end we reverse the result to get the correct order again. */ def toList: List[A] = { @annotation.tailrec def go(s: Stream[A], acc: List[A]): List[A] = s match { case Cons(h,t) => go(t(), h() :: acc) case _ => acc } go(this, List()).reverse } /* In order to avoid the `reverse` at the end, we could write it using a mutable list buffer and an explicit loop instead. Note that the mutable list buffer never escapes our `toList` method, so this function is still _pure_. */ def toListFast: List[A] = { val buf = new collection.mutable.ListBuffer[A] @annotation.tailrec def go(s: Stream[A]): List[A] = s match { case Cons(h,t) => buf += h() go(t()) case _ => buf.toList } go(this) } /* `take` first checks if n==0. In that case we need not look at the stream at all. */ def take(n: Int): Stream[A] = if (n > 0) this match { case Cons(h, t) if n == 1 => cons(h(), Stream.empty) // we can say Stream.empty case Cons(h, t) => cons(h(), t().take(n-1)) case _ => Stream.empty } else Stream() // or Stream() /* Unlike `take`, `drop` is not incremental. That is, it doesn't generate the answer lazily. It must traverse the first `n` elements of the stream eagerly. */ def drop(n: Int): Stream[A] = { @annotation.tailrec def go(s: Stream[A], n: Int): Stream[A] = if (n <= 0) s else s match { case Cons(h,t) => go(t(), n-1) case _ => Stream() } go(this, n) } /* It's a common Scala style to write method calls without `.` notation, as in `t() takeWhile f`. */ def takeWhile(f: A => Boolean): Stream[A] = this match { case Cons(h,t) if f(h()) => cons(h(), t() takeWhile f) case _ => empty } def foldRight[B](z: => B)(f: (A, => B) => B): B = // The arrow `=>` in front of the argument type `B` means that the function `f` takes its second argument by name and may choose not to evaluate it. this match { case Cons(h,t) => f(h(), t().foldRight(z)(f)) // If `f` doesn't evaluate its second argument, the recursion never occurs. case _ => z } def exists(p: A => Boolean): Boolean = foldRight(false)((a, b) => p(a) || b) // Here `b` is the unevaluated recursive step that folds the tail of the stream. If `p(a)` returns `true`, `b` will never be evaluated and the computation terminates early. /* Since `&&` is non-strict in its second argument, this terminates the traversal as soon as a nonmatching element is found. */ def forAll(f: A => Boolean): Boolean = foldRight(true)((a,b) => f(a) && b) def takeWhile_1(f: A => Boolean): Stream[A] = foldRight(empty[A])((h,t) => if (f(h)) cons(h,t) else empty) def headOption: Option[A] = foldRight(None: Option[A])((h,_) => Some(h)) def map[B](f: A => B): Stream[B] = foldRight(empty[B])((h,t) => cons(f(h), t)) def filter[B](f: A => Boolean): Stream[A] = foldRight(empty[A])((h,t) => if (f(h)) cons(h, t) else t) def append[B>:A](s: => Stream[B]): Stream[B] = foldRight(s)((h,t) => cons(h,t)) def flatMap[B](f: A => Stream[B]): Stream[B] = foldRight(empty[B])((h,t) => f(h) append t) def mapViaUnfold[B](f: A => B): Stream[B] = unfold(this) { case Cons(h,t) => Some((f(h()), t())) case _ => None } def takeViaUnfold(n: Int): Stream[A] = unfold((this,n)) { case (Cons(h,t), n) if n == 1 => Some((h(), (empty, n-1))) case (Cons(h,t), n) if n > 0 => Some((h(), (t(), n-1))) case _ => None } def takeWhileViaUnfold(f: A => Boolean): Stream[A] = unfold(this) { case Cons(h,t) if f(h()) => Some((h(), t())) case _ => None } def zipWith[B,C](s2: Stream[B])(f: (A,B) => C): Stream[C] = unfold((this, s2)) { case (Cons(h1,t1), Cons(h2,t2)) => Some((f(h1(), h2()), (t1(), t2()))) case _ => None } // special case of `zip` def zip[B](s2: Stream[B]): Stream[(A,B)] = zipWith(s2)((_,_)) def zipAll[B](s2: Stream[B]): Stream[(Option[A],Option[B])] = zipWithAll(s2)((_,_)) def zipWithAll[B, C](s2: Stream[B])(f: (Option[A], Option[B]) => C): Stream[C] = Stream.unfold((this, s2)) { case (Empty, Empty) => None case (Cons(h, t), Empty) => Some(f(Some(h()), Option.empty[B]) -> (t(), empty[B])) case (Empty, Cons(h, t)) => Some(f(Option.empty[A], Some(h())) -> (empty[A] -> t())) case (Cons(h1, t1), Cons(h2, t2)) => Some(f(Some(h1()), Some(h2())) -> (t1() -> t2())) } /* `s startsWith s2` when corresponding elements of `s` and `s2` are all equal, until the point that `s2` is exhausted. If `s` is exhausted first, or we find an element that doesn't match, we terminate early. Using non-strictness, we can compose these three separate logical steps--the zipping, the termination when the second stream is exhausted, and the termination if a nonmatching element is found or the first stream is exhausted. */ def startsWith[A](s: Stream[A]): Boolean = zipAll(s).takeWhile(!_._2.isEmpty) forAll { case (h,h2) => h == h2 } /* The last element of `tails` is always the empty `Stream`, so we handle this as a special case, by appending it to the output. */ def tails: Stream[Stream[A]] = unfold(this) { case Empty => None case s => Some((s, s drop 1)) } append (Stream(empty)) def hasSubsequence[A](s: Stream[A]): Boolean = tails exists (_ startsWith s) /* The function can't be implemented using `unfold`, since `unfold` generates elements of the `Stream` from left to right. It can be implemented using `foldRight` though. The implementation is just a `foldRight` that keeps the accumulated value and the stream of intermediate results, which we `cons` onto during each iteration. When writing folds, it's common to have more state in the fold than is needed to compute the result. Here, we simply extract the accumulated list once finished. */ def scanRight[B](z: B)(f: (A,=>B) => B): Stream[B] = foldRight((z, Stream(z)))((a,p) => { val b2 = f(a,p._1) (b2, cons(b2,p._2)) })._2 @annotation.tailrec final def find(f: A => Boolean): Option[A] = this match { case Empty => None case Cons(h, t) => if (f(h())) Some(h()) else t().find(f) } } case object Empty extends Stream[Nothing] case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A] object Stream { def cons[A](hd: => A, tl: => Stream[A]): Stream[A] = { lazy val head = hd lazy val tail = tl Cons(() => head, () => tail) } def empty[A]: Stream[A] = Empty def apply[A](as: A*): Stream[A] = if (as.isEmpty) empty else cons(as.head, apply(as.tail: _*)) val ones: Stream[Int] = Stream.cons(1, ones) // This is more efficient than `cons(a, constant(a))` since it's just // one object referencing itself. def constant[A](a: A): Stream[A] = { lazy val tail: Stream[A] = Cons(() => a, () => tail) tail } def from(n: Int): Stream[Int] = cons(n, from(n+1)) val fibs = { def go(f0: Int, f1: Int): Stream[Int] = cons(f0, go(f1, f0+f1)) go(0, 1) } def unfold[A, S](z: S)(f: S => Option[(A, S)]): Stream[A] = f(z) match { case Some((h,s)) => cons(h, unfold(s)(f)) case None => empty } /* Scala provides shorter syntax when the first action of a function literal is to match on an expression. The function passed to `unfold` in `fibsViaUnfold` is equivalent to `p => p match { case (f0,f1) => ... }`, but we avoid having to choose a name for `p`, only to pattern match on it. */ val fibsViaUnfold = unfold((0,1)) { case (f0,f1) => Some((f0,(f1,f0+f1))) } def fromViaUnfold(n: Int) = unfold(n)(n => Some((n,n+1))) def constantViaUnfold[A](a: A) = unfold(a)(_ => Some((a,a))) // could also of course be implemented as constant(1) val onesViaUnfold = unfold(1)(_ => Some((1,1))) }
#zHte5i Scala 22 hours ago
Compilation error
  1. import Stream._
  2. trait Stream[+A]
  3.  
  4. case object Empty extends Stream[Nothing]
  5. case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]
  6.  
  7. object Stream {
  8.  
  9. def cons[A](hd: => A, tl: => Stream[A]): Stream[A] = {
#irnn1I Scala 22 hours ago
Compilation error
  1. import Stream._
  2. trait Stream[+A]
  3.  
  4. case object Empty extends Stream[Nothing]
  5. case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]
  6.  
  7. object Stream {
  8. def constant[A](a: A): Stream[A] = {
  9. lazy val tail: Stream[A] = Cons(() => a, () => tail)
#QCm6Vv Scala 22 hours ago
Success
  1. import Stream._
  2. trait Stream[+A]
  3.  
  4. case object Empty extends Stream[Nothing]
  5. case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]
  6.  
  7. object Stream {
  8. def constant[A](a: A): Stream[A] = {
  9. lazy val tail: Stream[A] = Cons(() => a, () => tail)
#hUlo2n Scala 23 hours ago
Success
  1. object Main extends App {
  2. val pattern = """([a-z0-9]{10}+)""".r
  3. readLine() match {
  4. case pattern(input) => println(input.sortWith((a, b) => {
  5. a match {
  6. case a if a.isDigit && b.isDigit => a > b
  7. case _ => a < b
  8. }
  9. }))
#UUIsec Scala 1 day ago
Success
  1. object Main extends App {
  2. val pattern = """([a-z0-9]{10}+)""".r
  3. readLine() match {
  4. case pattern(input) => println(input.sorted.sortWith((a, b) => {
  5. a match {
  6. case a if a.isDigit => a > b
  7. case _ => a < b
  8. }
  9. }))
#s0dNF7 Scala 1 day ago
Success
  1.  
  2.  
  3. object Main extends App {
  4.  
  5. val pattern = """([a-z0-9]{10}+)""".r
  6. readLine() match {
  7. case pattern(input) => println(input.sortWith(sort))
  8. case _ => println("제대로 입력해라")
  9. }
#785Jds Scala 1 day ago
Compilation error
  1. import scala.io.StdIn
  2.  
  3. object Main extends App {
  4.  
  5. val pattern = """([a-z0-9]{10}+)""".r
  6. StdIn.readLine() match {
  7. case pattern(input) => println(input.sortWith(sort))
  8. case _ => println("제대로 입력해라")
  9. }
#Sgw8Rx Scala 1 day ago
Not running
  1. trait Similarity {
  2. def isSimilar(x: Any): Boolean
  3. def isNotSimilar(x: Any): Boolean = !isSimilar(x)
  4. }
  5.  
  6. class Point(xc: Int, yc: Int) extends Similarity {
  7. var x: Int = xc
  8. var y: Int = yc
  9. def isSimilar(obj: Any) =
#8fWFwD Scala 1 day ago
Not running
  1. import java.util.{Date, Locale}
  2. import java.text.DateFormat._
  3.  
  4. object Today extends Application {
  5.  
  6. override def main(args: Array[String]) {
  7.  
  8. val now = new Date
  9. val frenchDateFormatter = getDateInstance(LONG, Locale.FRANCE)
#ZWH8xh Scala 1 day ago
Not running
  1. object TimerExample {
  2. /** Runs the passed in function pausing approxmimately one second in between calls.
  3.   * as long as the function returns true.
  4.   */
  5. def oncePerSecond(callback: () => Boolean) {
  6. while(callback()) {
  7. Thread sleep 1000
  8. }
  9. }
#CikEY8 Scala 1 day ago
Not running
  1. /* Example of case class */
  2.  
  3.  
  4. case class Var(name: String) extends Term
  5. case class Fun(arg: String, body: Term) extends Term
  6. case class App(f: Term, v: Term) extends Term
  7.  
  8. object TermTest extends Application {
#mbJGbG Scala 1 day ago
Not running
  1. object HelloWorld extends Application {
  2. println("Hello, World!")
  3. }
#jCTRQW Scala 1 day ago
Not running
  1. class HelloClass {
  2. def inClass() {
  3. println("In class")
  4. }
  5. }
  6.  
  7. object HelloObjectPlusClass {
  8. println("In object")
#nWTQUE Scala 1 day ago
Not running
  1. object HelloObject {
  2. println("In object")
  3.  
  4. def main(args: Array[String]) {
  5. println("In main")
  6. }
  7.  
  8. }
#IblKkF Scala 1 day ago
Not running
  1. object HelloClass {
  2.  
  3. abstract class Person {
  4. def name: String
  5. def age: Int
  6.  
  7. def getName(): String = {
  8. name
  9. }