1
0

JAVA all works

This commit is contained in:
Sytnyk Yehor
2025-06-01 08:56:01 +03:00
parent 1e4d20b6a8
commit 232422d9d9
89 changed files with 3597 additions and 25 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

202
semester-4/ОПJа/lb-4/task4/.gitignore vendored Normal file
View File

@ -0,0 +1,202 @@
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
# Created by https://www.gitignore.io/api/git,java,maven,eclipse,windows
### Eclipse ###
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# PyDev specific (Python IDE for Eclipse)
*.pydevproject
# CDT-specific (C/C++ Development Tooling)
.cproject
# CDT- autotools
.autotools
# Java annotation processor (APT)
.factorypath
# PDT-specific (PHP Development Tools)
.buildpath
# sbteclipse plugin
.target
# Tern plugin
.tern-project
# TeXlipse plugin
.texlipse
# STS (Spring Tool Suite)
.springBeans
# Code Recommenders
.recommenders/
# Annotation Processing
.apt_generated/
# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet
### Eclipse Patch ###
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath
# Annotation Processing
.apt_generated
.sts4-cache/
### Git ###
# Created by git for backups. To disable backups in Git:
# $ git config --global mergetool.keepBackup false
*.orig
# Created by git when using merge tools for conflicts
*.BACKUP.*
*.BASE.*
*.LOCAL.*
*.REMOTE.*
*_BACKUP_*.txt
*_BASE_*.txt
*_LOCAL_*.txt
*_REMOTE_*.txt
### Java ###
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
### Maven ###
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
### Windows ###
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Some additional ignores (sort later)
*.DS_Store
*.sw?
.#*
*#
*~
.classpath
.project
.settings
bin
build
target
dependency-reduced-pom.xml
*.sublime-*
/scratch
.gradle
README.html
*.iml
.idea
.exercism

View File

@ -0,0 +1,2 @@
set JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8
mvn clean test

View File

@ -0,0 +1,2 @@
JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8
mvn clean test

View File

@ -0,0 +1,5 @@
Сонце гріє лице,
Радує це!
Ooh
What more
can I say

View File

@ -0,0 +1,3 @@
asd asdf asd asdf
asdf 43 asdsf 43 43 434
stop

View File

@ -0,0 +1 @@
0

View File

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ua.nure.jfn</groupId>
<artifactId>task4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
<junit5.version>5.12.0</junit5.version>
<surefire.version>3.5.3</surefire.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>task4</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -0,0 +1,37 @@
package ua.nure.jfn.task4;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class Demo {
public static void main(String[] args) {
String symin;
InputStream stdin = System.in;
// Part1
symin = "asdf\n" +
"Latn\n" +
"Cyrl\n" +
"Stop\n";
System.setIn(new ByteArrayInputStream(symin.getBytes(StandardCharsets.UTF_8)));
Part1.main(args);
// ~Part1
// Part3
symin = "table en\n" +
"table uk\n" +
"apple en\n" +
"asfd asdf\n" +
"apple uk\n" +
"stop\n";
System.setIn(new ByteArrayInputStream(symin.getBytes(StandardCharsets.UTF_8)));
Part3.main(args);
// ~Part3
System.setIn(stdin);
}
}

View File

@ -0,0 +1,59 @@
package ua.nure.jfn.task4;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Part1 {
private static final String PATH = "part1.txt";
private static final Pattern LATN_PATTERN = Pattern.compile("\\b\\p{IsLatin}+\\b",
Pattern.UNICODE_CHARACTER_CLASS);
private static final Pattern CYRL_PATTERN = Pattern.compile("\\b\\p{IsCyrillic}+\\b",
Pattern.UNICODE_CHARACTER_CLASS);
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String fileContent = null;
try {
fileContent = new String(Files.readAllBytes(Paths.get(PATH)));
} catch (IOException ex) {
ex.printStackTrace();
scanner.close();
return;
}
String result;
String input;
while (scanner.hasNextLine()) {
input = scanner.nextLine();
if (input.equalsIgnoreCase("stop")) {
break;
} else if (input.equals("Latn")) {
result = extractWords(fileContent, LATN_PATTERN);
} else if (input.equals("Cyrl")) {
result = extractWords(fileContent, CYRL_PATTERN);
} else {
result = "Incorrect input";
}
System.out.printf("%s: %s\n", input, result);
}
scanner.close();
}
private static String extractWords(String text, Pattern pattern) {
String words = "";
Matcher matcher = pattern.matcher(text);
while (matcher.find())
words += " " + matcher.group();
return words.trim();
}
}

View File

@ -0,0 +1,24 @@
package ua.nure.jfn.task4;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class Part2 {
public static void main(String[] args) {
String symin = "asdf\n" +
"asdf\n" +
"fdsa\n" +
"Stop\n";
InputStream stdin = System.in;
System.setIn(new ByteArrayInputStream(symin.getBytes(StandardCharsets.UTF_8)));
WordContainer.main(args);
System.setIn(stdin);
}
}

