add solution for hw6:3233
Some checks failed
Fast Reverse Tests / test (push) Failing after 4s
Reverse Tests / test (push) Failing after 4s
Sum Tests / test (push) Failing after 4s
Word Stat Tests / test (push) Failing after 4s
Word Stat++ Tests / test (push) Failing after 4s

This commit is contained in:
2026-02-02 14:27:24 +03:00
parent ddba0fc8b5
commit d8154da7a9
4 changed files with 64 additions and 25 deletions

View File

@@ -19,4 +19,4 @@ jobs:
- name: Run Word Stat++ tests - name: Run Word Stat++ tests
run: | run: |
java -ea -cp out wspp.WsppTest Base java -ea -cp out wspp.WsppTest Base 3233

View File

@@ -7,6 +7,7 @@ public class WordScanner {
private BufferedReader br; private BufferedReader br;
private String line = null; private String line = null;
private int pos = 0; private int pos = 0;
private int lineNumber = 0;
public WordScanner(String fileName) throws IOException { public WordScanner(String fileName) throws IOException {
br = new BufferedReader(new InputStreamReader( br = new BufferedReader(new InputStreamReader(
@@ -16,14 +17,17 @@ public class WordScanner {
private boolean hasNextLine() throws IOException { private boolean hasNextLine() throws IOException {
if (line != null && pos < line.length()) return true; if (line != null && pos < line.length()) return true;
line = br.readLine(); line = br.readLine();
if (line != null) {
lineNumber++;
}
pos = 0; pos = 0;
return line != null; return line != null;
} }
private boolean isWordChar(char c) { private boolean isWordChar(char c) {
return Character.isLetter(c) || c == '\'' || return Character.isLetter(c) || Character.isDigit(c) ||
Character.getType(c) == Character.DASH_PUNCTUATION || c == '\'' || c == '$' || c == '_' ||
c == '$' || c == '_'; Character.getType(c) == Character.DASH_PUNCTUATION;
} }
public boolean hasNextWord() throws IOException { public boolean hasNextWord() throws IOException {
@@ -51,7 +55,20 @@ public class WordScanner {
return line.substring(start, pos).toLowerCase(); return line.substring(start, pos).toLowerCase();
} }
public int getLineNumber() {
return lineNumber;
}
public void close() throws IOException { public void close() throws IOException {
br.close(); br.close();
} }
public void reset(String fileName) throws IOException {
br.close();
br = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), StandardCharsets.UTF_8));
line = null;
pos = 0;
lineNumber = 0;
}
} }

View File

@@ -1,17 +1,11 @@
package wspp; import java.io.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap; import java.util.*;
import java.util.Map;
public class WsppPos { public class WsppPos {
public static void main(String[] args) { public static void main(String[] args) {
if (args.length != 2) { if (args.length != 2) {
System.err.println("Usage: java Wspp inputFile outputFile"); System.err.println("Usage: java WsppPos inputFile outputFile");
return; return;
} }
@@ -19,31 +13,53 @@ public class WsppPos {
String outputFile = args[1]; String outputFile = args[1];
try { try {
// Первый проход: считаем количество слов в каждой строке
WordScanner scanner = new WordScanner(inputFile); WordScanner scanner = new WordScanner(inputFile);
Map<Integer, Integer> wordsPerLine = new HashMap<>();
while (scanner.hasNextWord()) {
scanner.nextWord();
int line = scanner.getLineNumber();
wordsPerLine.put(line, wordsPerLine.getOrDefault(line, 0) + 1);
}
scanner.close();
// Второй проход: собираем статистику
scanner = new WordScanner(inputFile);
Map<String, WordInfo> wordMap = new LinkedHashMap<>(); Map<String, WordInfo> wordMap = new LinkedHashMap<>();
int wordPosition = 1; Map<Integer, Integer> currentPosInLine = new HashMap<>();
while (scanner.hasNextWord()) { while (scanner.hasNextWord()) {
String word = scanner.nextWord(); String word = scanner.nextWord();
int lineNum = scanner.getLineNumber();
// Позиция с начала строки
int posInLine = currentPosInLine.getOrDefault(lineNum, 0) + 1;
currentPosInLine.put(lineNum, posInLine);
// Пересчитываем в позицию с конца строки
int totalWordsInLine = wordsPerLine.get(lineNum);
int posFromEnd = totalWordsInLine - posInLine + 1;
if (wordMap.containsKey(word)) { if (wordMap.containsKey(word)) {
WordInfo info = wordMap.get(word); WordInfo info = wordMap.get(word);
info.count++; info.count++;
info.positions.add(wordPosition); info.lineNumbers.add(lineNum);
info.positions.add(posFromEnd);
} else { } else {
WordInfo info = new WordInfo(); WordInfo info = new WordInfo();
info.count = 1; info.count = 1;
info.lineNumbers = new IntList();
info.positions = new IntList(); info.positions = new IntList();
info.positions.add(wordPosition); info.lineNumbers.add(lineNum);
info.positions.add(posFromEnd);
wordMap.put(word, info); wordMap.put(word, info);
} }
wordPosition++;
} }
scanner.close(); scanner.close();
// Запись результата
PrintWriter writer = new PrintWriter( PrintWriter writer = new PrintWriter(
new OutputStreamWriter( new OutputStreamWriter(
new FileOutputStream(outputFile), StandardCharsets.UTF_8)); new FileOutputStream(outputFile), StandardCharsets.UTF_8));
@@ -53,8 +69,8 @@ public class WsppPos {
WordInfo info = entry.getValue(); WordInfo info = entry.getValue();
writer.print(word + " " + info.count); writer.print(word + " " + info.count);
for (int i = 0; i < info.positions.size(); i++) { for (int i = 0; i < info.lineNumbers.size(); i++) {
writer.print(" " + info.positions.get(i)); writer.print(" " + info.lineNumbers.get(i) + ":" + info.positions.get(i));
} }
writer.println(); writer.println();
} }
@@ -65,4 +81,10 @@ public class WsppPos {
System.err.println("Error: " + e.getMessage()); System.err.println("Error: " + e.getMessage());
} }
} }
static class WordInfo {
int count;
IntList lineNumbers;
IntList positions;
}
} }