-
Notifications
You must be signed in to change notification settings - Fork 2
/
8ALib.java
134 lines (116 loc) · 3.17 KB
/
8ALib.java
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.time.*;
import java.text.*
int square(int n) {
return n*n;
}
int sign(int n) {
if(n > 0) { return 1; }
if(n < 0) { return -1; }
return 0;
}
int roundTo(int n, int toRoundTo) {
final int magRoundTo = Math.abs(toRoundTo);
final int rem = Math.abs(n) % magRoundTo;
final int multiple = Math.abs(n)/ magRoundTo;
final int sign = n>=0?1:-1;
if ( rem > (magRoundTo/2) ) {
return (magRoundTo*(multiple+1)*sign);
}
else {
return (magRoundTo*multiple*sign);
}
}
int length(String s) {
return s.length();
}
String substring(String base, int beginIndex, int endIndex) {
return base.substring(beginIndex, endIndex);
}
String concat(String left, String right) {
return left + right;
}
int indexOf(String base, String toFind) {
return base.indexOf(toFind);
}
int indexOfNth(String base, String toFind, int n) {
final int lenToFind = length(toFind);
int baseIndex = 0;
String baseSuffix = base;
for (int toFindCounter=0; toFindCounter<=n; toFindCounter++) {
int currIndex= indexOf(baseSuffix,toFind);
if (currIndex == -1) {
return -1;
}
else {
baseIndex += currIndex;
baseSuffix = substring(baseSuffix,currIndex+lenToFind,length(baseSuffix));
}
}
return baseIndex + n * lenToFind;
}
int parseInt(String s) {
return Integer.parseInt(s);
}
String readLine(String path, int index) throws java.io.IOException {
final Path filePath = Paths.get(path);
final List<String> contents = Files.readAllLines(filePath);
return contents.get(index);
}
int count(String base, String toFind) {
if (toFind.isEmpty()) {
throw new java.lang.IllegalArgumentException("toFind argument of count method must be nonempty string");
}
int count = 0;
String suffix = base;
while (!suffix.isEmpty()) {
int indexToFind = indexOf(suffix,toFind);
if(indexToFind == -1) {
return count;
}
else {
count += 1;
suffix = substring(suffix, indexToFind+1, length(suffix));
}
}
return count;
}
long dateMonthDayYearTime(String date) {
try {
final DateFormat d = new SimpleDateFormat("MMMMM dd yyyy hh:mm z");
return (long)d.parse(date).getTime();
} catch(java.text.ParseException e) {
throw new IllegalArgumentException(e.toString());
}
}
long dateYearMonthDayTime(String date) {
try {
final DateFormat d = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
return (long)d.parse(date).getTime();
} catch(java.text.ParseException e) {
throw new IllegalArgumentException(e.toString());
}
}
boolean show(String s, int numLines) {
final String[] lines = s.split("\n");
for(int i = 0; i < numLines; i += 1) {
if(numLines >= lines.length) { return false; }
System.out.println(lines[i]);
}
return true;
}
String readFile(String path) throws java.io.IOException {
final Path filePath = Paths.get(path);
final List<String> contents = Files.readAllLines(filePath);
StringBuilder result = new StringBuilder();
for(String s: contents) { result.append(s + "\n"); }
return result.toString();
}
String intToString(int n) {
return Integer.toString(n);
}
String trim(String s) {
return s.trim();
}
boolean stringEquals(String s1, String s2) {
return s1.equals(s2);
}