View File

@ -0,0 +1,30 @@
package ua.nure.jfn.task4;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Scanner;
public class Part3 {
private static final String BASE_NAME = "resources";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input;
while (scanner.hasNextLine()) {
input = scanner.nextLine();
if (input.equalsIgnoreCase("stop")) {
break;
}
String[] parts = input.split(" ");
Locale locale = new Locale(parts[1]);
ResourceBundle bundle = ResourceBundle.getBundle(BASE_NAME, locale);
System.out.println(bundle.getString(parts[0]));
}
scanner.close();
}
}

View File

@ -0,0 +1,110 @@
package ua.nure.jfn.task4;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Part4 {
private static final Path INPUT_FILE = Paths.get("part4.txt");
private static final Path OUTPUT_FILE = Paths.get("part4_sorted.txt");
private static final int NUM_INTEGERS = 10;
private static final int MAX_VALUE = 50;
public static void main(String[] args) {
List<Integer> nums;
if (!Files.exists(INPUT_FILE)) {
Random random = new Random();
nums = new ArrayList<Integer>(NUM_INTEGERS);
for (int i = 0; i < NUM_INTEGERS; i++) {
nums.add(random.nextInt(MAX_VALUE + 1));
}
if (!tryWriteNums(INPUT_FILE, nums)) {
return;
}
}
nums = tryReadNums(INPUT_FILE);
if (nums == null) {
return;
}
System.out.printf("input ==> %s\n", joinInts(nums));
nums = sortDedup(nums);
if (!tryWriteNums(OUTPUT_FILE, nums)) {
return;
}
System.out.printf("output ==> %s\n", joinInts(nums));
}
private static List<Integer> sortDedup(List<Integer> nums) {
for (int i = 0; i < nums.size() - 1; i++) {
for (int j = 0; j < nums.size() - i - 1; j++) {
int a = nums.get(j);
int b = nums.get(j + 1);
if (a > b) {
nums.set(j, b);
nums.set(j + 1, a);
}
}
}
List<Integer> dedup = new ArrayList<Integer>();
for (int num : nums) {
if (dedup.indexOf(num) == -1)
dedup.add(num);
}
return dedup;
}
private static boolean tryWriteNums(Path path, List<Integer> nums) {
try {
String s = "";
for (int i = 0; i < nums.size(); i++) {
s += " " + nums.get(i);
}
Files.write(path, s.trim().getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
return false;
}
return true;
}
private static List<Integer> tryReadNums(Path path) {
List<Integer> nums = new ArrayList<Integer>(NUM_INTEGERS);
try {
String content = Files.readString(path);
for (String s : content.trim().split(" ")) {
nums.add(Integer.parseInt(s));
}
} catch (Exception e) {
return null;
}
return nums;
}
private static String joinInts(Iterable<Integer> numbers) {
String result = "";
for (int num : numbers) {
result += " " + num;
}
return result.trim();
}
}

View File

@ -0,0 +1,25 @@
package ua.nure.jfn.task4;
public class Part5 {
private static final String SEPARATOR = "~~~~~~~";
public static void main(String[] args) {
Tree<Integer> tree = new Tree<>();
System.out.println(tree.add(3));
System.out.println(tree.add(3));
System.out.println(SEPARATOR);
tree.add(new Integer[] { 1, 2, 6, 4, 7, 0, 5 });
tree.print();
System.out.println(SEPARATOR);
System.out.println(tree.remove(5));
System.out.println(tree.remove(5));
System.out.println(SEPARATOR);
tree.print();
}
}

View File

@ -0,0 +1,194 @@
package ua.nure.jfn.task4;
public class Tree<E extends Comparable<E>> {
private Node<E> root;
private int size;
public Tree() {
root = null;
size = 0;
}
public boolean add(E element) {
if (element == null) {
return false;
}
int oldSize = size;
root = addRecursive(root, element);
return size > oldSize;
}
private Node<E> addRecursive(Node<E> node, E element) {
if (node == null) {
size++;
return new Node<>(element);
}
int comparison = element.compareTo(node.data);
if (comparison < 0) {
node.left = addRecursive(node.left, element);
} else if (comparison > 0) {
node.right = addRecursive(node.right, element);
}
return node;
}
public void add(E[] elements) {
if (elements == null) {
throw new IllegalArgumentException("Array cannot be null");
}
for (E e : elements) {
if (e != null) {
add(e);
}
}
}
public boolean remove(E element) {
if (element == null) {
return false;
}
int oldSize = size;
root = removeRecursive(root, element);
return size < oldSize;
}
private Node<E> removeRecursive(Node<E> node, E element) {
if (node == null) {
return null;
}
int comparison = element.compareTo(node.data);
if (comparison < 0) {
node.left = removeRecursive(node.left, element);
} else if (comparison > 0) {
node.right = removeRecursive(node.right, element);
} else {
size--;
if (node.left == null && node.right == null) {
return null;
}
if (node.left == null) {
return node.right;
}
if (node.right == null) {
return node.left;
}
Node<E> successor = findMin(node.right);
node.data = successor.data;
node.right = removeRecursive(node.right, successor.data);
size++;
}
return node;
}
private Node<E> findMin(Node<E> node) {
while (node.left != null) {
node = node.left;
}
return node;
}
public boolean contains(E element) {
if (element == null) {
return false;
}
return containsRecursive(root, element);
}
private boolean containsRecursive(Node<E> node, E element) {
if (node == null) {
return false;
}
int comparison = element.compareTo(node.data);
if (comparison == 0) {
return true;
} else if (comparison < 0) {
return containsRecursive(node.left, element);
} else {
return containsRecursive(node.right, element);
}
}
public int size() {
return size;
}
public void clear() {
root = null;
size = 0;
}
public void print() {
if (root == null) {
return;
}
String pad = " ".repeat(root.width());
if (root.left != null) {
root.left.print(pad, "", false);
}
System.out.printf("%s%s\n", root.data, root.pointer());
if (root.right != null) {
root.right.print(pad, "", true);
}
}
private static class Node<E> {
E data;
Node<E> left;
Node<E> right;
Node(E data) {
this.data = data;
this.left = null;
this.right = null;
}
int width() {
return data.toString().length();
}
String pointer() {
if (right != null && left != null) {
return "";
} else if (right != null) {
return "";
} else if (left != null) {
return "";
} else {
return "";
}
}
public void print(String prev, String handle, boolean nested) {
String pad = " ".repeat(width());
String ptr = pointer();
if (left != null) {
left.print(prev + (nested ? "" : " ") + pad, "", false);
}
System.out.printf("%s%s%s%s\n", prev, handle, data, ptr);
if (right != null) {
right.print(prev + (!nested ? "" : " ") + pad, "", true);
}
}
}
}

View File

@ -0,0 +1,61 @@
package ua.nure.jfn.task4;
import java.util.Objects;
public class Word implements Comparable<Word> {
private String content;
private int frequency;
public Word(String content) {
this.content = content;
this.frequency = 1;
}
public String getContent() {
return content;
}
public int getFrequency() {
return frequency;
}
public void incrementFrequency() {
this.frequency++;
}
@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (this == o) {
return true;
}
Word word = (Word) o;
return content.equals(word.content);
}
@Override
public int hashCode() {
return Objects.hash(content);
}
@Override
public String toString() {
return content + " : " + frequency;
}
@Override
public int compareTo(Word other) {
int frequencyComparison = Integer.compare(this.frequency, other.frequency);
if (frequencyComparison != 0) {
return frequencyComparison;
}
return this.content.compareTo(other.content);
}
}

