From 5f04a2016705f2ce9604318633478dd4053568d4 Mon Sep 17 00:00:00 2001 From: PWyrostek Date: Mon, 21 Oct 2019 19:26:14 +0200 Subject: [PATCH 1/7] Rozwiazania listy 2 --- .idea/.gitignore | 2 ++ .idea/codeStyles/codeStyleConfig.xml | 5 +++++ .idea/misc.xml | 6 ++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ L2Z1.sc | 17 +++++++++++++++++ L2Z2.sc | 9 +++++++++ L2Z3.sc | 23 +++++++++++++++++++++++ pdgm-mm.iml | 12 ++++++++++++ 9 files changed, 88 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 L2Z1.sc create mode 100644 L2Z2.sc create mode 100644 L2Z3.sc create mode 100644 pdgm-mm.iml 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/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 From 9f438928bd0f3e2ad85c76c7335c948684b8f161 Mon Sep 17 00:00:00 2001 From: PWyrostek Date: Mon, 28 Oct 2019 15:52:10 +0100 Subject: [PATCH 2/7] Dodano rozwiazania zadania 1 i 2 z listy 3 --- L3Z1.sc | 0 L3Z2.sc | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 L3Z1.sc create mode 100644 L3Z2.sc diff --git a/L3Z1.sc b/L3Z1.sc new file mode 100644 index 0000000..e69de29 diff --git a/L3Z2.sc b/L3Z2.sc new file mode 100644 index 0000000..e69de29 From 4fb935693f1293fac73ea83fca0ef8fa411b71fe Mon Sep 17 00:00:00 2001 From: PWyrostek Date: Mon, 4 Nov 2019 16:11:39 +0100 Subject: [PATCH 3/7] Dodano rozwiazania listy 3 i 4 --- L4Z1.sc | 18 ++++++++++++++++++ L4Z2.sc | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 L4Z1.sc create mode 100644 L4Z2.sc 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) From 4bda97fe475a7e9aa64e0a886c7f28f061d19ad1 Mon Sep 17 00:00:00 2001 From: PWyrostek Date: Mon, 4 Nov 2019 16:27:12 +0100 Subject: [PATCH 4/7] naprawiono pliki z rozwiazaniami listy 3 --- L3Z1.sc | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ L3Z2.sc | 31 ++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/L3Z1.sc b/L3Z1.sc index e69de29..546a4aa 100644 --- a/L3Z1.sc +++ 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 index e69de29..3023b21 100644 --- a/L3Z2.sc +++ 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 From 7d3692a518b9282c448b3723041ff293a901ed7e Mon Sep 17 00:00:00 2001 From: PWyrostek Date: Tue, 12 Nov 2019 18:43:15 +0100 Subject: [PATCH 5/7] Dodano rozwiazania listy 5 --- L5Z1Z2.sc | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ L5Z3.sc | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 L5Z1Z2.sc create mode 100644 L5Z3.sc diff --git a/L5Z1Z2.sc b/L5Z1Z2.sc new file mode 100644 index 0000000..f38660c --- /dev/null +++ b/L5Z1Z2.sc @@ -0,0 +1,58 @@ +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 generateRandomNumberWithout0(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,10) +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) + + + diff --git a/L5Z3.sc b/L5Z3.sc new file mode 100644 index 0000000..a8e8afb --- /dev/null +++ b/L5Z3.sc @@ -0,0 +1,60 @@ +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] + +//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 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 +} + + +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) \ No newline at end of file From 79221fc1db85056bb35737dc4782cbc5692129cf Mon Sep 17 00:00:00 2001 From: PWyrostek Date: Sun, 24 Nov 2019 14:59:29 +0100 Subject: [PATCH 6/7] Updated list 5 after laboratories --- L5Z1Z2.sc | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++---- L5Z3.sc | 48 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 5 deletions(-) diff --git a/L5Z1Z2.sc b/L5Z1Z2.sc index f38660c..965eccd 100644 --- a/L5Z1Z2.sc +++ b/L5Z1Z2.sc @@ -15,9 +15,10 @@ def preorder[A](bt:BT[A]):List[A] = bt match { 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) + //def generateRandomNumber(n: Int)=(-1)*n+generator.nextInt((2*n)+1) + //funkcja generujaca liczbe z zakresu -n do n bez 0 - def generateRandomNumberWithout0(n: Int)={ + def generateRandomNumber(n: Int)={ val result=1+generator.nextInt(n) if(generator.nextInt(2)==0) result*(-1) else result @@ -27,11 +28,11 @@ def generateTreeDepth(depth: Int, numberRange: 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) + if(depth>=0 && numberRange>0) generateTree(depth) else Empty } -val tree1=generateTreeDepth(2,10) +val tree1=generateTreeDepth(2,6) preorder(tree1) val tree2=generateTreeDepth(3,10) preorder(tree2) @@ -54,5 +55,52 @@ def productOfTree(tree: BT[Int]): Int={ 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 index a8e8afb..38e9397 100644 --- a/L5Z3.sc +++ b/L5Z3.sc @@ -1,5 +1,7 @@ import scala.util.Random +val generator=new Random() + //implementacja drzewa binarnego z wykładu sealed trait BT[+A] case object Empty extends BT[Nothing] @@ -35,6 +37,29 @@ def depthWithoutDuplicates[A](t: BT[A]): List[A]={ 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 { @@ -50,6 +75,24 @@ def breadthWithoutDuplicates[A](t:BT[A]):List[A]={ 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)) @@ -57,4 +100,7 @@ val tree10=Node(1,Node(1,Node(2,Empty,Empty),Node(3,Empty,Empty)),Node(2,Empty,E depthWithoutDuplicates(tree8) breadthWithoutDuplicates(tree8) depthWithoutDuplicates(Empty) -breadthWithoutDuplicates(Empty) \ No newline at end of file +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 From 7865a785a255dff415718b35f10edc1e3c6e0b19 Mon Sep 17 00:00:00 2001 From: PWyrostek Date: Sun, 24 Nov 2019 15:08:25 +0100 Subject: [PATCH 7/7] Add list 6 --- L6Z1.sc | 30 ++++++++++++++++++++++++++++++ L6Z2.sc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 L6Z1.sc create mode 100644 L6Z2.sc 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