05 & 06 exercises (#114)
Co-authored-by: Aditya Bhargava <bluemangroupie@gmail.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
|
|
||||||
val book = hashMapOf<String, Double>(
|
val book = hashMapOf<String, Double>(
|
||||||
@@ -7,4 +8,4 @@ fun main(args: Array<String>) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
println(book)
|
println(book)
|
||||||
}
|
}
|
||||||
@@ -16,4 +16,4 @@ fun main(args: Array<String>) {
|
|||||||
checkVoter("tom")
|
checkVoter("tom")
|
||||||
checkVoter("mike")
|
checkVoter("mike")
|
||||||
checkVoter("mike")
|
checkVoter("mike")
|
||||||
}
|
}
|
||||||
29
06_breadth-first_search/kotlin/01_breadth-first_search.kt
Normal file
29
06_breadth-first_search/kotlin/01_breadth-first_search.kt
Normal file
@@ -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<String>()
|
||||||
|
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<String>) = println(breadthFirstSearch("You"))
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
typealias Graph<V> = MutableMap<V, List<V>>
|
|
||||||
|
|
||||||
fun <V> Graph<V>.breadthFirstSearch(key: V, isSearched: (V) -> Boolean): Boolean {
|
|
||||||
val queue: Deque<V> = LinkedList()
|
|
||||||
this[key]?.let { queue += it }
|
|
||||||
val searched = HashSet<V>()
|
|
||||||
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<String>) {
|
|
||||||
|
|
||||||
val graph: Graph<Person> = HashMap()
|
|
||||||
|
|
||||||
(graph as java.util.HashMap<Person, List<Person>>).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)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user