View File

@ -0,0 +1,56 @@
package ua.nure.jfn.task4;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordContainer {
private static Pattern WORD_PATTERN = Pattern.compile("\\S+");
private ArrayList<Word> words = new ArrayList<>();
public static void main(String[] args) {
WordContainer container = new WordContainer();
container.readAndProcessInput();
container.printWords();
}
private void readAndProcessInput() {
Scanner scanner = new Scanner(System.in);
String input;
outer: while (scanner.hasNextLine()) {
input = scanner.nextLine();
Matcher matcher = WORD_PATTERN.matcher(input);
while (matcher.find()) {
String wordContent = matcher.group();
if (wordContent.equalsIgnoreCase("stop"))
break outer;
addWord(wordContent);
}
}
scanner.close();
}
private void addWord(String content) {
Word newWord = new Word(content);
int i = words.indexOf(newWord);
if (i == -1) {
words.add(newWord);
} else {
words.get(i).incrementFrequency();
}
}
private void printWords() {
words.sort(null);
for (Word word : words)
System.out.println(word);
}
}

View File

@ -0,0 +1,2 @@
table = table
apple = apple

View File

@ -0,0 +1,2 @@
table = \u0441\u0442\u0456\u043b
apple = \u044f\u0431\u043b\u0443\u043a\u043e

View File

