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
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions L2Z1.sc
Original file line number Diff line number Diff line change
@@ -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))


9 changes: 9 additions & 0 deletions L2Z2.sc
Original file line number Diff line number Diff line change
@@ -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))
23 changes: 23 additions & 0 deletions L2Z3.sc
Original file line number Diff line number Diff line change
@@ -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())
63 changes: 63 additions & 0 deletions L3Z1.sc
Original file line number Diff line number Diff line change
@@ -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)
31 changes: 31 additions & 0 deletions L3Z2.sc
Original file line number Diff line number Diff line change
@@ -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))
18 changes: 18 additions & 0 deletions L4Z1.sc
Original file line number Diff line number Diff line change
@@ -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)
48 changes: 48 additions & 0 deletions L4Z2.sc
Original file line number Diff line number Diff line change
@@ -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)
106 changes: 106 additions & 0 deletions L5Z1Z2.sc
Original file line number Diff line number Diff line change
@@ -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)

Loading