Upload files to "java/reverse"
This commit is contained in:
126
java/reverse/FastReverseTest.java
Normal file
126
java/reverse/FastReverseTest.java
Normal file
@@ -0,0 +1,126 @@
|
||||
package reverse;
|
||||
|
||||
import base.ExtendedRandom;
|
||||
import base.Named;
|
||||
import base.Selector;
|
||||
import base.TestCounter;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.LongBinaryOperator;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import wordStat.WordStatTest;
|
||||
|
||||
/**
|
||||
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||
*/
|
||||
public final class FastReverseTest {
|
||||
|
||||
// === 3637
|
||||
|
||||
private static final Named<
|
||||
BiFunction<ExtendedRandom, Integer, String>
|
||||
> OCT = Named.of("", (r, i) ->
|
||||
r.nextBoolean()
|
||||
? Integer.toString(i)
|
||||
: Integer.toOctalString(i) + (r.nextBoolean() ? "o" : "O")
|
||||
);
|
||||
private static final Named<
|
||||
BiFunction<ExtendedRandom, Integer, String>
|
||||
> DEC = Named.of("", (r, i) -> Integer.toString(i));
|
||||
|
||||
private static final Named<String> PUNCT = Named.of(
|
||||
"",
|
||||
IntStream.range(0, Character.MAX_VALUE)
|
||||
.filter(
|
||||
ch ->
|
||||
ch == ' ' ||
|
||||
Character.getType(ch) == Character.START_PUNCTUATION ||
|
||||
Character.getType(ch) == Character.END_PUNCTUATION
|
||||
)
|
||||
.filter(ch -> ch != 13 && ch != 10)
|
||||
.mapToObj(Character::toString)
|
||||
.collect(Collectors.joining())
|
||||
);
|
||||
|
||||
public static final Named<ReverseTester.Op> MIN_C = Named.of(
|
||||
"MinC",
|
||||
scan2((a, b) -> b)
|
||||
);
|
||||
public static final Named<ReverseTester.Op> MIN = Named.of(
|
||||
"Min",
|
||||
scan2(Math::min)
|
||||
);
|
||||
|
||||
private static ReverseTester.Op scan2(final LongBinaryOperator reduce) {
|
||||
return ints -> {
|
||||
// This code is intentionally obscure
|
||||
final int length = Arrays.stream(ints)
|
||||
.mapToInt(r -> r.length)
|
||||
.max()
|
||||
.orElse(0);
|
||||
final long[] cs = new long[length];
|
||||
final long[] cc = new long[length + 1];
|
||||
Arrays.fill(cs, Integer.MAX_VALUE);
|
||||
Arrays.fill(cc, Integer.MAX_VALUE);
|
||||
//noinspection NestedAssignment
|
||||
final long[][] rows = range(ints.length)
|
||||
.mapToObj(i -> {
|
||||
range(ints[i].length).forEachOrdered(j ->
|
||||
cc[j] = reduce.applyAsLong(
|
||||
cc[j + 1],
|
||||
cs[j] = Math.min(cs[j], ints[i][j])
|
||||
)
|
||||
);
|
||||
return Arrays.copyOf(cc, ints[i].length);
|
||||
})
|
||||
.toArray(long[][]::new);
|
||||
return range(ints.length)
|
||||
.mapToObj(i -> rows[i])
|
||||
.toArray(long[][]::new);
|
||||
};
|
||||
}
|
||||
|
||||
private static IntStream range(final int length) {
|
||||
return IntStream.iterate(length - 1, i -> i >= 0, i -> i - 1);
|
||||
}
|
||||
|
||||
// === Common
|
||||
|
||||
public static final int MAX_SIZE =
|
||||
1_000_000 / TestCounter.DENOMINATOR / TestCounter.DENOMINATOR;
|
||||
|
||||
public static final Selector SELECTOR = new Selector(FastReverseTest.class)
|
||||
.variant("Base", ReverseTester.variant(MAX_SIZE, ReverseTest.REVERSE))
|
||||
.variant(
|
||||
"3637",
|
||||
ReverseTester.variant(MAX_SIZE, "", MIN_C, OCT, DEC, PUNCT)
|
||||
)
|
||||
.variant(
|
||||
"3839",
|
||||
ReverseTester.variant(MAX_SIZE, "", MIN, OCT, DEC, PUNCT)
|
||||
)
|
||||
.variant(
|
||||
"3435",
|
||||
ReverseTester.variant(MAX_SIZE, ReverseTest.ROTATE, PUNCT)
|
||||
)
|
||||
.variant(
|
||||
"3233",
|
||||
ReverseTester.variant(MAX_SIZE, ReverseTest.EVEN, PUNCT)
|
||||
)
|
||||
.variant(
|
||||
"4142",
|
||||
ReverseTester.variant(MAX_SIZE, ReverseTest.AVG, PUNCT)
|
||||
)
|
||||
.variant(
|
||||
"4749",
|
||||
ReverseTester.variant(MAX_SIZE, ReverseTest.SUM, PUNCT)
|
||||
);
|
||||
|
||||
private FastReverseTest() {}
|
||||
|
||||
public static void main(final String... args) {
|
||||
SELECTOR.main(args);
|
||||
WordStatTest.main(args);
|
||||
}
|
||||
}
|
||||
71
java/reverse/FastScanner.java
Normal file
71
java/reverse/FastScanner.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package reverse;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* @author Nikita Doschennikov (me@fymio.us)
|
||||
*/
|
||||
public class FastScanner {
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
String line = null;
|
||||
int pos = 0;
|
||||
|
||||
boolean hasNextLine() throws IOException {
|
||||
if (line != null && pos < line.length()) return true;
|
||||
line = br.readLine();
|
||||
pos = 0;
|
||||
return line != null;
|
||||
}
|
||||
|
||||
boolean hasNextInt() {
|
||||
if (line == null) return false;
|
||||
while (
|
||||
pos < line.length() &&
|
||||
(Character.isWhitespace(line.charAt(pos)) ||
|
||||
Character.getType(line.charAt(pos)) ==
|
||||
Character.START_PUNCTUATION ||
|
||||
Character.getType(line.charAt(pos)) ==
|
||||
Character.END_PUNCTUATION)
|
||||
) {
|
||||
pos++;
|
||||
}
|
||||
return (
|
||||
pos < line.length() &&
|
||||
(Character.isDigit(line.charAt(pos)) || line.charAt(pos) == '-')
|
||||
);
|
||||
}
|
||||
|
||||
int nextInt() {
|
||||
while (
|
||||
pos < line.length() &&
|
||||
(Character.isWhitespace(line.charAt(pos)) ||
|
||||
Character.getType(line.charAt(pos)) ==
|
||||
Character.START_PUNCTUATION ||
|
||||
Character.getType(line.charAt(pos)) ==
|
||||
Character.END_PUNCTUATION)
|
||||
) {
|
||||
pos++;
|
||||
}
|
||||
|
||||
int start = pos;
|
||||
boolean negative = line.charAt(pos) == '-';
|
||||
if (negative) pos++;
|
||||
|
||||
while (pos < line.length() && Character.isDigit(line.charAt(pos))) {
|
||||
pos++;
|
||||
}
|
||||
|
||||
int result = 0;
|
||||
for (int i = negative ? start + 1 : start; i < pos; i++) {
|
||||
result = result * 10 + (line.charAt(i) - '0');
|
||||
}
|
||||
return negative ? -result : result;
|
||||
}
|
||||
|
||||
void nextLine() {
|
||||
pos = line.length();
|
||||
}
|
||||
}
|
||||
47
java/reverse/Reverse.java
Normal file
47
java/reverse/Reverse.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package reverse;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Nikita Doschennikov (me@fymio.us)
|
||||
*/
|
||||
public class Reverse {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
FastScanner sc = new FastScanner();
|
||||
int[][] lines = new int[8][];
|
||||
int linesCount = 0;
|
||||
|
||||
while (sc.hasNextLine()) {
|
||||
int[] line = new int[8];
|
||||
int count = 0;
|
||||
|
||||
while (sc.hasNextInt()) {
|
||||
if (count >= line.length) {
|
||||
line = Arrays.copyOf(line, line.length * 2);
|
||||
}
|
||||
line[count++] = sc.nextInt();
|
||||
}
|
||||
sc.nextLine();
|
||||
|
||||
line = Arrays.copyOf(line, count);
|
||||
|
||||
if (linesCount >= lines.length) {
|
||||
lines = Arrays.copyOf(lines, lines.length * 2);
|
||||
}
|
||||
lines[linesCount++] = line;
|
||||
}
|
||||
|
||||
PrintWriter out = new PrintWriter(System.out);
|
||||
for (int i = linesCount - 1; i >= 0; i--) {
|
||||
int[] line = lines[i];
|
||||
for (int j = line.length - 1; j >= 0; j--) {
|
||||
if (j < line.length - 1) out.print(" ");
|
||||
out.print(line[j]);
|
||||
}
|
||||
out.println();
|
||||
}
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
71
java/reverse/ReverseAvg.java
Normal file
71
java/reverse/ReverseAvg.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package reverse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Nikita Doschennikov (me@fymio.us)
|
||||
*/
|
||||
public class ReverseAvg {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
FastScanner sc = new FastScanner();
|
||||
int[][] lines = new int[8][];
|
||||
int linesCount = 0;
|
||||
|
||||
while (sc.hasNextLine()) {
|
||||
int[] line = new int[8];
|
||||
int count = 0;
|
||||
|
||||
while (sc.hasNextInt()) {
|
||||
if (count >= line.length) {
|
||||
line = Arrays.copyOf(line, line.length * 2);
|
||||
}
|
||||
line[count++] = sc.nextInt();
|
||||
}
|
||||
sc.nextLine();
|
||||
|
||||
line = Arrays.copyOf(line, count);
|
||||
|
||||
if (linesCount >= lines.length) {
|
||||
lines = Arrays.copyOf(lines, lines.length * 2);
|
||||
}
|
||||
lines[linesCount++] = line;
|
||||
}
|
||||
|
||||
printResult(linesCount, lines);
|
||||
}
|
||||
|
||||
private static void printResult(int linesCount, int[][] lines) {
|
||||
PrintWriter out = new PrintWriter(System.out);
|
||||
|
||||
for (int i = 0; i < linesCount; i++) {
|
||||
int[] line = lines[i];
|
||||
|
||||
for (int j = 0; j < line.length; j++) {
|
||||
if (j > 0) out.print(" ");
|
||||
|
||||
long sum = 0;
|
||||
long count = 0;
|
||||
|
||||
for (int m = 0; m < lines[i].length; m++) {
|
||||
sum += lines[i][m];
|
||||
count++;
|
||||
}
|
||||
|
||||
for (int k = 0; k < linesCount; k++) {
|
||||
if (k != i && lines[k].length > j) {
|
||||
sum += lines[k][j];
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
out.print(sum / count);
|
||||
}
|
||||
out.println();
|
||||
}
|
||||
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
7
java/reverse/package-info.java
Normal file
7
java/reverse/package-info.java
Normal file
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* Tests for <a href="https://www.kgeorgiy.info/courses/prog-intro/homeworks.html#reverse">Reverse</a> homework
|
||||
* of <a href="https://www.kgeorgiy.info/courses/prog-intro/">Introduction to Programming</a> course.
|
||||
*
|
||||
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
|
||||
*/
|
||||
package reverse;
|
||||
Reference in New Issue
Block a user