Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/main/java/algorithms/sprint1/SleightOfHand.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,21 @@
return val * sign;
}

String next() throws IOException {
String next(int maxLength) throws IOException {
int c;
do {
c = read();
if (c == -1) throw new EOFException("Unexpected EOF");
} while (c <= ' ');

byte[] tmp = new byte[32];
byte[] tmp = new byte[Math.min(32, maxLength)];
int n = 0;
while (c > ' ') {
if (n == maxLength) {
throw new IOException("Token length exceeds " + maxLength);
}
if (n == tmp.length) {
byte[] t2 = new byte[tmp.length * 2];
byte[] t2 = new byte[Math.min(maxLength, tmp.length * 2)];
System.arraycopy(tmp, 0, t2, 0, tmp.length);
tmp = t2;
}
Expand Down Expand Up @@ -112,14 +115,18 @@
}

public static void main(String[] args) throws Exception {
if (System.getProperty("os.name").startsWith("Windows")) {
test();
} else {
run();
try {
if (System.getProperty("os.name").startsWith("Windows")) {
test();
} else {
run();
}
} catch (IOException | IllegalArgumentException e) {
System.err.println(e.getMessage());

Check warning on line 125 in src/main/java/algorithms/sprint1/SleightOfHand.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this use of System.err by a logger.

See more on https://sonarcloud.io/project/issues?id=krotname_JavaAlgorithmsShowcase&issues=AZ-4pRa7lD4Vfl8e7JNV&open=AZ-4pRa7lD4Vfl8e7JNV&pullRequest=88
Comment thread
krotname marked this conversation as resolved.
}
}

private static void run() throws Exception {

Check failure on line 129 in src/main/java/algorithms/sprint1/SleightOfHand.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=krotname_JavaAlgorithmsShowcase&issues=AZ-4pRa7lD4Vfl8e7JNW&open=AZ-4pRa7lD4Vfl8e7JNW&pullRequest=88
FastIn in = new FastIn(System.in);
FastOut out = new FastOut(System.out);

Expand All @@ -128,14 +135,17 @@
int[] count = new int[10];

for (int r = 0; r < 4; r++) {
StringBuilder row = new StringBuilder(in.next());
StringBuilder row = new StringBuilder(in.next(4));
// На всякий случай, если токенайзер разделит строку (обычно не будет)
while (row.length() < 4) {
row.append(in.next());
row.append(in.next(4 - row.length()));
}
for (int c = 0; c < 4; c++) {
char ch = row.charAt(c);
if (ch != '.') {
if (ch < '0' || ch > '9') {
throw new IOException("Invalid grid cell: " + ch);
}
count[ch - '0']++;
}
}
Expand Down Expand Up @@ -219,7 +229,12 @@

for (int[] row : a) {
for (int v : row) {
if (v != 0) count[v]++;
if (v != 0) {
if (v < 0 || v > 9) {
throw new IllegalArgumentException("Grid values must be between 0 and 9");
}
count[v]++;
}
}
}

Expand Down
Loading