Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
out/
*.iml
1 change: 0 additions & 1 deletion README.md

This file was deleted.

17 changes: 0 additions & 17 deletions lista1.txt

This file was deleted.

27 changes: 0 additions & 27 deletions lista2.txt

This file was deleted.

13 changes: 13 additions & 0 deletions lista2/Zad1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def split(lista: List[Double]): (List[Double], List[Double]) = {

def splitIn(lista: List[Double], listInsideOne:List[Double], listOutsideOne:List[Double]):(List[Double], List[Double]) =
(lista) match {
case Nil => (listOutsideOne, listInsideOne)
case hd::tl =>
if (hd >= 1 || hd <= -1) splitIn(tl, hd::listInsideOne, listOutsideOne) else
splitIn(tl, listInsideOne, hd::listOutsideOne)
}

splitIn(lista, List(), List())

}
5 changes: 5 additions & 0 deletions lista2/Zad2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

def dlugosc(list:List[Int]):Int =
if(list == Nil) 0 else
if(list.head <= 1 && list.head >= -1) 1 + dlugosc(list.tail) else
dlugosc(list.tail)
7 changes: 7 additions & 0 deletions lista2/Zad3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def polacz (list1:List[Double], list2:List[Double]):List[Double] =
(list1, list2) match {
case (Nil, Nil) => 0.1::0.1::Nil
case (Nil, restOfList) => 0.1::restOfList
case (restOfList, Nil) => restOfList::0.1::Nil
case (hd1::tl1, hd2::tl2) => hd1::hd2::polacz(tl1, tl2)
}
9 changes: 9 additions & 0 deletions lista3/Zad1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def filter [A](listOfLists:List[List[A]], toValidate: A):List[List[A]] = {
def filterHelper[A](listOfLists: List[List[A]], result: List[List[A]]): List[List[A]] =
if (listOfLists == Nil) result
else if (listOfLists.head.contains(toValidate)) filterHelper(listOfLists.tail, listOfLists.head :: result)
else filterHelper(listOfLists.tail, result)
filterHelper(listOfLists, List())
}

def simpleFilter [A](listOfLists:List[List[A]], toValidate: A):List[List[A]] = listOfLists.filter(_.contains(toValidate))
12 changes: 12 additions & 0 deletions lista3/Zad2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def transformFromDecimal (number:Int, system: Int):List[Int] = {
if (system < 2) throw new Exception("Numeric system cannot be smaller than 2")
else if (number < 0) throw new Exception ("Cannot convert negative number")
else if (number == 0) List(0)
else {
def tFD(number: Int, result: List[Int]): List[Int] =
if (number == 0) result
else tFD(number / system, number % system :: result)
tFD(number, List())
}
}

9 changes: 9 additions & 0 deletions lista4/Zad1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def filter [A](listOfLists:List[List[A]], toValidate: A):List[List[A]] = {
def filterHelper[A](listOfLists: List[List[A]], result: List[List[A]]): List[List[A]] =
if (listOfLists == Nil) result
else if (listOfLists.head.contains(toValidate)) filterHelper(listOfLists.tail, listOfLists.head :: result)
else filterHelper(listOfLists.tail, result)
filterHelper(listOfLists, List())
}

def simpleFilter [A](listOfLists:List[List[A]], toValidate: A):List[List[A]] = listOfLists.filter(_.contains(toValidate))
12 changes: 12 additions & 0 deletions lista4/Zad2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def transformFromDecimal (number:Int, system: Int):List[Int] = {
if (system < 2) throw new Exception("Numeric system cannot be smaller than 2")
else if (number < 0) throw new Exception ("Cannot convert negative number")
else if (number == 0) List(0)
else {
def tFD(number: Int, result: List[Int]): List[Int] =
if (number == 0) result
else tFD(number / system, number % system :: result)
tFD(number, List())
}
}

56 changes: 56 additions & 0 deletions lista5/lista5.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
sealed trait BT[+A]
case object Empty extends BT[Nothing]
case class Node[+A](elem: Int, left:BT[A], right:BT[A]) extends BT[A]

def generateTreeFromList(listOfNodes:List[Int], elementID:Int):BT[Int] = {
if(elementID < listOfNodes.length) Node(listOfNodes(elementID), generateTreeFromList(listOfNodes, 2 * elementID + 1), generateTreeFromList(listOfNodes, 2 * elementID + 2))
else Empty
}

//zad1

def generateTree(depth: Int): BT[Int] = {
if (depth > 0)
Node(util.Random.nextInt(10) + 1, generateTree(depth-1), generateTree(depth-1))
else
Empty
}

//zad2

def multiplyTree(tree: BT[Int]): Int = tree match {
case Empty => 1
case Node(v, l, r) => v * multiplyTree(l) * multiplyTree(r)
}

//zad3