@ -0,0 +1,67 @@
package ua.nure.jfn.task4;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
/**
* @author Dmytro Kolesnykov
*/
public class Part1Test {
private static final InputStream STD_IN = System.in;
private static final PrintStream STD_OUT = System.out;
private static final String ENCODING = "UTF-8";
private static final String EOL_CHAR = "~";
private static final String[] EMPTY_ARGS = {};
private static final String INPUT_FILE = "part1.txt";
private static final String EOL = System.lineSeparator();
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
private final PrintStream out = new PrintStream(baos);
private static String toMultiline(String line) {
return line.replaceAll(EOL_CHAR, EOL).trim();
}
@ParameterizedTest
@CsvFileSource(resources = "part1.csv", delimiter = ';')
void part1(String data, String consoleInput, String expectedLine) throws Exception {
String expected = toMultiline(expectedLine);
String input = toMultiline(consoleInput);
byte[] buf = input.getBytes(ENCODING);
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
System.setOut(out);
System.setIn(bais);
String actual = "";
byte[] content = null;
try {
content = Utils.getContent(INPUT_FILE);
Utils.saveContent(INPUT_FILE, toMultiline(data).getBytes(ENCODING));
Part1.main(EMPTY_ARGS);
out.flush();
actual = baos.toString();
} finally {
System.setOut(STD_OUT);
System.setIn(STD_IN);
Utils.saveContent(INPUT_FILE, content);
}
assertEquals(expected.trim(), actual.trim());
}
}

View File

@ -0,0 +1,53 @@
package ua.nure.jfn.task4;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
/**
* @author Dmytro Kolesnykov
*/
public class Part2Test {
private static final InputStream STD_IN = System.in;
private static final PrintStream STD_OUT = System.out;
private static final String TEST_DATA_FILE_ENCODINE = "UTF-8";
private static final String EOL_CHAR = "~";
private static final String[] EMPTY_ARGS = {};
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
private final PrintStream out = new PrintStream(baos);
@ParameterizedTest
@CsvFileSource(resources = "part2.csv", delimiter = ';')
void part2(String inputLine, String expectedLine) throws Exception {
String input = inputLine.replace(EOL_CHAR, System.lineSeparator());
String expected = expectedLine.replace(EOL_CHAR, System.lineSeparator());
byte[] buf = input.getBytes(TEST_DATA_FILE_ENCODINE);
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
System.setOut(out);
System.setIn(bais);
try {
WordContainer.main(EMPTY_ARGS);
out.flush();
String actual = baos.toString();
assertEquals(expected.trim(), actual.trim());
} finally {
System.setOut(STD_OUT);
System.setIn(STD_IN);
}
}
}

View File

@ -0,0 +1,111 @@
package ua.nure.jfn.task4;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
/**
* @author Dmytro Kolesnykov
*/
public class Part3Test {
private static final InputStream STD_IN = System.in;
private static final PrintStream STD_OUT = System.out;
private static final String TEST_DATA_FILE_ENCODINE = "UTF-8";
private static final String EOL_CHAR = "~";
private static final String[] EMPTY_ARGS = {};
private static final String EOL = System.lineSeparator();
private static final String BASE_NAME = "resources";
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
private final PrintStream out = new PrintStream(baos);
private static String toMultiline(String line) {
return line.replaceAll(EOL_CHAR, EOL).trim();
}
private static String removeCR(String s) {
return s.replaceAll("\r?", "");
}
private static final URI classPathURI;
static {
try {
classPathURI = Part3.class.getProtectionDomain().getCodeSource().getLocation().toURI();
} catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
}
}
@ParameterizedTest
@CsvFileSource(resources = "part3.csv", delimiter = ';')
void part3(String defaultLocale, String localesString, String consoleInput, String expectedLine) throws Exception {
ResourceBundle.clearCache();
Locale.setDefault(new Locale(defaultLocale));
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Path.of(classPathURI))) {
for (Path p : stream) {
if (p.toString().endsWith(".properties")) {
Files.delete(p);
}
}
}
Path classPath = Paths.get(classPathURI);
List<String> locales = Arrays.asList(localesString.trim().split(" "));
String actual = "";
InputStream is = null;
try {
for (String locale : locales) {
String bundleName = BASE_NAME + '_' + locale + ".properties";
is = getClass().getClassLoader().getResourceAsStream(locale + ".props");
try (OutputStream out = new FileOutputStream(new File(classPath.toString(), bundleName))) {
is.transferTo(out);
is.close();
}
}
String input = toMultiline(consoleInput);
byte[] buf = input.getBytes(TEST_DATA_FILE_ENCODINE);
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
System.setOut(out);
System.setIn(bais);
Part3.main(EMPTY_ARGS);
out.flush();
actual = baos.toString();
} finally {
System.setOut(STD_OUT);
System.setIn(STD_IN);
}
assertEquals(removeCR(toMultiline(expectedLine)).trim(), removeCR(actual).trim());
}
}

View File

@ -0,0 +1,65 @@
package ua.nure.jfn.task4;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
/**
* @author Dmytro Kolesnykov
*/
public class Part4Test {
private static final InputStream STD_IN = System.in;
private static final PrintStream STD_OUT = System.out;
private static final String EOL_CHAR = "~";
private static final String[] EMPTY_ARGS = {};
private static final String INPUT_FILE = "part4.txt";
private static final String EOL = System.lineSeparator();
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
private final PrintStream out = new PrintStream(baos);
private static String toMultiline(String line) {
return line.replaceAll(EOL_CHAR, EOL).trim();
}
@AfterAll
static void tearDown() throws IOException {
Files.deleteIfExists(Path.of(INPUT_FILE));
}
@ParameterizedTest
@CsvFileSource(resources = "part4.csv", delimiter = ';')
void part4(String inputData, String expected) throws Exception {
Files.writeString(Paths.get(INPUT_FILE), inputData);
System.setOut(out);
String actual = "";
try {
Part4.main(EMPTY_ARGS);
out.flush();
actual = baos.toString();
} finally {
System.setOut(STD_OUT);
System.setIn(STD_IN);
}
assertEquals(toMultiline(expected).trim(), actual.trim());
}
}

View File

@ -0,0 +1,69 @@
package ua.nure.jfn.task4;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Method;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
/**
* @author Dmytro Kolesnykov
*/
public class Part5Test {
private static final PrintStream STD_OUT = System.out;
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
private final PrintStream out = new PrintStream(baos);
private static String removeCR(String s) {
return s.replaceAll("\r?", "");
}
@ParameterizedTest
@CsvFileSource(resources = "part5.csv", delimiter = ';', ignoreLeadingAndTrailingWhitespace = false)
void part5(String actionsData, String expectedTree) throws Exception {
String expected = expectedTree == null
? ""
: expectedTree.replaceAll("~", System.lineSeparator());
Tree<Integer> tree = new Tree<>();
for (String action : actionsData.split("\\s+")) {
String actionName = actionResolver(action.charAt(0));
Method actionMethod = Tree.class.getMethod(actionName, Comparable.class);
String actionValues = action.substring(action.indexOf('-') + 1);
for (String value : actionValues.split("-")) {
actionMethod.invoke(tree, new Object[] { Integer.parseInt(value) });
}
}
String actual = null;
try {
System.setOut(out);
tree.print();
out.flush();
actual = baos.toString();
} finally {
System.setOut(STD_OUT);
}
expected = removeCR(expected).stripTrailing();
actual = removeCR(actual.replaceAll("(?m)\\s+$", "").stripTrailing());
assertEquals(expected, actual);
}
private static String actionResolver(char actionChar) {
switch (actionChar) {
case 'a':
return "add";
case 'r':
return "remove";
}
return null;
}
}

View File

@ -0,0 +1,19 @@
package ua.nure.jfn.task4;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Utils {
public static byte[] getContent(String fileName) throws IOException {
return Files.readAllBytes(Paths.get(fileName));
}
public static void saveContent(String fileName, byte[] content) throws IOException {
if (content != null) {
Files.write(Paths.get(fileName), content);
}
}
}

View File

@ -0,0 +1,2 @@
table = table
apple = apple

View File

@ -0,0 +1,2 @@
table = \u10db\u10d0\u10d2\u10d8\u10d3\u10d0
apple = \u10d5\u10d0\u10e8\u10da\u10d8

View File

@ -0,0 +1,27 @@
for f in *-data.txt; do
sed -rne '
/^#/d
/^\s*$/d
/^~~~/ {
g
s/\n/~/g
s/$/;/
h
b
}
/^===/ {
g
s/\n/~/g
s/;~/;/g
s/^~//
p
s/^.*$//
h
b
}
H
' $f>${f/-data.txt/.csv}
done

View File

@ -0,0 +1,72 @@
#1
Is there anybody
going to listen to my story
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
asdf
Latn
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
asdf: Incorrect input
Latn: Is there anybody going to listen to my story
====================================
#2
Is there anybody going to listen
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
asdf
Latn
stop
Latn
asdf
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
asdf: Incorrect input
Latn: Is there anybody going to listen
====================================
#3
Is there listen
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
asdf
Latn
Stop
Latn
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
asdf: Incorrect input
Latn: Is there listen
====================================
#4
Incorrect input
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a
Latn
b
stop
a
Latn
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a: Incorrect input
Latn: Incorrect input
b: Incorrect input
====================================
#5
Сонце гріє лице,
Радує це!
Ooh, baby, I love you
What more
can I say
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
latn
Latn
cyrl
Cyrl
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
latn: Incorrect input
Latn: Ooh baby I love you What more can I say
cyrl: Incorrect input
Cyrl: Сонце гріє лице Радує це
====================================

View File

@ -0,0 +1,5 @@
Is there anybody ~going to listen to my story;asdf~Latn;asdf: Incorrect input~Latn: Is there anybody going to listen to my story
Is there anybody going to listen;asdf~Latn~stop~Latn~asdf;asdf: Incorrect input~Latn: Is there anybody going to listen
Is there listen;asdf~Latn~Stop~Latn;asdf: Incorrect input~Latn: Is there listen
Incorrect input;a~Latn~b~stop~a~Latn;a: Incorrect input~Latn: Incorrect input~b: Incorrect input
Сонце гріє лице,~ Радує це!~Ooh, baby, I love you~ What more ~can I say;latn~Latn~cyrl~Cyrl;latn: Incorrect input~Latn: Ooh baby I love you What more can I say~cyrl: Incorrect input~Cyrl: Сонце гріє лице Радує це
1 Is there anybody ~going to listen to my story asdf~Latn asdf: Incorrect input~Latn: Is there anybody going to listen to my story
2 Is there anybody going to listen asdf~Latn~stop~Latn~asdf asdf: Incorrect input~Latn: Is there anybody going to listen
3 Is there listen asdf~Latn~Stop~Latn asdf: Incorrect input~Latn: Is there listen
4 Incorrect input a~Latn~b~stop~a~Latn a: Incorrect input~Latn: Incorrect input~b: Incorrect input
5 Сонце гріє лице,~ Радує це!~Ooh, baby, I love you~ What more ~can I say latn~Latn~cyrl~Cyrl latn: Incorrect input~Latn: Ooh baby I love you What more can I say~cyrl: Incorrect input~Cyrl: Сонце гріє лице Радує це

View File

@ -0,0 +1,44 @@
#1
a~b
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a : 1
b : 1
====================================
#2
a b a d~b c stop b
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
c : 1
d : 1
a : 2
b : 2
====================================
#3
a b a stop d~b c stop b
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
b : 1
a : 2
====================================
#4
ab ab abd
b abd
stop
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
b : 1
ab : 2
abd : 2
====================================
#5
a a a b b stop c c
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
b : 2
a : 3
====================================

View File

@ -0,0 +1,5 @@
a~b;a : 1~b : 1
a b a d~b c stop b;c : 1~d : 1~a : 2~b : 2
a b a stop d~b c stop b;b : 1~a : 2
ab ab abd~b abd~stop;b : 1~ab : 2~abd : 2
a a a b b stop c c;b : 2~a : 3
1 a~b a : 1~b : 1
2 a b a d~b c stop b c : 1~d : 1~a : 2~b : 2
3 a b a stop d~b c stop b b : 1~a : 2
4 ab ab abd~b abd~stop b : 1~ab : 2~abd : 2
5 a a a b b stop c c b : 2~a : 3

View File

@ -0,0 +1,101 @@
#1
en
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
en uk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
table en
table ka
table uk
apple en
apple uk
stop
ka
apple
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
table
table
стіл
apple
яблуко
====================================
#2
uk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
en uk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
table en
table ka
table uk
apple en
apple uk
stop
ka
apple
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
table
стіл
стіл
apple
яблуко
====================================
#3
en
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
en uk ka
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
table en
table ka
table uk
apple en
apple uk
stop
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
table
მაგიდა
стіл
apple
яблуко
====================================
#4
ka
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
en ka uk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
table en
table ka
table uk
apple en
apple uk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
table
მაგიდა
стіл
apple
яблуко
====================================
#5
uk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
en uk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
table en
table en
apple uk
apple uk
stop
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
table
table
яблуко
яблуко
====================================

View File

@ -0,0 +1,5 @@
en;en uk;table en~table ka~table uk~apple en~apple uk~stop~ka~apple;table~table~стіл~apple~яблуко
uk;en uk;table en~table ka~table uk~apple en~apple uk~stop~ka~apple;table~стіл~стіл~apple~яблуко
en;en uk ka;table en~table ka~table uk~apple en~apple uk~stop;table~მაგიდა~стіл~apple~яблуко
ka;en ka uk;table en~table ka~table uk~apple en~apple uk;table~მაგიდა~стіл~apple~яблуко
uk;en uk;table en~table en~apple uk~apple uk~stop;table~table~яблуко~яблуко
1 en en uk table en~table ka~table uk~apple en~apple uk~stop~ka~apple table~table~стіл~apple~яблуко
2 uk en uk table en~table ka~table uk~apple en~apple uk~stop~ka~apple table~стіл~стіл~apple~яблуко
3 en en uk ka table en~table ka~table uk~apple en~apple uk~stop table~მაგიდა~стіл~apple~яблуко
4 ka en ka uk table en~table ka~table uk~apple en~apple uk table~მაგიდა~стіл~apple~яблуко
5 uk en uk table en~table en~apple uk~apple uk~stop table~table~яблуко~яблуко

View File

@ -0,0 +1,39 @@
#1
1 2 3 4 5 4 3 2 1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input ==> 1 2 3 4 5 4 3 2 1
output ==> 1 2 3 4 5
====================================
#2
4 3 1 3 4 1 3 4 1 3 4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input ==> 4 3 1 3 4 1 3 4 1 3 4
output ==> 1 3 4
====================================
#3
4 9 8 2 9 4 8 9 1 9
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input ==> 4 9 8 2 9 4 8 9 1 9
output ==> 1 2 4 8 9
====================================
#4
11 12 11 18 12 11 18 17
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input ==> 11 12 11 18 12 11 18 17
output ==> 11 12 17 18
====================================
#5
0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input ==> 0
output ==> 0
====================================

View File

@ -0,0 +1,5 @@
1 2 3 4 5 4 3 2 1;input ==> 1 2 3 4 5 4 3 2 1~output ==> 1 2 3 4 5
4 3 1 3 4 1 3 4 1 3 4;input ==> 4 3 1 3 4 1 3 4 1 3 4~output ==> 1 3 4
4 9 8 2 9 4 8 9 1 9;input ==> 4 9 8 2 9 4 8 9 1 9~output ==> 1 2 4 8 9
11 12 11 18 12 11 18 17;input ==> 11 12 11 18 12 11 18 17~output ==> 11 12 17 18
0;input ==> 0~output ==> 0
1 1 2 3 4 5 4 3 2 1 input ==> 1 2 3 4 5 4 3 2 1~output ==> 1 2 3 4 5
2 4 3 1 3 4 1 3 4 1 3 4 input ==> 4 3 1 3 4 1 3 4 1 3 4~output ==> 1 3 4
3 4 9 8 2 9 4 8 9 1 9 input ==> 4 9 8 2 9 4 8 9 1 9~output ==> 1 2 4 8 9
4 11 12 11 18 12 11 18 17 input ==> 11 12 11 18 12 11 18 17~output ==> 11 12 17 18
5 0 input ==> 0~output ==> 0

View File

@ -0,0 +1,199 @@
#1
a-3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3
====================================
#2
a-3-1-2-6-4-7-0-5
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌0
┌1┤
│ └2
3┤
│ ┌4┐
│ │ └5
└6┤
└7
====================================
#3
a-3-1-2-6-4-7-0-5 r-3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌0
┌1┤
│ └2
4┤
│ ┌5
└6┤
└7
====================================
#4
a-3-1-2-6-4-7-0-5 r-3-5
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌0
┌1┤
│ └2
4┤
└6┐
└7
====================================
#5
a-3-1-2-6-4-7-0-5 r-3-5-4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌0
┌1┤
│ └2
6┤
└7
====================================
#6
a-3-1-2-6-4-7-0-5 r-3-5-4-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌0
┌2┘
6┤
└7
====================================
#7
a-3-1-2-6-4-7-0 r-3-5-4-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌0
┌2┘
6┤
└7
====================================
#8
a-3-1-2-6-4-7-0-5 r-3-5-4-1-6-7-2-0-0 a-3-2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌2
3┘
====================================
#9
a-3-3-2-2-1-1 r-2-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3
====================================
#10
a-3-3-1-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌1
3┘
====================================
#11
a-3-1-2-6-4-7-0-5 r-3-4-5-6
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌0
┌1┤
│ └2
7┘
====================================
#12
a-3-12-116-14-7-0-5 r-3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌0
5┤
│ ┌7
└12┤
│ ┌14
└116┘
====================================
#13
a-3-1-2-6-4-7-0-5 r-3-4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌0
┌1┤
│ └2
5┤
└6┐
└7
====================================
#14
a-3-1-2-6-4-7-0-5 r-3-4-5-6-7
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌0
1┤
└2
====================================
#15
a-3-1-2-6-4-7-0-5 r-3-4-5-6-7-1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌0
2┘
====================================
#16
a-3-1-2-6-4-7-0-5 r-3-4-5-6-7-1-2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
====================================
#17
a-3-1-2-6-4-7-0-5 r-3-4-5-6-7-1-2-0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
====================================
#18
a-3-1-2-6-4-7 r-3 a-3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌1┐
│ └2┐
│ └3
4┤
└6┐
└7
====================================
#19
a-3-1-2-6-4-7 r-3 a-3 r-2 a-2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌1┐
│ │ ┌2
│ └3┘
4┤
└6┐
└7
====================================
#20
a-3-1-2-6-4-7 r-3 a-3 r-2 a-2 r-6
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
┌1┐
│ │ ┌2
│ └3┘
4┤
└7
====================================

View File

