From 64a09d6584503fae4e03275a939ff714f31d9dd1 Mon Sep 17 00:00:00 2001 From: NikitaLipatov Date: Fri, 18 Nov 2022 23:34:48 +0300 Subject: [PATCH] 05 & 06 exercises (#114) Co-authored-by: Aditya Bhargava --- .../kotlin/01_price_of_groceries.kt | 3 +- 05_hash_tables/kotlin/02_check_voter.kt | 2 +- .../kotlin/01_breadth-first_search.kt | 29 +++++++++++ .../kotlin/BreadthFirstSearch.kt | 50 ------------------- 4 files changed, 32 insertions(+), 52 deletions(-) create mode 100644 06_breadth-first_search/kotlin/01_breadth-first_search.kt delete mode 100644 06_breadth-first_search/kotlin/BreadthFirstSearch.kt diff --git a/05_hash_tables/kotlin/01_price_of_groceries.kt b/05_hash_tables/kotlin/01_price_of_groceries.kt index 3de68db..e38091f 100644 --- a/05_hash_tables/kotlin/01_price_of_groceries.kt +++ b/05_hash_tables/kotlin/01_price_of_groceries.kt @@ -1,3 +1,4 @@ + fun main(args: Array) { val book = hashMapOf( @@ -7,4 +8,4 @@ fun main(args: Array) { ) println(book) -} +} \ No newline at end of file diff --git a/05_hash_tables/kotlin/02_check_voter.kt b/05_hash_tables/kotlin/02_check_voter.kt index 97acaee..09c9ec4 100644 --- a/05_hash_tables/kotlin/02_check_voter.kt +++ b/05_hash_tables/kotlin/02_check_voter.kt @@ -16,4 +16,4 @@ fun main(args: Array) { checkVoter("tom") checkVoter("mike") checkVoter("mike") -} +} \ No newline at end of file diff --git a/06_breadth-first_search/kotlin/01_breadth-first_search.kt b/06_breadth-first_search/kotlin/01_breadth-first_search.kt new file mode 100644 index 0000000..e8ffbfb --- /dev/null +++ b/06_breadth-first_search/kotlin/01_breadth-first_search.kt @@ -0,0 +1,29 @@ +import java.util.* + +val graph = hashMapOf( + "You" to listOf("Sergey", "Viktoria"), + "Viktoria" to listOf("Sergey", "Vladimir"), + "Vladimir" to listOf("Sergey", "Andrew", "Nikita", "Boris") +) + +private fun breadthFirstSearch(name: String) { + val queue = ArrayDeque(graph[name]) + val searched = arrayListOf() + while (queue.isNotEmpty()) { + val person = queue.poll() + if (!searched.contains(person)) { + if (personIsSeller(person)) { + println("$person is a mango seller!") + return + } else { + graph[person]?.let { queue.addAll(it) } + searched.add(person) + } + } + } + println("No mango sellers found!") +} + +private fun personIsSeller(name: String): Boolean = name.endsWith("s") + +fun main(args: Array) = println(breadthFirstSearch("You")) \ No newline at end of file diff --git a/06_breadth-first_search/kotlin/BreadthFirstSearch.kt b/06_breadth-first_search/kotlin/BreadthFirstSearch.kt deleted file mode 100644 index be7ea6f..0000000 --- a/06_breadth-first_search/kotlin/BreadthFirstSearch.kt +++ /dev/null @@ -1,50 +0,0 @@ -typealias Graph = MutableMap> - -fun Graph.breadthFirstSearch(key: V, isSearched: (V) -> Boolean): Boolean { - val queue: Deque = LinkedList() - this[key]?.let { queue += it } - val searched = HashSet() - while (queue.isNotEmpty()) { - val value = queue.pop() - if (!searched.contains(value)) - if (isSearched(value)) { - println("value $value is here!") - return true - } else { - this[value]?.let { queue += it } - searched.add(value) - } - } - return false -} - -data class Person( - val name: String, - val isSellerMango: Boolean = false -) { - override fun equals(other: Any?): Boolean = - if (other is Person) other.name == name - else false - - - override fun hashCode(): Int { - return name.length - } -} - -fun main(args: Array) { - - val graph: Graph = HashMap() - - (graph as java.util.HashMap>).apply { - put(Person("John"), listOf(Person("Sergey"), Person("Viktoria"))) - put(Person("Viktoria"), listOf(Person("Sergey"), Person("Phara"))) - put(Person("Phara"), listOf(Person("Sergey"), Person("Thrall"), Person("Xul"), Person("Juncart", true))) - } - - println( - graph.breadthFirstSearch(Person("John"), Person::isSellerMango) - ) - - -} \ No newline at end of file