-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseutil.kts
More file actions
executable file
·77 lines (67 loc) · 1.67 KB
/
parseutil.kts
File metadata and controls
executable file
·77 lines (67 loc) · 1.67 KB
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
package parseutil
import java.io.File
import java.util.Scanner
/**
* hello
* world
*
* hi
* world
*/
fun getMultipleStringsPerBreak(args: Array<String>): List<List<String>> {
val inputScanner = Scanner(getInputFile(args))
val inputStrings = mutableListOf<List<String>>()
var innerString = mutableListOf<String>()
while (inputScanner.hasNextLine()) {
val next = inputScanner.nextLine()
if (next == "") {
inputStrings.add(innerString)
innerString = mutableListOf<String>()
} else {
innerString.add(next)
}
}
inputStrings.add(innerString)
return inputStrings
}
/**
* hello,world
* hi,world
*/
fun getMultipleStringsWithSplits(args: Array<String>, split: String = ","): List<List<String>> {
val inputScanner = Scanner(getInputFile(args))
val inputStrings = mutableListOf<List<String>>()
while (inputScanner.hasNextLine()) {
val next = inputScanner.nextLine()
inputStrings.add(next.split(split))
}
return inputStrings
}
/**
* hello world hi world
*/
fun getStringPerSplit(args: Array<String>, split: String = " "): List<String> {
return getStringPerLine(args)[0].split(split)
}
/**
* hello
* world
* hi
* world
*/
fun getStringPerLine(args: Array<String>): List<String> {
val inputScanner = Scanner(getInputFile(args))
val inputStrings = mutableListOf<String>()
while (inputScanner.hasNextLine()) {
inputStrings.add(inputScanner.nextLine())
}
return inputStrings
}
// If no input specified, will use "input". If no "input" exists, will use "easy"
fun getInputFile(args: Array<String>): File {
var input = if (args.isNotEmpty() && args[0] != "") args[0] else "input"
if (File(input).exists().not()) {
input = "easy"
}
return File(input)
}