-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.kt
93 lines (76 loc) · 2.83 KB
/
Search.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package search
class Search(private val list: List<String>) {
private val indexMap: MutableMap<String, List<Int>> = Index(list).map
fun run() {
while (true) {
println()
println(
"=== Menu ===\n" +
"1. Find a person\n" +
"2. Print all people\n" +
"0. Exit"
)
when (scanner.nextLine().toInt()) {
1 -> performSearch()
2 -> printAll(list)
0 -> return
else -> println("Incorrect option! Try again.")
}
}
}
private fun performSearch() {
println()
println("Select a matching strategy: ALL, ANY, NONE")
val strategy = Strategy.valueOf(scanner.nextLine())
println()
println("Enter a name or email to search all suitable people.")
val query = scanner.nextLine()
val fetchedIndexesByStrategy: Set<Int> = fetchListByStrategy(query, strategy)
if (fetchedIndexesByStrategy.isNotEmpty()) {
val message = if (fetchedIndexesByStrategy.size > 1) " persons found:"
else " person found:"
println("${fetchedIndexesByStrategy.size}" + message)
for (index in fetchedIndexesByStrategy) {
println(list[index])
}
} else
println("No matching people found.")
}
private fun fetchListByStrategy(query: String, strategy: Strategy): Set<Int> {
val words = query.split(" ").map { it.lowercase() }
var finalListOfLineIndexes = mutableSetOf<Int>()
when (strategy) {
Strategy.ALL -> {
finalListOfLineIndexes = indexMap.values.flatten().toMutableSet()
for (word in words) {
val elements = indexMap[word]?.toSet() ?: emptySet()
finalListOfLineIndexes = finalListOfLineIndexes.intersect(elements).toMutableSet()
}
}
Strategy.ANY -> {
for (word in words) {
val elements: Set<Int> = indexMap[word]?.toSet() ?: emptySet()
finalListOfLineIndexes.addAll(elements)
}
}
Strategy.NONE -> {
finalListOfLineIndexes = indexMap.values.flatten().toMutableSet()
for (word in words) {
val elements = indexMap[word]?.toSet() ?: emptySet()
finalListOfLineIndexes.removeAll(elements)
}
}
}
return finalListOfLineIndexes
}
private fun printAll(list: List<String>) {
println()
println("=== List of people ===")
for (line in list) {
println(line)
}
}
enum class Strategy {
ALL, ANY, NONE
}
}