Institute of Computer Science
  1. Courses
  2. 2019/20 fall
  3. Programming Languages (MTAT.03.006)
ET
Log in

Programming Languages 2019/20 fall

  • Info
  • Õppekava
  • Moodle
  • Loengud & Praksid
  • Lisamaterjalid
  • Küsi abi! (Fleep)

Scala esimene kodutöö

Ülesanne 1

Korduvate elementidega listi tuvastamine. Kirjuta (näiteks kasutades mustrisobitust ja rekursiooni) meetod korduvad, mis tagastab true parajasti siis kui listis leidub korduvaid elemente.

object Korduvad {
  def korduvad(l: List[Int]): Boolean = ???

  def main(args: Array[String]): Unit = {
    assert(!korduvad(List()))
    assert(!korduvad(List(1, 2, 3, 4)))
    assert(!korduvad(List(1, 2, 3, 4, 5)))
    assert(korduvad(List(1, 1)))
    assert(korduvad(List(1, 2, 3, 4, 4)))
  }
}

Ülesanne 2

Kasutades foldLeft, kirjuta meetod maksta, mis tagastab argumendiks saadud arvete hulgast mittemakstud arvete kogusumma.

object MaksaArved {
  class Arve {
    var summa   = 0d
    var toode   = ""
    var makstud = false
  }

  def maksta(as: List[Arve]): Double = {
    as.foldLeft(???)(???)
  }

  def main(args: Array[String]): Unit = {
    val a1 = new Arve; a1.toode = "A"; 
    a1.summa = 10; a1.makstud = false
    val a2 = new Arve; a2.toode = "B"; 
    a2.summa = 22; a2.makstud = true
    val a3 = new Arve; a3.toode = "B"; 
    a3.summa = 22; a3.makstud = false
    val a4 = new Arve; a4.toode = "A"; 
    a4.summa = 10; a4.makstud = false
    assert(maksta(List(a1, a2, a3, a4)) == 42)
    assert(maksta(List(a1, a2, a3)) == 32)
    assert(maksta(List(a1, a2, a4)) == 20)
    assert(maksta(List()) == 0)
  }

}

Ülesanne 3

Kirjuta meetod kontrolliKontot, mis kontrollib, kas konto tehingute jooksul pole kunagi mindud miinusesse rohkem kui 1000 ühikut. Pane tähele et konto avatakse tühjalt.


object Krediidikontroll {
  type Raha = Int
  type KontoAjalugu = List[Raha]

  // Kui palju võib võlgu minna:
  def krediit: Raha = 1000

  def kontrolliKontot(konto: KontoAjalugu): Boolean = ???

  def main(args: Array[String]): Unit = {
    assert(kontrolliKontot(List(10,-5,20)))
    assert(kontrolliKontot(List(-1000)))
    assert(!kontrolliKontot(List(-1000,-1)))
    assert(!kontrolliKontot(List(10,-1000,10,-30,10000)))
  }
}

Ülesanne 4

Tõlgi Haskelli funktsioon transpose Scalasse, meetodiks transponeeri. (S.t. ärge siin kasutage standardteegi meetodit transpose.)


object Transponeerimine {
  val m1 = List(List( 1, 2, 3),
                List(11,12,13),
                List(21,22,23))

  def prindiMaatriks(xss: List[List[Int]]): Unit = {
    for (xs <- xss) {
      for (x <- xs) {
        printf("%3d", x)
      }
      println()
    }
  }

  // Haskellis on järgneva funktsiooni nimi 'transpose'.
  // tr               :: [[a]] -> [[a]]
  // tr []             = []
  // tr ([]   : xss)   = tr xss
  // tr ((x:xs) : xss) = (x : map head xss]) : tr (xs : map tail xss)
  def transponeeri(m: List[List[Int]]): List[List[Int]] = ???

  def main(args: Array[String]): Unit = {
    prindiMaatriks(m1)
    println()
    prindiMaatriks(transponeeri(m1))
  }
}
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment