vavc~>zg__>"
48 | );
49 | }
50 |
51 | protected void test(Paragraph paragraph, final String expected) {
52 | test(paragraph::toMarkdown, expected, MARKDOWN);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/Md2Html/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /workspace.xml
--------------------------------------------------------------------------------
/Md2Html/.idea/codeStyles/codeStyleConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Md2Html/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Md2Html/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Md2Html/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Md2Html/Md2Html.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Md2Html/kekTest.in:
--------------------------------------------------------------------------------
1 | Знаете ли вы, что в Markdown, одиночные * и
2 | не означают выделение?
3 | О\[н\] \[\] {} \(\)\(\)\{\]\[ [т_а_к](z_a_z) же могут быть заэкранированы
4 | при помощи обра\\тного слэша: \*.
5 |
--------------------------------------------------------------------------------
/Md2Html/out.txt:
--------------------------------------------------------------------------------
1 | Знаете ли вы, что в Markdown, одиночные * и
2 | не означают выделение?
3 | О[н] [] {} ()(){][ так же могут быть заэкранированы
4 | при помощи обра\тного слэша: *.
5 |
--------------------------------------------------------------------------------
/Md2Html/out/production/Md2Html/META-INF/Md2Html.kotlin_module:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Md2Html/out/production/Md2Html/Md2HtmlLinkTest.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pprettysimpple/ITMOJava/78fd386b97d37628820a49b96588f1b2d4266462/Md2Html/out/production/Md2Html/Md2HtmlLinkTest.jar
--------------------------------------------------------------------------------
/Md2Html/out/production/Md2Html/Md2HtmlTest.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pprettysimpple/ITMOJava/78fd386b97d37628820a49b96588f1b2d4266462/Md2Html/out/production/Md2Html/Md2HtmlTest.jar
--------------------------------------------------------------------------------
/Md2Html/out/production/Md2Html/Md2HtmlUnderlineTest.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/pprettysimpple/ITMOJava/78fd386b97d37628820a49b96588f1b2d4266462/Md2Html/out/production/Md2Html/Md2HtmlUnderlineTest.jar
--------------------------------------------------------------------------------
/Md2Html/src/base/Asserts.java:
--------------------------------------------------------------------------------
1 | package base;
2 |
3 | import java.util.List;
4 | import java.util.Locale;
5 | import java.util.Objects;
6 |
7 | /**
8 | * @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
9 | */
10 | public class Asserts {
11 | static {
12 | Locale.setDefault(Locale.US);
13 | }
14 |
15 | public static void assertEquals(final String message, final Object expected, final Object actual) {
16 | assertTrue(String.format("%s:%n expected `%s`,%n actual `%s`", message, expected, actual), Objects.equals(expected, actual));
17 | }
18 |
19 | public static void assertEquals(final String message, final List expected, final List actual) {
20 | for (int i = 0; i < Math.min(expected.size(), actual.size()); i++) {
21 | assertEquals(message + ":" + (i + 1), expected.get(i), actual.get(i));
22 | }
23 | assertEquals(message + ": Number of items", expected.size(), actual.size());
24 | }
25 |
26 | public static void assertTrue(final String message, final boolean value) {
27 | if (!value) {
28 | throw new AssertionError(message);
29 | }
30 | }
31 |
32 | public static void assertEquals(final String message, final double expected, final double actual, final double precision) {
33 | final double error = Math.abs(expected - actual);
34 | assertTrue(
35 | String.format("%s: expected %f, found %f", message, expected, actual),
36 | error <= precision || (Math.abs(expected) >= 1 && error / Math.abs(expected) < precision)
37 | );
38 | }
39 |
40 | public static void assertSame(final String message, final Object expected, final Object actual) {
41 | assertTrue(String.format("%s: expected same objects: %s and %s", message, expected, actual), expected == actual);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Md2Html/src/base/MainFilesChecker.java:
--------------------------------------------------------------------------------
1 | package base;
2 |
3 | import java.io.IOException;
4 | import java.nio.file.Files;
5 | import java.nio.file.Path;
6 | import java.nio.file.Paths;
7 | import java.util.List;
8 |
9 | /**
10 | * @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
11 | */
12 | public class MainFilesChecker extends MainChecker {
13 | public MainFilesChecker(final String className) {
14 | super(className);
15 | }
16 |
17 | private Path getFile(final String suffix) {
18 | return Paths.get(String.format("test%d.%s", counter.getTest() + 1, suffix));
19 | }
20 |
21 | protected List runFiles(final List input) {
22 | try {
23 | final Path inf = getFile("in");
24 | final Path ouf = getFile("out");
25 | Files.write(inf, input);
26 | run(inf.toString(), ouf.toString());
27 | final List output = Files.readAllLines(ouf);
28 | Files.delete(inf);
29 | Files.delete(ouf);
30 | return output;
31 | } catch (IOException e) {
32 | throw new AssertionError(e);
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Md2Html/src/base/MainStdChecker.java:
--------------------------------------------------------------------------------
1 | package base;
2 |
3 | import java.io.*;
4 | import java.util.List;
5 |
6 | /**
7 | * @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
8 | */
9 | public class MainStdChecker extends MainChecker {
10 |
11 | public MainStdChecker(final String className) {
12 | super(className);
13 | }
14 |
15 | protected List runStd(final List input) {
16 | final ByteArrayOutputStream baos = new ByteArrayOutputStream();
17 | try (final PrintWriter writer = new PrintWriter(baos)) {
18 | input.forEach(writer::println);
19 | }
20 |
21 | final InputStream oldIn = System.in;
22 | try {
23 | System.setIn(new ByteArrayInputStream(baos.toByteArray()));
24 | return runComment(String.format("<%d input lines>", input.size()));
25 | } finally {
26 | System.setIn(oldIn);
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Md2Html/src/base/Pair.java:
--------------------------------------------------------------------------------
1 | package base;
2 |
3 | import java.util.Map;
4 |
5 | /**
6 | * @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
7 | */
8 | public class Pair {
9 | public final F first;
10 | public final S second;
11 |
12 | public Pair(final F first, final S second) {
13 | this.first = first;
14 | this.second = second;
15 | }
16 |
17 | public static Pair of(final F first, final S second) {
18 | return new Pair<>(first, second);
19 | }
20 |
21 | public static Pair of(final Map.Entry e) {
22 | return of(e.getKey(), e.getValue());
23 | }
24 |
25 | @Override
26 | public boolean equals(final Object o) {
27 | if (this == o) return true;
28 | if (o == null || getClass() != o.getClass()) return false;
29 |
30 | final Pair, ?> pair = (Pair, ?>) o;
31 |
32 | return first.equals(pair.first) && second.equals(pair.second);
33 | }
34 |
35 | @Override
36 | public int hashCode() {
37 | int result = first.hashCode();
38 | result = 31 * result + second.hashCode();
39 | return result;
40 | }
41 |
42 | @Override
43 | public String toString() {
44 | return first + ": " + second;
45 | }
46 |
47 | public F getFirst() {
48 | return first;
49 | }
50 |
51 | public S getSecond() {
52 | return second;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/Md2Html/src/base/Randomized.java:
--------------------------------------------------------------------------------
1 | package base;
2 |
3 | import java.util.List;
4 | import java.util.Random;
5 |
6 | /**
7 | * @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
8 | */
9 | public class Randomized {
10 | public static final String ENGLISH = "abcdefghijklmnopqrstuvwxyz";
11 | public static final String RUSSIAN = "абвгдеежзийклмнопрстуфхцчшщъыьэюя";
12 | public static final String GREEK = "αβγŋδεζηθικλμνξοπρτυφχψω";
13 |
14 | public final Random random = new Random(8045702385702345702L);
15 |
16 | public String randomString(final String chars) {
17 | return randomChar(chars) + (random.nextBoolean() ? "" : randomString(chars));
18 | }
19 |
20 | public char randomChar(final String chars) {
21 | return chars.charAt(random.nextInt(chars.length()));
22 | }
23 |
24 | public String randomString(final String chars, final int length) {
25 | final StringBuilder string = new StringBuilder();
26 | for (int i = 0; i < length; i++) {
27 | string.append(randomChar(chars));
28 | }
29 | return string.toString();
30 | }
31 |
32 | public String randomString(final String chars, final int minLength, int maxLength) {
33 | return randomString(chars, randomInt(minLength, maxLength + 1));
34 | }
35 |
36 | public int randomInt(final int min, final int max) {
37 | return random.nextInt(max - min) + min;
38 | }
39 |
40 | @SafeVarargs
41 | public final T randomItem(final T... items) {
42 | return items[random.nextInt(items.length)];
43 | }
44 |
45 | public final T randomItem(final List items) {
46 | return items.get(random.nextInt(items.size()));
47 | }
48 |
49 | public Random getRandom() {
50 | return random;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/Md2Html/src/md2html/BlockParser.java:
--------------------------------------------------------------------------------
1 | package md2html;
2 |
3 | class BlockParser {
4 | private StringBuilder source;
5 |
6 | BlockParser(StringBuilder source) {
7 | this.source = source;
8 | }
9 |
10 | private boolean isHeader(StringBuilder text) {
11 | int pos = 0;
12 | while (pos < text.length() && text.charAt(pos) == '#') {
13 | pos++;
14 | }
15 | return pos > 0 && pos < text.length() && text.charAt(pos) == ' ';
16 | }
17 |
18 | public void toHtml(StringBuilder result) {
19 | if (isHeader(source)) {
20 | new HeaderParser(source).toHtml(result);
21 | } else {
22 | new ParagraphParser(source).toHtml(result);
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/Md2Html/src/md2html/HeaderParser.java:
--------------------------------------------------------------------------------
1 | package md2html;
2 |
3 | class HeaderParser {
4 | private StringBuilder source;
5 |
6 | HeaderParser(StringBuilder source) {
7 | this.source = source;
8 | }
9 |
10 | private int headerLevel(StringBuilder text) {
11 | int pos = 0;
12 | while (pos < text.length() && text.charAt(pos) == '#') {
13 | pos++;
14 | }
15 | return pos;
16 | }
17 |
18 | public void toHtml(StringBuilder result) {
19 | int lvl = headerLevel(source);
20 | result.append("");
21 | new TextParser(
22 | new StringBuilder(
23 | source.substring(lvl + 1))
24 | ).toHtml(result);
25 | result.append("");
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Md2Html/src/md2html/IntList.java:
--------------------------------------------------------------------------------
1 | package md2html;
2 |
3 | import java.util.Arrays;
4 |
5 | public class IntList {
6 | private int size = 0;
7 | private int[] arr = new int[5];
8 |
9 | public void add(int value) {
10 | if (size == arr.length) {
11 | arr = Arrays.copyOf(arr, 3 * arr.length / 2);
12 | }
13 | arr[size++] = value;
14 | }
15 |
16 | public int get(int position) {
17 | return arr[position];
18 | }
19 |
20 | public void pop() {
21 | size--;
22 | }
23 |
24 | public int getSize() {
25 | return size;
26 | }
27 | }
--------------------------------------------------------------------------------
/Md2Html/src/md2html/Md2Html.java:
--------------------------------------------------------------------------------
1 | package md2html;
2 |
3 | import java.io.*;
4 | import java.nio.charset.StandardCharsets;
5 |
6 | public class Md2Html {
7 |
8 | public static void main(String[] args) {
9 | if (args.length != 2) {
10 | System.err.println("Missed filenames\n");
11 | return;
12 | }
13 | StringBuilder result = new StringBuilder();
14 | try {
15 | try (BufferedReader reader = new BufferedReader(
16 | new InputStreamReader(
17 | new FileInputStream(
18 | new File(args[0])),
19 | StandardCharsets.UTF_8))) {
20 | String line = "";
21 | StringBuilder paragraph = new StringBuilder();
22 | while (line != null && (line = reader.readLine()) != null) {
23 | while (line != null && !line.isEmpty()) {
24 | paragraph.append(line).append('\n');
25 | line = reader.readLine();
26 | }
27 | if (paragraph.length() != 0) {
28 | paragraph.setLength(paragraph.length() - 1);
29 | new BlockParser(paragraph).toHtml(result);
30 | result.append('\n');
31 | paragraph = new StringBuilder();
32 | }
33 | }
34 | }
35 | } catch (FileNotFoundException e) {
36 | System.err.println("Input4 file not found: " + e.getMessage());
37 | } catch (IOException e) {
38 | System.err.println("Read error: " + e.getMessage());
39 | }
40 | try {
41 | try (BufferedWriter writer = new BufferedWriter(
42 | new OutputStreamWriter(
43 | new FileOutputStream(
44 | new File(args[1])),
45 | StandardCharsets.UTF_8))) {
46 | writer.write(result.toString());
47 | }
48 | } catch (FileNotFoundException e) {
49 | System.err.println("Output file not found: " + e.getMessage());
50 | } catch (IOException e) {
51 | System.err.println("Write error: " + e.getMessage());
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/Md2Html/src/md2html/ParagraphParser.java:
--------------------------------------------------------------------------------
1 | package md2html;
2 |
3 | class ParagraphParser {
4 | private StringBuilder source;
5 |
6 | public ParagraphParser(StringBuilder source) {
7 | this.source = source;
8 | }
9 |
10 | public void toHtml(StringBuilder result) {
11 | result.append("");
12 | new TextParser(source).toHtml(result);
13 | result.append("
");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/NERCHW/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /workspace.xml
--------------------------------------------------------------------------------
/NERCHW/.idea/codeStyles/codeStyleConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/NERCHW/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/NERCHW/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/NERCHW/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/NERCHW/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/NERCHW/NERCHW.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/NERCHW/src/A.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class A {
4 | public static void main(String[] args) {
5 | Scanner sc = new Scanner(System.in);
6 | int a = sc.nextInt();
7 | int b = sc.nextInt();
8 | int n = sc.nextInt();
9 | int ans = 2 * ((n - a - 1) / (b - a)) + 1;
10 | System.out.println(ans);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/NERCHW/src/B.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class B {
4 | public static void main(String[] args) {
5 | Scanner sc = new Scanner(System.in);
6 | int n = sc.nextInt();
7 | for (int i = -25000; i < n - 25000; i++) {
8 | System.out.println(710 * i);
9 | }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/NERCHW/src/H.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | import java.io.*;
3 |
4 | import java.lang.Math;
5 |
6 | public class H {
7 | private static int[] pr;
8 |
9 | private static int get(int t) {
10 | int cnt = 0;
11 | for (int i = 0; i < pr.length; i++) {
12 | int l = i, r = pr.length - 1;
13 | int before = (i == 0) ? 0 : pr[i - 1];
14 | while (l < r) {
15 | int m = r - (r - l) / 2;
16 | if (pr[m] - before > t) {
17 | r = m - 1;
18 | } else {
19 | l = m;
20 | }
21 | }
22 | i = l;
23 | cnt++;
24 | }
25 | return cnt;
26 | }
27 |
28 | public static void main(String[] args) {
29 | Scanner sc = new Scanner(System.in);
30 | pr = new int[sc.nextInt()];
31 | int mx = 0;
32 | for (int i = 0; i < pr.length; i++) {
33 | pr[i] = sc.nextInt();
34 | mx = Math.max(mx, pr[i]);
35 | pr[i] += (i == 0 ? 0 : pr[i - 1]);
36 | }
37 | int q = sc.nextInt();
38 | int[] mem = new int[1000_001];
39 | Arrays.fill(mem, -1);
40 | while (q --> 0) {
41 | int t = sc.nextInt();
42 | if (mem[t] != -1) {
43 | System.out.println(mem[t]);
44 | continue;
45 | }
46 | if (t < mx) {
47 | System.out.println("Impossible");
48 | continue;
49 | }
50 | System.out.println(mem[t] = get(t));
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/NERCHW/src/I.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 | import java.lang.Math;
3 |
4 | public class I {
5 | private static final int INF = (int) (1e9 + 7);
6 | public static void main(String[] args) {
7 | Scanner sc = new Scanner(System.in);
8 | int xl = INF;
9 | int xr = -INF;
10 | int yl = INF;
11 | int yr = -INF;
12 | int n = sc.nextInt();
13 | for (int i = 0; i < n; i++) {
14 | int x = sc.nextInt();
15 | int y = sc.nextInt();
16 | int h = sc.nextInt();
17 | xl = Math.min(xl, x - h);
18 | xr = Math.max(xr, x + h);
19 | yl = Math.min(yl, y - h);
20 | yr = Math.max(yr, y + h);
21 | }
22 | int x = (xl + xr) / 2;
23 | int y = (yl + yr) / 2;
24 | int h = (Math.max(xr - xl, yr - yl) + 1) / 2;
25 | System.out.println(x + " " + y + " " + h);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/NERCHW/src/J.java:
--------------------------------------------------------------------------------
1 | import java.util.Scanner;
2 |
3 | public class J {
4 | public static void main(String[] args) {
5 | Scanner sc = new Scanner(System.in);
6 | int n = Integer.parseInt(sc.nextLine());
7 | int[][] d = new int[n][n];
8 | for (int i = 0; i < n; i++) {
9 | String s = sc.nextLine();
10 | for (int j = 0; j < n; j++) {
11 | d[i][j] = s.charAt(j) - '0';
12 | }
13 | }
14 | for (int v = 0; v < n; v++) {
15 | for (int i = v + 1; i < n; i++) {
16 | if (d[v][i] == 0) {
17 | continue;
18 | }
19 | for (int j = i + 1; j < n; j++) {
20 | d[v][j] -= d[i][j];
21 | if (d[v][j] < 0) {
22 | d[v][j] += 10;
23 | }
24 | }
25 | }
26 | }
27 | for (int i = 0; i < n; i++) {
28 | for (int j = 0; j < n; j++) {
29 | System.out.print(d[i][j]);
30 | }
31 | System.out.println("");
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/NERCHW/src/M.java:
--------------------------------------------------------------------------------
1 | import java.util.*;
2 | import java.io.*;
3 |
4 | public class M {
5 | public static void main(String[] args) {
6 | int[] a;
7 | int n;
8 | Scanner sc = new Scanner(System.in);
9 | int testCount = sc.nextInt();
10 | while (testCount --> 0) {
11 | a = new int[n = sc.nextInt()];
12 | for (int i = 0; i < n; i++) {
13 | a[i] = sc.nextInt();
14 | }
15 | Map mp = new HashMap<>();
16 | long ans = 0;
17 | for (int i = 0; i < n; i++) {
18 | for (int j = i + 1; j < n; j++) {
19 | int cur = 2 * a[i] - a[j];
20 | if (mp.containsKey(cur)) {
21 | ans += mp.get(cur);
22 | }
23 | }
24 | if (mp.containsKey(a[i])) {
25 | mp.put(a[i], mp.get(a[i]) + 1);
26 | } else {
27 | mp.put(a[i], 1);
28 | }
29 | }
30 | System.out.println(ans);
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Queue/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /workspace.xml
--------------------------------------------------------------------------------
/Queue/.idea/codeStyles/codeStyleConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Queue/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Queue/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Queue/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Queue/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Queue/Queue.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Queue/out/production/Queue/META-INF/Queue.kotlin_module:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Queue/src/ArrayQueueTest.java:
--------------------------------------------------------------------------------
1 | import queue.ArrayQueue;
2 |
3 | public class ArrayQueueTest {
4 | public static void fill(ArrayQueue stack) {
5 | for (int i = 0; i < 10; i++) {
6 | stack.enqueue(i);
7 | }
8 | }
9 |
10 | public static void dump(ArrayQueue stack) {
11 | while (!stack.isEmpty()) {
12 | System.out.println(stack.size() + " " +
13 | stack.element() + " " + stack.dequeue());
14 | }
15 | }
16 |
17 | public static void main(String[] args) {
18 | ArrayQueue stack = new ArrayQueue();
19 | fill(stack);
20 | dump(stack);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/Queue/src/base/Asserts.java:
--------------------------------------------------------------------------------
1 | package base;
2 |
3 | import java.util.Locale;
4 | import java.util.Objects;
5 |
6 | /**
7 | * @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
8 | */
9 | public class Asserts {
10 | static {
11 | Locale.setDefault(Locale.US);
12 | }
13 |
14 | public static void assertEquals(final String message, final Object expected, final Object actual) {
15 | assertTrue(String.format("%s:%n expected `%s`,%n actual `%s`", message, expected, actual), Objects.deepEquals(expected, actual));
16 | }
17 |
18 | public static void assertTrue(final String message, final boolean value) {
19 | if (!value) {
20 | throw new AssertionError(message);
21 | }
22 | }
23 |
24 | public static void assertEquals(final String message, final int expected, final int actual) {
25 | assertTrue(String.format("%s: Expected %d, found %d", message, expected, actual), actual == expected);
26 | }
27 |
28 | public static void assertEquals(final String message, final double precision, final double expected, final double actual) {
29 | assertTrue(
30 | String.format("%s: Expected %.12f, found %.12f", message, expected, actual),
31 | Math.abs(actual - expected) < precision ||
32 | Math.abs(actual - expected) < precision * Math.abs(actual) ||
33 | (Double.isNaN(actual) || Double.isInfinite(actual)) &&
34 | (Double.isNaN(expected) || Double.isInfinite(expected))
35 | );
36 | }
37 |
38 | public static void checkAssert(final Class> c) {
39 | if (!c.desiredAssertionStatus()) {
40 | throw error("You should enable assertions by running 'java -ea %s'", c.getName());
41 | }
42 | }
43 |
44 | public static AssertionError error(final String format, final Object... args) {
45 | return new AssertionError(String.format(format, args));
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/Queue/src/base/MainFilesChecker.java:
--------------------------------------------------------------------------------
1 | package base;
2 |
3 | import java.io.IOException;
4 | import java.nio.file.Files;
5 | import java.nio.file.Path;
6 | import java.nio.file.Paths;
7 | import java.util.List;
8 |
9 | /**
10 | * @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
11 | */
12 | public class MainFilesChecker extends MainChecker {
13 | public MainFilesChecker(final String className) {
14 | super(className);
15 | }
16 |
17 | private Path getFile(final String suffix) {
18 | return Paths.get(String.format("test%d.%s", counter.getTest() + 1, suffix));
19 | }
20 |
21 | protected List runFiles(final List input) {
22 | try {
23 | final Path inf = getFile("in");
24 | final Path ouf = getFile("out");
25 | Files.write(inf, input);
26 | run(inf.toString(), ouf.toString());
27 | final List output = Files.readAllLines(ouf);
28 | Files.delete(inf);
29 | Files.delete(ouf);
30 | return output;
31 | } catch (final IOException e) {
32 | throw new AssertionError(e);
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Queue/src/base/Randomized.java:
--------------------------------------------------------------------------------
1 | package base;
2 |
3 | import java.util.List;
4 | import java.util.Random;
5 |
6 | /**
7 | * @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
8 | */
9 | public class Randomized {
10 | public static final String ENGLISH = "abcdefghijklmnopqrstuvwxyz";
11 |
12 | public final Random random = new Random(8045702385702345702L);
13 |
14 | public String randomString(final String chars) {
15 | return randomChar(chars) + (random.nextBoolean() ? "" : randomString(chars));
16 | }
17 |
18 | public char randomChar(final String chars) {
19 | return chars.charAt(random.nextInt(chars.length()));
20 | }
21 |
22 | public String randomString(final String chars, final int length) {
23 | final StringBuilder string = new StringBuilder();
24 | for (int i = 0; i < length; i++) {
25 | string.append(randomChar(chars));
26 | }
27 | return string.toString();
28 | }
29 |
30 | public String randomString(final String chars, final int minLength, int maxLength) {
31 | return randomString(chars, randomInt(minLength, maxLength + 1));
32 | }
33 |
34 | public int randomInt(final int min, final int max) {
35 | return random.nextInt(max - min) + min;
36 | }
37 |
38 | @SafeVarargs
39 | public final T randomItem(final T... items) {
40 | return items[random.nextInt(items.length)];
41 | }
42 |
43 | public final T randomItem(final List items) {
44 | return items.get(random.nextInt(items.size()));
45 | }
46 |
47 | public Random getRandom() {
48 | return random;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Queue/src/base/Triple.java:
--------------------------------------------------------------------------------
1 | package base;
2 |
3 | /**
4 | * @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
5 | */
6 | public class Triple {
7 | private final F first;
8 | private final S second;
9 | private final T third;
10 |
11 | public Triple(final F first, final S second, final T third) {
12 | this.first = first;
13 | this.second = second;
14 | this.third = third;
15 | }
16 |
17 | public F first() {
18 | return first;
19 | }
20 |
21 | public S second() {
22 | return second;
23 | }
24 |
25 | public T third() {
26 | return third;
27 | }
28 |
29 | public static Triple of(final F first, final S second, final T third) {
30 | return new Triple<>(first, second, third);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Queue/src/queue/ArrayQueueADTTest.java:
--------------------------------------------------------------------------------
1 | package queue;
2 |
3 | import queue.ArrayQueueADT;
4 |
5 | public class ArrayQueueADTTest {
6 | public static void fill(ArrayQueueADT stack) {
7 | for (int i = 0; i < 10; i++) {
8 | ArrayQueueADT.enqueue(stack, i);
9 | }
10 | }
11 |
12 | public static void dump(ArrayQueueADT stack) {
13 | while (!ArrayQueueADT.isEmpty(stack)) {
14 | System.out.println(ArrayQueueADT.size(stack) + " " +
15 | ArrayQueueADT.element(stack) + " " + ArrayQueueADT.dequeue(stack));
16 | }
17 | }
18 |
19 | public static void main(String[] args) {
20 | ArrayQueueADT stack = new ArrayQueueADT();
21 | fill(stack);
22 | dump(stack);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/Queue/src/queue/ArrayQueueDequeTest.java:
--------------------------------------------------------------------------------
1 | package queue;
2 |
3 | import java.util.function.Function;
4 | import java.util.stream.Stream;
5 |
6 | /**
7 | * @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
8 | */
9 | public class ArrayQueueDequeTest extends ArrayQueueTest {
10 | public ArrayQueueDequeTest(final Class type, final Function, T> reference) {
11 | super(type, reference);
12 | }
13 |
14 | public static void main(final String[] args) {
15 | new ArrayQueueDequeTest<>(QueueDeque.class, ReferenceQueueDeque::new).test();
16 | }
17 |
18 | @Override
19 | protected void add(final T queue, final Object element) {
20 | if (random.nextBoolean()) {
21 | super.add(queue, element);
22 | } else {
23 | queue.push(element);
24 | }
25 | }
26 |
27 | @Override
28 | protected void check(final T queue) {
29 | if (random.nextBoolean()) {
30 | super.check(queue);
31 | } else {
32 | queue.peek();
33 | }
34 | }
35 |
36 | @Override
37 | protected void remove(final T queue) {
38 | if (random.nextBoolean()) {
39 | super.remove(queue);
40 | } else {
41 | queue.remove();
42 | }
43 | }
44 |
45 | protected interface QueueDeque extends Queue {
46 | void push(final Object element);
47 | Object peek();
48 | Object remove();
49 | }
50 |
51 | protected static class ReferenceQueueDeque extends ReferenceQueue implements QueueDeque {
52 | public ReferenceQueueDeque(final Stream