add solutions for hw6:Base
All checks were successful
Fast Reverse Tests / test (push) Successful in 24s
Reverse Tests / test (push) Successful in 8s
Sum Tests / test (push) Successful in 9s
Word Stat Tests / test (push) Successful in 8s

This commit is contained in:
2026-02-02 13:25:10 +05:00
parent 1e5c8fab61
commit 380611c4df
3 changed files with 155 additions and 0 deletions

28
java/wspp/IntList.java Normal file
View File

@@ -0,0 +1,28 @@
package wspp;
public class IntList {
private int[] array;
private int size;
public IntList() {
array = new int[10];
size = 0;
}
public void add(int value) {
if (size >= array.length) {
int[] newArray = new int[array.length * 2];
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}
array[size++] = value;
}
public int get(int index) {
return array[index];
}
public int size() {
return size;
}
}

View File

@@ -0,0 +1,56 @@
package wspp;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class WordScanner {
private BufferedReader br;
private String line = null;
private int pos = 0;
public WordScanner(String fileName) throws IOException {
br = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), StandardCharsets.UTF_8));
}
private boolean hasNextLine() throws IOException {
if (line != null && pos < line.length()) return true;
line = br.readLine();
pos = 0;
return line != null;
}
private boolean isWordChar(char c) {
return Character.isLetter(c) || c == '\'' ||
Character.getType(c) == Character.DASH_PUNCTUATION;
}
public boolean hasNextWord() throws IOException {
while (hasNextLine()) {
while (pos < line.length() && !isWordChar(line.charAt(pos))) {
pos++;
}
if (pos < line.length()) {
return true;
}
}
return false;
}
public String nextWord() {
while (pos < line.length() && !isWordChar(line.charAt(pos))) {
pos++;
}
int start = pos;
while (pos < line.length() && isWordChar(line.charAt(pos))) {
pos++;
}
return line.substring(start, pos).toLowerCase();
}
public void close() throws IOException {
br.close();
}
}

71
java/wspp/Wspp.java Normal file
View File

@@ -0,0 +1,71 @@
package wspp;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class Wspp {
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("Usage: java Wspp inputFile outputFile");
return;
}
String inputFile = args[0];
String outputFile = args[1];
try {
WordScanner scanner = new WordScanner(inputFile);
// LinkedHashMap сохраняет порядок вставки
Map<String, WordInfo> wordMap = new LinkedHashMap<>();
int wordPosition = 1;
while (scanner.hasNextWord()) {
String word = scanner.nextWord();
if (wordMap.containsKey(word)) {
WordInfo info = wordMap.get(word);
info.count++;
info.positions.add(wordPosition);
} else {
WordInfo info = new WordInfo();
info.count = 1;
info.positions = new IntList();
info.positions.add(wordPosition);
wordMap.put(word, info);
}
wordPosition++;
}
scanner.close();
// Запись результата
PrintWriter writer = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream(outputFile), StandardCharsets.UTF_8));
for (Map.Entry<String, WordInfo> entry : wordMap.entrySet()) {
String word = entry.getKey();
WordInfo info = entry.getValue();
writer.print(word + " " + info.count);
for (int i = 0; i < info.positions.size(); i++) {
writer.print(" " + info.positions.get(i));
}
writer.println();
}
writer.close();
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
static class WordInfo {
int count;
IntList positions;
}
}