diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a18d1f6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/L2Z1.sc b/L2Z1.sc new file mode 100644 index 0000000..65c921c --- /dev/null +++ b/L2Z1.sc @@ -0,0 +1,17 @@ +def podziel (list: List[Double])={ + def znajdzPozaZakresem(li: List[Double], res: List[Double], pop: List[Double]):(List[Double], List[Double], List[Double])={ + if(li==Nil) (Nil,res,pop) + else{ + if(li.head<=(-1.0) || li.head>=1.0) znajdzPozaZakresem(li.tail, res:::List(li.head), pop) + else znajdzPozaZakresem(li.tail, res, pop:::List(li.head)) + } + } + + val wynik = znajdzPozaZakresem(list, List(), List()) + (wynik._2, wynik._3) +} + +podziel(List(-3.0,-2.0,0.0,0.1,2)) +podziel(List(1)) + + diff --git a/L2Z2.sc b/L2Z2.sc new file mode 100644 index 0000000..b594245 --- /dev/null +++ b/L2Z2.sc @@ -0,0 +1,9 @@ +def dlugoscBezwzglednych(lista: List[Double]):Int={ + if(lista==Nil) 0 + else{ + if(lista.head<=1.0 && lista.head>=(-1.0)) 1+dlugoscBezwzglednych(lista.tail) + else dlugoscBezwzglednych(lista.tail) + } +} + +dlugoscBezwzglednych(List(-3.0,-2.0,-0.9,0.0,0.1,1.0,2.0)) \ No newline at end of file diff --git a/L2Z3.sc b/L2Z3.sc new file mode 100644 index 0000000..aca2765 --- /dev/null +++ b/L2Z3.sc @@ -0,0 +1,23 @@ +def polacz(xs: List[Double], ys: List[Double]): List[Double] = { + def polaczNaprzemiennie(xs:List[Double],ys:List[Double]):List[Double]={ + (xs,ys) match{ + case(Nil,Nil)=> Nil + case(h1::t1,Nil)=>h1::polaczNaprzemiennie(t1,Nil) + case(Nil,h2::t2)=>h2::polaczNaprzemiennie(Nil,t2) + case(h1::t1,h2::t2)=>h1::h2::polaczNaprzemiennie(t1,t2) + } + } + + (xs,ys) match{ + case(Nil,Nil) => 0.1::Nil + case(_,Nil) => xs:::List(0.1) + case(Nil,_) => List(0.1):::ys + case(h1::t1,h2::t2) => h1::h2::polaczNaprzemiennie(t1,t2) + } + +} + +polacz(List(5,4,3,2), List(1,2,3,4,5,6)) +polacz(List(),List(1,2,3,4,5)) +polacz(List(1,2,3,4,5),List()) +polacz(List(),List()) \ No newline at end of file diff --git a/L3Z1.sc b/L3Z1.sc new file mode 100644 index 0000000..546a4aa --- /dev/null +++ b/L3Z1.sc @@ -0,0 +1,63 @@ +def reverse[A](list: List[A]):List[A]={ + def rev(li: List[A],res:List[A]):List[A]={ + if(li==Nil)res + else rev(li.tail,li.head::res) + } + rev(list,List()) +} + +def containsStrRegex(elem: String, toFind:String):Boolean={ + val pattern=".*"+toFind+".*" + (elem matches pattern) +} + +def findInListTail (list: List[String], element: String, total: Int): List[String] ={ + + def findRec(li: List[String], res: List[String], elem: String, tot: Int): List[String] ={ + if(li==Nil || tot==0) reverse(res) + else{ + if(containsStrRegex(li.head,elem)) findRec(li.tail,li.head::res,elem, tot-1) + else findRec(li.tail,res,elem, tot) + } + } + findRec(list, List(), element, total) +} + +def findInList (li: List[String], elem: String, total: Int): List[String] ={ + if(li==Nil || total==0) Nil + else{ + if(containsStrRegex(li.head,elem)) li.head::findInList(li.tail,elem, total-1) + else findInList(li.tail,elem,total) + } +} + +def containsNStrRegex(elem: String, toFind:List[String]):Boolean={ + if(toFind==Nil) false + else if(containsStrRegex(elem, toFind.head)) true + else containsNStrRegex(elem,toFind.tail) +} + +def findInList2 (li: List[String], elem: List[String], total: Int): List[String]={ + if(li==Nil || total==0) Nil + else{ + if(containsNStrRegex(li.head,elem)) li.head::findInList2(li.tail,elem, total-1) + else findInList2(li.tail,elem, total) + } +} + +def findInList2Tail (list: List[String], element: List[String], total: Int): List[String]={ + + def findRec(li: List[String], res: List[String], elem: List[String], tot: Int): List[String] ={ + if(li==Nil || tot==0) reverse(res) + else{ + if(containsNStrRegex(li.head,elem)) findRec(li.tail,li.head::res,elem, tot-1) + else findRec(li.tail,res,elem, tot) + } + } + findRec(list, List(), element, total) +} + +findInList(List("index0169", "index0170", "index0168202", "index0168211", "index0168210", "index0169222", "index0169224"), "index0168", 2) +findInListTail(List("index0169", "index0170", "index0168202", "index0168211", "index0168210", "index0169222", "index0169224"), "index0168", 2) +findInList2(List("index0169", "index0170", "index0168202", "index0168211", "index0168210", "index0169222", "index0169224"), List("index0168", "index0169"), 3) +findInList2Tail(List("index0169", "index0170", "index0168202", "index0168211", "index0168210", "index0169222", "index0169224"), List("index0168", "index0169"), 3) diff --git a/L3Z2.sc b/L3Z2.sc new file mode 100644 index 0000000..3023b21 --- /dev/null +++ b/L3Z2.sc @@ -0,0 +1,31 @@ +def reverse[A](list: List[A]):List[A]={ + def rev(li: List[A],res:List[A]):List[A]={ + if(li==Nil)res + else rev(li.tail,li.head::res) + } + rev(list,List()) +} + +def join3Lists[A](li1: List[A], li2: List[A], li3:List[A]): List[A]={ + (li1,li2,li3) match { + case(Nil,Nil,Nil)=>Nil + case(h1::t1,_,_)=>h1::join3Lists(t1,li2,li3) + case(Nil,h2::t2,_)=>h2::join3Lists(li1,t2,li3) + case(Nil,Nil,h3::t3)=>h3::join3Lists(li1,li2,t3) + } +} + +def join3ListsTail[A](li1: List[A], li2: List[A], li3:List[A]): List[A]={ + def joinRecu3[A](list1: List[A], list2: List[A], list3: List[A], result: List[A]): List[A]={ + (list1,list2,list3) match { + case(Nil,Nil,Nil)=>reverse(result) + case(h1::t1,_,_)=>joinRecu3(t1,list2,list3,h1::result) + case(Nil,h2::t2,_)=>joinRecu3(list1,t2,list3,h2::result) + case(Nil,Nil,h3::t3)=>joinRecu3(list1,list2,t3,h3::result) + } + } + joinRecu3(li1,li2,li3,List()) +} + +join3Lists(List(5,4,3,2),List(1,0), List(9)) +join3ListsTail(List(5,4,3,2),List(1,0), List(9)) \ No newline at end of file diff --git a/L4Z1.sc b/L4Z1.sc new file mode 100644 index 0000000..db85b07 --- /dev/null +++ b/L4Z1.sc @@ -0,0 +1,18 @@ +def filterList[A](list: List[List[A]], phrase: A): List[List[A]] ={ + def listContains(li: List[A], ph: A): Boolean={ + if(li==Nil) false + else if (li.head == ph) true + else listContains(li.tail,ph) + } + if(list==Nil) Nil + else if(phrase==Nil) list + else list filter (x=>listContains(x,phrase)) +} + +filterList(List(List(1,2,3),List(2,4),List(5,6)),6) +filterList(List(List(1,2,3),List(1,2,3,4,5),List(2,4),List(5,6)),6) +filterList(List(List(1,2,3),List(1,2,3,6,7),List(2,4),List(5,6)),6) +filterList(List(List(),List(6),List(1,2,3),List(1,2,3,6,7),List(2,4),List(5,6)),6) +filterList(List(List("A","B","C"),List(),List("C","D"),List("A","B","D"),List("C"),List("C","C","C","C")),"C") +filterList(List(List(true,false),List(false,false),List(),List(true,true,true),List(false,true,false),List(true),List(false)),false) +filterList(Nil,5) diff --git a/L4Z2.sc b/L4Z2.sc new file mode 100644 index 0000000..c51aa82 --- /dev/null +++ b/L4Z2.sc @@ -0,0 +1,48 @@ +def convertToHex(num: Int):List[Int]={ + def convert(n: Int, res: List[Int]):List[Int]={ + if(n==0) res + else convert(n/16,n%16::res) + } + if(num==0) List(0) + else if(num>0) convert(num,List()) + else { + val result=convert(Math.abs(num),List()) + result.head*(-1)::result.tail + } +} + +convertToHex(31) +convertToHex(0) +convertToHex(-31) +convertToHex(555) + +///////////////////////////////////////////////////////////// + +def convertToBase(num: Int, base: Int):List[Int]={ + def convert(n: Int, b: Int, res: List[Int]):List[Int]={ + if(n==0) res + else convert(n/b,b,n%b::res) + } + if(base>1){ + if(num==0) List(0) + else if(num>0) convert(num,base,List()) + else { + val result=convert(Math.abs(num),base,List()) + result.head*(-1)::result.tail + } + } + else Nil +} + +convertToBase(31,16) +convertToBase(31,10) +convertToBase(31,8) +convertToBase(31,4) +convertToBase(31,2) +convertToBase(-31,4) +convertToBase(31,0) +convertToBase(31,-16) +convertToBase(0,20) +convertToBase(312312,14) +convertToBase(312312, 27) +convertToBase(31,1) diff --git a/L5Z1Z2.sc b/L5Z1Z2.sc new file mode 100644 index 0000000..965eccd --- /dev/null +++ b/L5Z1Z2.sc @@ -0,0 +1,106 @@ +import scala.util.Random + +//implementacja drzewa binarnego z wykładu +sealed trait BT[+A] +case object Empty extends BT[Nothing] +case class Node[+A](elem:A, left:BT[A], right:BT[A]) extends BT[A] + +//tylko po to, żeby łatwiej było zobaczyć wartosci w wygenerowanym drzewie +def preorder[A](bt:BT[A]):List[A] = bt match { + case Node(v,l,r) => v :: preorder(l) ::: preorder(r) + case Empty => Nil +} + +//przyjąłem, że generowane liczby są z zakresu od -numberRange do numberRange (numberRange>=0) +def generateTreeDepth(depth: Int, numberRange: Int):BT[Int]={ + val generator=new Random() + //funkcja generujaca liczbe z zakresu -n do n + //def generateRandomNumber(n: Int)=(-1)*n+generator.nextInt((2*n)+1) + + //funkcja generujaca liczbe z zakresu -n do n bez 0 + def generateRandomNumber(n: Int)={ + val result=1+generator.nextInt(n) + if(generator.nextInt(2)==0) result*(-1) + else result + } + //funkcja generujaca drzewo + def generateTree(d: Int): BT[Int]={ + if(d==0) Node(generateRandomNumber(numberRange),Empty,Empty) + else Node(generateRandomNumber(numberRange),generateTree(d-1),generateTree(d-1)) + } + if(depth>=0 && numberRange>0) generateTree(depth) + else Empty +} + +val tree1=generateTreeDepth(2,6) +preorder(tree1) +val tree2=generateTreeDepth(3,10) +preorder(tree2) +val tree3=generateTreeDepth(-1,10) +val tree4=generateTreeDepth(2,0) +val tree5=generateTreeDepth(2,-1) +val tree6=generateTreeDepth(-2,-1) + +def productOfTree(tree: BT[Int]): Int={ + def product(t: BT[Int]): Int={ + t match{ + case Empty=>1 + case Node(v,l,r)=>v*product(l)*product(r) + } + } + if(tree!=Empty) product(tree) + else 0 +// else throw new Exception("the tree is empty") +} + +productOfTree(tree1) + +def listContains[A](li: List[A], el: A): Boolean={ + if(li==Nil) false + else if (li.head == el) true + else listContains(li.tail,el) +} + +def breadthWithoutDuplicatesv2(t:BT[Int]):List[Int]={ + def helper(queue:List[BT[Int]],visited: List[Int]): List[Int] ={ + queue match { + case Nil=>Nil + case Empty::t=> helper(t,visited) + case Node(v,l,r)::t=>{ + if(listContains(visited,v)) { + (0)::helper(t:::(l::r::Nil),visited) + } + else v::helper(t:::(l::r::Nil),v::visited) + } + } + } + if(t!=Empty) helper(List(t),List()) + else Nil +} + +breadthWithoutDuplicatesv2(tree1) + +def depthWithoutDuplicatesv2(t: BT[Int]): List[Int]={ + def helper(bt:BT[Int],visited: List[Int]):(List[Int],List[Int])={ + bt match { + case Empty=>(Nil,visited) + case Node(v,l,r)=>{ + if(listContains(visited,v)){ + val left=helper(l,visited) + val right=helper(r,left._2) + (0::left._1:::right._1,right._2) + } + else{ + val left=helper(l,v::visited) + val right=helper(r,left._2) + (v::left._1:::right._1,right._2) + } + } + } + } + if(t!=Empty) helper(t,List())._1 + else Nil +} + +depthWithoutDuplicatesv2(tree1) + diff --git a/L5Z3.sc b/L5Z3.sc new file mode 100644 index 0000000..38e9397 --- /dev/null +++ b/L5Z3.sc @@ -0,0 +1,106 @@ +import scala.util.Random + +val generator=new Random() + +//implementacja drzewa binarnego z wykładu +sealed trait BT[+A] +case object Empty extends BT[Nothing] +case class Node[+A](elem:A, left:BT[A], right:BT[A]) extends BT[A] + +//funkcja sprawdzająca czy lista zawiera element +def listContains[A](li: List[A], el: A): Boolean={ + if(li==Nil) false + else if (li.head == el) true + else listContains(li.tail,el) +} + +//funkcja przechodząca wgłąb +def depthWithoutDuplicates[A](t: BT[A]): List[A]={ + def helper(bt:BT[A],visited: List[A]):(List[A],List[A])={ + bt match { + case Empty=>(Nil,visited) + case Node(v,l,r)=>{ + if(listContains(visited,v)){ + val left=helper(l,visited) + val right=helper(r,left._2) + (left._1:::right._1,right._2) + } + else{ + val left=helper(l,v::visited) + val right=helper(r,left._2) + (v::left._1:::right._1,right._2) + } + } + } + } + if(t!=Empty) helper(t,List())._1 + else Nil +} + +def depthWithoutDuplicatesv2(t: BT[Int]): List[Int]={ + def helper(bt:BT[Int],visited: List[Int]):(List[Int],List[Int])={ + bt match { + case Empty=>(Nil,visited) + case Node(v,l,r)=>{ + if(listContains(visited,v)){ + val left=helper(l,visited) + val right=helper(r,left._2) + (0::left._1:::right._1,right._2) + } + else{ + val left=helper(l,v::visited) + val right=helper(r,left._2) + (v::left._1:::right._1,right._2) + } + } + } + } + if(t!=Empty) helper(t,List())._1 + else Nil +} + + +def breadthWithoutDuplicates[A](t:BT[A]):List[A]={ + def helper(queue:List[BT[A]],visited: List[A]): List[A] ={ + queue match { + case Nil=>Nil + case Empty::t=> helper(t,visited) + case Node(v,l,r)::t=>{ + if(listContains(visited,v)) helper(t:::(l::r::Nil),visited) + else v::helper(t:::(l::r::Nil),v::visited) + } + } + } + if(t!=Empty) helper(List(t),List()) + else Nil +} + +def breadthWithoutDuplicatesv2(t:BT[Int]):List[Int]={ + def helper(queue:List[BT[Int]],visited: List[Int]): List[Int] ={ + queue match { + case Nil=>Nil + case Empty::t=> helper(t,visited) + case Node(v,l,r)::t=>{ + if(listContains(visited,v)) { + (0)::helper(t:::(l::r::Nil),visited) + } + else v::helper(t:::(l::r::Nil),v::visited) + } + } + } + if(t!=Empty) helper(List(t),List()) + else Nil +} + + + +val tree1=Node(1,Node(2,Node(4,Empty,Empty),Node(5,Empty,Empty)),Node(3,Node(6,Empty,Empty),Node(7,Empty,Empty))) +val tree8=Node(1,Node(1,Node(4,Node(1,Empty,Empty),Node(7,Empty,Empty)),Node(5,Empty,Empty)),Node(3,Empty,Empty)) +val tree10=Node(1,Node(1,Node(2,Empty,Empty),Node(3,Empty,Empty)),Node(2,Empty,Empty)) +depthWithoutDuplicates(tree8) +breadthWithoutDuplicates(tree8) +depthWithoutDuplicates(Empty) +breadthWithoutDuplicates(Empty) + +val treeTest=Node(1,Node(2,Node(3,Empty,Empty),Node(4,Empty,Empty)),Node(2,Node(7,Empty,Empty),Node(8,Empty,Empty))) +breadthWithoutDuplicatesv2(treeTest) \ No newline at end of file diff --git a/L6Z1.sc b/L6Z1.sc new file mode 100644 index 0000000..e672ba0 --- /dev/null +++ b/L6Z1.sc @@ -0,0 +1,30 @@ +def eachNElement[A](llist: Stream[A], n: Int, m: Int): Stream[A] = { + def eachNElementHelper(llist: Stream[A], index: Int): Stream[A] = { + llist match { + case (Stream.Empty) => Stream.Empty + case (h #:: t) => { + if (index >= m) Stream.Empty + else { + if (index % n == 0) h #:: eachNElementHelper(t, index + 1) + else eachNElementHelper(t, index + 1) + } + } + } + } + + if (n <= 0) throw new Exception("N value cannot be less or equal 0") + if (m <= 0) throw new Exception("M value cannot be less or equal 0") + eachNElementHelper(llist, 0) +} + +//TESTY: + +eachNElement(Stream(5, 6, 3, 2, 1), 2, 3).force +eachNElement(Stream(5, 6, 3, 2, 1), 2, 4).force +eachNElement(Stream(5, 6, 3, 2, 1), 2, 5).force +eachNElement(Stream(5, 6, 3, 2, 1), 2, 99).force +eachNElement(Stream(5, 6, 3, 2, 1), 1, 3).force +eachNElement(Stream(5, 6, 3, 2, 1), 2, 1).force +eachNElement(Stream(5, 6, 3, 2, 1), 0, 3).force +eachNElement(Stream(5, 6, 3, 2, 1), 2, 0).force +eachNElement(Stream(5, 6, 3, 2, 1), 0, 0).force \ No newline at end of file diff --git a/L6Z2.sc b/L6Z2.sc new file mode 100644 index 0000000..af3f372 --- /dev/null +++ b/L6Z2.sc @@ -0,0 +1,44 @@ +def add(left: Double, right: Double): Double = left + right + +def subtract(left: Double, right: Double): Double = left - right + +def multiply(left: Double, right: Double): Double = left * right + +def divide(left: Double, right: Double): Double = { + if (right != 0) left / right + else throw new ArithmeticException("Divide by zero") +} + +def loperation(lListLeft: Stream[Double], lListRight: Stream[Double], operation: String): Stream[Double] = { + def loperationHelper(lListLeft: Stream[Double], lListRight: Stream[Double], operation: (Double, Double) => Double, defaultEmptyValue: Double): Stream[Double] = { + (lListLeft, lListRight) match { + case (Stream.Empty, Stream.Empty) => Stream.Empty + case (Stream.Empty, Stream.cons(h2, t2)) => operation(defaultEmptyValue, h2) #:: loperationHelper(lListLeft, t2, operation, defaultEmptyValue) + case (Stream.cons(h1, t1), Stream.Empty) => operation(h1, defaultEmptyValue) #:: loperationHelper(t1, lListRight, operation, defaultEmptyValue) + case (Stream.cons(h1, t1), Stream.cons(h2, t2)) => operation(h1, h2) #:: loperationHelper(t1, t2, operation, defaultEmptyValue) + } + } + + operation match { + case ("+") => loperationHelper(lListLeft, lListRight, add, 0) + case ("-") => loperationHelper(lListLeft, lListRight, subtract, 0) + case ("*") => loperationHelper(lListLeft, lListRight, multiply, 1) + case ("/") => loperationHelper(lListLeft, lListRight, divide, 1) + case (_) => throw new Exception("Operation unknown") + } +} + +//TESTY: + +loperation(Stream(1, 2, 3), Stream(2, 3, 4, 5), "+").force +loperation(Stream(1, 2, 3), Stream(2, 3, 4, 5), "-").force +loperation(Stream(1, 2, 3), Stream(2, 3, 4, 5), "*").force +loperation(Stream(1, 2, 3), Stream(2, 3, 4, 5), "/").force +loperation(Stream(2, 3, 4, 5), Stream(1, 2, 3), "+").force +loperation(Stream(2, 3, 4, 5), Stream(1, 2, 3), "-").force +loperation(Stream(2, 3, 4, 5), Stream(1, 2, 3), "*").force +loperation(Stream(2, 3, 4, 5), Stream(1, 2, 3), "/").force +loperation(Stream(2, 0, 4, 5), Stream(1, 2, 3, 4), "/").force +loperation(Stream(2, 3, 4, 5), Stream(1, 0, 3, 4), "/").take(1).force +loperation(Stream(2, 3, 4, 5), Stream(1, 0, 3, 4), "/").force +loperation(Stream(2, 3, 4, 5), Stream(1, 2, 3, 4), "^").force diff --git a/pdgm-mm.iml b/pdgm-mm.iml new file mode 100644 index 0000000..25bc755 --- /dev/null +++ b/pdgm-mm.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file