@ -0,0 +1,20 @@
a-3;3
a-3-1-2-6-4-7-0-5; ┌0~ ┌1┤~ │ └2~3┤~ │ ┌4┐~ │ │ └5~ └6┤~ └7
a-3-1-2-6-4-7-0-5 r-3; ┌0~ ┌1┤~ │ └2~4┤~ │ ┌5~ └6┤~ └7
a-3-1-2-6-4-7-0-5 r-3-5; ┌0~ ┌1┤~ │ └2~4┤~ └6┐~ └7
a-3-1-2-6-4-7-0-5 r-3-5-4; ┌0~ ┌1┤~ │ └2~6┤~ └7
a-3-1-2-6-4-7-0-5 r-3-5-4-1; ┌0~ ┌2┘~6┤~ └7
a-3-1-2-6-4-7-0 r-3-5-4-1; ┌0~ ┌2┘~6┤~ └7
a-3-1-2-6-4-7-0-5 r-3-5-4-1-6-7-2-0-0 a-3-2; ┌2~3┘
a-3-3-2-2-1-1 r-2-1;3
a-3-3-1-1; ┌1~3┘
a-3-1-2-6-4-7-0-5 r-3-4-5-6; ┌0~ ┌1┤~ │ └2~7┘
a-3-12-116-14-7-0-5 r-3; ┌0~5┤~ │ ┌7~ └12┤~ │ ┌14~ └116┘
a-3-1-2-6-4-7-0-5 r-3-4; ┌0~ ┌1┤~ │ └2~5┤~ └6┐~ └7
a-3-1-2-6-4-7-0-5 r-3-4-5-6-7; ┌0~1┤~ └2
a-3-1-2-6-4-7-0-5 r-3-4-5-6-7-1; ┌0~2┘
a-3-1-2-6-4-7-0-5 r-3-4-5-6-7-1-2;0
a-3-1-2-6-4-7-0-5 r-3-4-5-6-7-1-2-0;
a-3-1-2-6-4-7 r-3 a-3; ┌1┐~ │ └2┐~ │ └3~4┤~ └6┐~ └7
a-3-1-2-6-4-7 r-3 a-3 r-2 a-2; ┌1┐~ │ │ ┌2~ │ └3┘~4┤~ └6┐~ └7
a-3-1-2-6-4-7 r-3 a-3 r-2 a-2 r-6; ┌1┐~ │ │ ┌2~ │ └3┘~4┤~ └7
1 a-3 3
2 a-3-1-2-6-4-7-0-5 ┌0~ ┌1┤~ │ └2~3┤~ │ ┌4┐~ │ │ └5~ └6┤~ └7
3 a-3-1-2-6-4-7-0-5 r-3 ┌0~ ┌1┤~ │ └2~4┤~ │ ┌5~ └6┤~ └7
4 a-3-1-2-6-4-7-0-5 r-3-5 ┌0~ ┌1┤~ │ └2~4┤~ └6┐~ └7
5 a-3-1-2-6-4-7-0-5 r-3-5-4 ┌0~ ┌1┤~ │ └2~6┤~ └7
6 a-3-1-2-6-4-7-0-5 r-3-5-4-1 ┌0~ ┌2┘~6┤~ └7
7 a-3-1-2-6-4-7-0 r-3-5-4-1 ┌0~ ┌2┘~6┤~ └7
8 a-3-1-2-6-4-7-0-5 r-3-5-4-1-6-7-2-0-0 a-3-2 ┌2~3┘
9 a-3-3-2-2-1-1 r-2-1 3
10 a-3-3-1-1 ┌1~3┘
11 a-3-1-2-6-4-7-0-5 r-3-4-5-6 ┌0~ ┌1┤~ │ └2~7┘
12 a-3-12-116-14-7-0-5 r-3 ┌0~5┤~ │ ┌7~ └12┤~ │ ┌14~ └116┘
13 a-3-1-2-6-4-7-0-5 r-3-4 ┌0~ ┌1┤~ │ └2~5┤~ └6┐~ └7
14 a-3-1-2-6-4-7-0-5 r-3-4-5-6-7 ┌0~1┤~ └2
15 a-3-1-2-6-4-7-0-5 r-3-4-5-6-7-1 ┌0~2┘
16 a-3-1-2-6-4-7-0-5 r-3-4-5-6-7-1-2 0
17 a-3-1-2-6-4-7-0-5 r-3-4-5-6-7-1-2-0
18 a-3-1-2-6-4-7 r-3 a-3 ┌1┐~ │ └2┐~ │ └3~4┤~ └6┐~ └7
19 a-3-1-2-6-4-7 r-3 a-3 r-2 a-2 ┌1┐~ │ │ ┌2~ │ └3┘~4┤~ └6┐~ └7
20 a-3-1-2-6-4-7 r-3 a-3 r-2 a-2 r-6 ┌1┐~ │ │ ┌2~ │ └3┘~4┤~ └7

View File

@ -0,0 +1,2 @@
table = \u0441\u0442\u0456\u043b
apple = \u044f\u0431\u043b\u0443\u043a\u043e

Binary file not shown.