def depthBT(tree:BT[Int]):List[Int] = {
def depthBTHelper(stack:List[BT[Int]], met: List[Int]):List[Int] = {
stack match {
case Nil => Nil
case Empty::tail => depthBTHelper(tail, met)
case Node(v, l, r)::tail => if(met.contains(v)) (generateNewRandomNumber(met))::depthBTHelper(List(l, r)++tail, met)
else v::depthBTHelper(List(l, r)++tail, v::met)
}
}
depthBTHelper(List(tree), List())
}

def breadthBT(tree:BT[Int]):List[Int] = {
def breadthBTHelper(queue: List[BT[Int]], met: List[Int]): List[Int] = queue match {
case Nil => Nil
case Empty::tail => breadthBTHelper(tail, met)
case Node(v, l, r)::tail => if(met.contains(v)) (generateNewRandomNumber(met))::breadthBTHelper(tail++List(l, r), met)
else v::breadthBTHelper(tail++List(l, r), v::met)
}
breadthBTHelper(List(tree), List())
}

def generateNewRandomNumber(duplicates:List[Int]):Int = {
val x = util.Random.nextInt(100) + 1
if(!duplicates.contains(x)) x
else generateNewRandomNumber(duplicates)
}


12 changes: 12 additions & 0 deletions lista6/zad1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def lfrom (k:Int):LazyList[Int] = k#::lfrom(k+1)

def eachNElement [A](llist:LazyList[A], n:Int, m:Int):LazyList[A] = {
if (m < 1) throw new Exception ("Last element smaller than 1")
if (n < 1) throw new Exception ("Nth element smaller than 1")
def helper (llist:LazyList[A], nh:Int, i:Int):LazyList[A] = {
if (i > m) LazyList()
else if(nh > 0) helper (llist.tail, nh-1, i+1)
else llist.head #:: helper(llist.tail, n-1, i+1)
}
llist.head#::helper(llist, n, 1)
}
32 changes: 32 additions & 0 deletions lista6/zad2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def lfrom (k:Int):LazyList[Int] = k#::lfrom(k+1)

def addition (elem1: Int, elem2: Int): Int = elem1 + elem2
def subtraction (elem1: Int, elem2: Int): Int = elem1 - elem2
def multiplication (elem1: Int, elem2: Int): Int = elem1 * elem2
def division (elem1: Int, elem2: Int): Int = {
if (elem2 == 0) throw new Exception("Cannot divide by 0")
else elem1 / elem2
}

def chooseOperation(symbol:String):(Int, Int) => Int = {
symbol match {
case "+" => addition
case "-" => subtraction
case "*" => multiplication
case "/" => division
case _ => throw new Exception("Unknown operation")
}
}

def loperation(llist1: LazyList[Int], llist2: LazyList[Int], operationSymbol:String):LazyList[Int] = {
val operation = chooseOperation(operationSymbol)
def helper(llist1: LazyList[Int], llist2: LazyList[Int]):LazyList[Int] = {
(llist1, llist2) match {
case (LazyList(), LazyList()) => LazyList()
case (LazyList(), _) => llist2
case (_, LazyList()) => llist1
case (hd1 #:: tl1, hd2 #:: tl2) => operation(hd1, hd2) #::helper(tl1, tl2)
}
}
helper(llist1, llist2)
}
25 changes: 25 additions & 0 deletions lista7/lista7.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import scala.collection.immutable.ListSet

def duplicate (howMany: List[Int], list: List[Int]): List[Int] = {
if (howMany == Nil || list == Nil) Nil
else {
def repeat(rpt: Int, rest: List[Int]): List[Int] = {
if (rpt > 0) rest.head :: repeat(rpt - 1, rest)
else duplicate(howMany.tail, rest.tail)
}
repeat(howMany.head, list)
}
}

//zad2

def duplicateWithoutRepeats (howMany: List[Int], set: ListSet[Int]): List[Int] = {
if (howMany == Nil || set.isEmpty) Nil
else {
def repeat(rpt: Int, rest: ListSet[Int]): List[Int] = {
if (rpt > 0) rest.head :: repeat(rpt - 1, rest)
else duplicateWithoutRepeats(howMany.tail, rest.tail)
}
repeat(howMany.head, set)
}
}
37 changes: 37 additions & 0 deletions lista8/src/Point.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.lang.reflect.Field

trait Debug {
def debugName(): String
def debugVars(): Unit
}

class Point(xv: Int, yv: Int) extends Debug {
var x: Int = xv
var y: Int = yv
var a: String = "test"

def parseToString(x: Field):String =
"Pole: " + x.getName + " => " + x.getType.toString + ", " + x.get(this)

def arrayOfDeclaredFields(): Array[String] =
this.getClass.getDeclaredFields.map { elem =>
elem.setAccessible(true)
val res = parseToString(elem)
elem.setAccessible(false)
res
}

def debugName(): String = "Klasa: " + a
def debugVars(): Unit = arrayOfDeclaredFields().foreach(elem => println(elem))


}

object Lista8 {
def main(args: Array[String]): Unit = {
val p1 = new Point(3,6)

println(p1.debugName())
p1.debugVars()
}
}
44 changes: 0 additions & 44 deletions wymagania.txt

This file was deleted.