From 696a657c6f5b49d8169d748293adb77ffe9b1759 Mon Sep 17 00:00:00 2001 From: Joe Czarnecki <28707072+joeczar@users.noreply.github.com> Date: Tue, 3 Mar 2020 16:16:49 +0100 Subject: [PATCH 1/4] Create Scala Solutions I've been working through the book and I was surprised to see there wasn't a Scala Solution here. I created a mutable version as close to the original as possible and immutable recursive version. --- 06_breadth-first_search/Scala Solutions | 64 +++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 06_breadth-first_search/Scala Solutions diff --git a/06_breadth-first_search/Scala Solutions b/06_breadth-first_search/Scala Solutions new file mode 100644 index 0000000..3ed4274 --- /dev/null +++ b/06_breadth-first_search/Scala Solutions @@ -0,0 +1,64 @@ +package SearchAndSort + +import scala.collection.immutable.HashMap +import scala.collection.{immutable, mutable} + + +object BreadthFirstSearch extends App { + + val graph = HashMap( + ("you" -> Vector("alice", "bob", "claire")), + ("bob" -> Vector("anuj", "peggy")), + ("alice" -> Vector("peggy")), + ("claire" -> Vector("thom", "johnny")), + ("anuj" -> Vector()), + ("peggy" -> Vector()), + ("thom" -> Vector()), + ("jonny" -> Vector()), + ) + + def personIsSeller(name: String): Boolean = + name.endsWith("y") + + def search(name: String): Unit = { + var searchQue = mutable.Queue[String]() + searchQue ++= graph(name) + var searched = Array() + var seller = false + while (!seller) { + val person = searchQue.dequeue() + if (!searched.contains(person)){ + if (personIsSeller(person)) { + println(person + " is a mango seller!") + seller = true + } + else { + searchQue ++= graph(person) + searched + person + } + } + } + } + + println(search("you")) + + def searchFunctional(name: String): String = { + var searchQue = immutable.Queue[String]() + + @scala.annotation.tailrec + def helper(searchQue: immutable.Queue[String], searched: Vector[String] = Vector()): String = { + if (searchQue.isEmpty) return "No Mango sellers" + val person = searchQue.dequeue._1 + if (searched.contains(person)) + helper(searchQue.tail) + else if (personIsSeller(person)) { + val result: String = s"$person is a mango seller!" + result + } else + helper(searchQue.tail ++ graph(person), searched :+ person) + } + helper(searchQue ++ graph(name)) + } + + println(searchFunctional("you")) +} From a6502e0af07c0fc80872725f3dd6f79c262d03ec Mon Sep 17 00:00:00 2001 From: Joe Czarnecki <28707072+joeczar@users.noreply.github.com> Date: Tue, 3 Mar 2020 16:46:16 +0100 Subject: [PATCH 2/4] Update Scala Solutions --- 06_breadth-first_search/Scala Solutions | 33 ++++++++++++++++--------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/06_breadth-first_search/Scala Solutions b/06_breadth-first_search/Scala Solutions index 3ed4274..b2cedd5 100644 --- a/06_breadth-first_search/Scala Solutions +++ b/06_breadth-first_search/Scala Solutions @@ -18,7 +18,7 @@ object BreadthFirstSearch extends App { ) def personIsSeller(name: String): Boolean = - name.endsWith("y") + name.endsWith("z") def search(name: String): Unit = { var searchQue = mutable.Queue[String]() @@ -26,16 +26,25 @@ object BreadthFirstSearch extends App { var searched = Array() var seller = false while (!seller) { - val person = searchQue.dequeue() - if (!searched.contains(person)){ - if (personIsSeller(person)) { - println(person + " is a mango seller!") - seller = true - } - else { - searchQue ++= graph(person) - searched + person - } + if (searchQue.isEmpty) + println("No Mango Sellers") + else { + val person = searchQue.dequeue() + if (!searched.contains(person)){ + if (personIsSeller(person)) { + println(person + " is a mango seller!") + seller = true + } + else { + if (searchQue.isEmpty) { + println("No Mango Sellers") + seller = true + } + else + searchQue ++= graph(person) + searched + person + } + } } } } @@ -47,7 +56,7 @@ object BreadthFirstSearch extends App { @scala.annotation.tailrec def helper(searchQue: immutable.Queue[String], searched: Vector[String] = Vector()): String = { - if (searchQue.isEmpty) return "No Mango sellers" + if (searchQue.length > 2) return "No Mango sellers" val person = searchQue.dequeue._1 if (searched.contains(person)) helper(searchQue.tail) From 4e5867cff1b40858d8f7f46e1dd43a1c05cf40e1 Mon Sep 17 00:00:00 2001 From: Joe Czarnecki <28707072+joeczar@users.noreply.github.com> Date: Tue, 3 Mar 2020 16:54:25 +0100 Subject: [PATCH 3/4] Update Scala Solutions --- 06_breadth-first_search/Scala Solutions | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/06_breadth-first_search/Scala Solutions b/06_breadth-first_search/Scala Solutions index b2cedd5..20c4b60 100644 --- a/06_breadth-first_search/Scala Solutions +++ b/06_breadth-first_search/Scala Solutions @@ -56,7 +56,7 @@ object BreadthFirstSearch extends App { @scala.annotation.tailrec def helper(searchQue: immutable.Queue[String], searched: Vector[String] = Vector()): String = { - if (searchQue.length > 2) return "No Mango sellers" + if (searchQue.length > 1) return "No Mango sellers" val person = searchQue.dequeue._1 if (searched.contains(person)) helper(searchQue.tail) From 43fb096e64d7eba5a426743f1760ad4d775527db Mon Sep 17 00:00:00 2001 From: Joe Czarnecki <28707072+joeczar@users.noreply.github.com> Date: Fri, 6 Mar 2020 14:39:52 +0100 Subject: [PATCH 4/4] removed an extra if clause --- 06_breadth-first_search/Scala Solutions | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/06_breadth-first_search/Scala Solutions b/06_breadth-first_search/Scala Solutions index 20c4b60..674fd85 100644 --- a/06_breadth-first_search/Scala Solutions +++ b/06_breadth-first_search/Scala Solutions @@ -26,9 +26,6 @@ object BreadthFirstSearch extends App { var searched = Array() var seller = false while (!seller) { - if (searchQue.isEmpty) - println("No Mango Sellers") - else { val person = searchQue.dequeue() if (!searched.contains(person)){ if (personIsSeller(person)) { @@ -40,12 +37,13 @@ object BreadthFirstSearch extends App { println("No Mango Sellers") seller = true } - else + else { searchQue ++= graph(person) searched + person + } } } - } + } }