Skip to content

Commit 355e650

Browse files
committed
collect bfs, binary-search, max-depth, permutations, qsort, and two sum code
1 parent fedd0b9 commit 355e650

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

bfs/b.scala

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import scala.collection.immutable.Queue
2+
3+
/**
4+
* https://stackoverflow.com/questions/41347337/how-to-implement-breadth-first-search-in-scala-with-fp
5+
*/
6+
object b {
7+
8+
case class Node(value: Int, children: List[Node] = List.empty[Node])
9+
10+
def main(args: Array[String]) {
11+
val root = Node(
12+
value = 3,
13+
children = List(
14+
Node(value = 4, children = List(Node(value = 5))),
15+
Node(value = 6),
16+
Node(value = 8)
17+
)
18+
)
19+
val result = bfs(root, { (node: Node) =>
20+
Queue.empty[Node] ++ node.children
21+
}).find(_.value==5)
22+
println(result)
23+
}
24+
25+
def bfs[Node](node: Node, f: Node => Queue[Node]): Stream[Node] = {
26+
def _bfs(queue: Queue[Node]): Stream[Node] = queue match {
27+
case q if q.isEmpty => Stream.Empty
28+
case _ =>
29+
val (head, rest) = queue.dequeue
30+
head #:: _bfs(rest ++ f(head))
31+
}
32+
node #:: _bfs(Queue.empty[Node] ++ f(node))
33+
}
34+
35+
}

binary-search/b.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
object b {
2+
3+
def main(args: Array[String]): Unit = {
4+
println(search(List(1, 2, 3, 4, 5), 6))
5+
}
6+
7+
def search(list: List[Int], target: Int) = {
8+
def _s(start: Int, end: Int): Int = {
9+
if(start > end) -1 else {
10+
val mid = start + (end - start)/ 2
11+
list(mid) match {
12+
case m if m == target => mid
13+
case m if m <= target => _s(mid+1, end)
14+
case m if target < m => _s(start, mid-1)
15+
}
16+
}
17+
}
18+
_s(0, list.length - 1)
19+
}
20+
21+
22+
}

max-depth/m.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//https://stackoverflow.com/questions/45678000/maximum-depth-of-binary-tree-in-scala
2+
object m {
3+
4+
sealed trait Node
5+
case object Empty extends Node
6+
case class Leaf(value: Int, left: Node = Empty, right: Node = Empty) extends Node
7+
8+
def main(args: Array[String]) {
9+
val root = Leaf(
10+
value = 3,
11+
left = Leaf(value = 5, right = Leaf(value = 6)),
12+
right = Leaf(value = 8, left = Leaf(value = 10, right = Leaf(value = 11)))
13+
)
14+
println(depth(root))
15+
}
16+
17+
def depth(node: Node): Int = node match {
18+
case Empty => 0
19+
case Leaf(value, left, right) =>
20+
Math.max(depth(left), depth(right)) + 1
21+
}
22+
23+
}

permutations/p.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object p {
2+
3+
def main(args: Array[String]): Unit = {
4+
println(permutations(List(1, 2, 3)))
5+
}
6+
7+
def permutations(ls: List[Int]): List[List[Int]] = ls match {
8+
case Nil => List(List())
9+
case _ => for {
10+
e <- ls
11+
r <- permutations(ls diff List(e))
12+
} yield e :: r
13+
}
14+
15+
16+
}

quick-sort/q.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
object q {
2+
3+
def main(args: Array[String]) {
4+
println(sort(List(9, 2, 4, 1, 3)))
5+
}
6+
7+
def sort(list: List[Int]): List[Int] = list match {
8+
case Nil => Nil
9+
case head :: tail =>
10+
val (low, hight) = tail.partition(_<head)
11+
sort(low) ::: head :: sort(hight)
12+
}
13+
14+
}

two-sum/ts.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object ts {
2+
3+
def main(args: Array[String]): Unit = {
4+
println(s(List(1, 2, 3, 4, 5), 8))
5+
6+
}
7+
8+
def s(list: List[Int], target: Int) =
9+
list.combinations(2).find(_.sum==target)
10+
11+
}

0 commit comments

Comments
 (0)