Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sem2/Cherendichenko Ivan/mini_hw3/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SUMO TATAMI is the best game, worthy of the Game Awards.
The great Kondyusha is the best voice actor — give him an Oscar!
May Danya Grishin punish me for not following Google style.
SOLID is my bible.
17 changes: 17 additions & 0 deletions sem2/Cherendichenko Ivan/mini_hw3/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <string>

#include "../Header Files/text_processor.h"

int main() {
std::size_t threshold = 0;
std::cout << "Enter minimum line length threshold: ";
std::cin >> threshold;
std::cin.ignore();
std::string search_word;
std::cout << "Enter word to search: ";
std::getline(std::cin, search_word);
homework::runHomework("input.txt", "output.txt", threshold, search_word);
std::cout << "Done. Results written to output.txt\n";
return 0;
}
30 changes: 30 additions & 0 deletions sem2/Cherendichenko Ivan/mini_hw3/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
===== SUMO TATAMI <3 <3 <3 =====

===== Mini-homework 3 (Variant 1) =====

1. Original lines from input.txt (4 lines):
[0] SUMO TATAMI is the best game, worthy of the Game Awards.
[1] The great Kondyusha is the best voice actor — give him an Oscar!
[2] May Danya Grishin punish me for not following Google style.
[3] SOLID is my bible.

Threshold for short lines: 50

2. After removing short lines (3 lines):
[0] SUMO TATAMI is the best game, worthy of the Game Awards.
[1] The great Kondyusha is the best voice actor — give him an Oscar!
[2] May Danya Grishin punish me for not following Google style.

3. After replacing spaces with '_' (3 lines):
[0] SUMO_TATAMI_is_the_best_game,_worthy_of_the_Game_Awards.
[1] The_great_Kondyusha_is_the_best_voice_actor_—_give_him_an_Oscar!
[2] May_Danya_Grishin_punish_me_for_not_following_Google_style.

4. Search for word "SUMO":
found in: SUMO_TATAMI_is_the_best_game,_worthy_of_the_Game_Awards.

5. Total characters (excluding '_'): 150

6. Line lengths (3 values):
[56, 66, 59]

144 changes: 144 additions & 0 deletions sem2/Cherendichenko Ivan/mini_hw3/text_processor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#include "../Header Files/text_processor.h"

#include <algorithm>
#include <fstream>
#include <iterator>
#include <numeric>

namespace homework {

std::vector<std::string> readLinesFromFile(const std::string& filename) {
std::vector<std::string> lines;
std::ifstream input_file(filename);
std::string line;
while (std::getline(input_file, line)) {
lines.push_back(line);
}
return lines;
}

std::vector<std::string> filterShortLines(const std::vector<std::string>& lines,
std::size_t threshold) {
std::vector<std::string> result;

auto isLongEnough = [threshold](const std::string& s) {
return s.length() >= threshold;
};
std::copy_if(lines.begin(), lines.end(), std::back_inserter(result),
isLongEnough);
return result;
}

std::vector<std::string> replaceSpacesWithUnderscore(
const std::vector<std::string>& lines) {
std::vector<std::string> result;
result.reserve(lines.size());

auto replaceSpaces = [](std::string s) {
std::replace(s.begin(), s.end(), ' ', '_');
return s;
};
std::transform(lines.begin(), lines.end(), std::back_inserter(result),
replaceSpaces);
return result;
}

std::string findFirstLineWithWord(const std::vector<std::string>& lines,
const std::string& word) {
auto containsWord = [&word](const std::string& s) {
return s.find(word) != std::string::npos;
};
auto it = std::find_if(lines.begin(), lines.end(), containsWord);
if (it != lines.end()) {
return *it;
}
return std::string();
}

std::size_t countCharactersWithoutUnderscore(
const std::vector<std::string>& lines) {
return std::accumulate(
lines.begin(), lines.end(), static_cast<std::size_t>(0),
[](std::size_t sum, const std::string& s) {
return sum + static_cast<std::size_t>(std::count_if(
s.begin(), s.end(), [](char c) { return c != '_'; }));
});
}

std::vector<int> computeLineLengths(const std::vector<std::string>& lines) {
std::vector<int> lengths;
lengths.reserve(lines.size());
std::transform(
lines.begin(), lines.end(), std::back_inserter(lengths),
[](const std::string& s) { return static_cast<int>(s.length()); });
return lengths;
}

namespace {

void writeStringVector(std::ofstream& out, const std::string& title,
const std::vector<std::string>& vec) {
out << title << " (" << vec.size() << " lines):\n";
if (vec.empty()) {
out << " [empty]\n";
} else {
for (std::size_t i = 0; i < vec.size(); ++i) {
out << " [" << i << "] " << vec[i] << "\n";
}
}
out << "\n";
}

void writeIntVector(std::ofstream& out, const std::string& title,
const std::vector<int>& vec) {
out << title << " (" << vec.size() << " values):\n [";
for (std::size_t i = 0; i < vec.size(); ++i) {
out << vec[i];
if (i + 1 < vec.size()) {
out << ", ";
}
}
out << "]\n\n";
}

} // namespace

void writeResultsToFile(const std::string& filename,
const std::vector<std::string>& original,
const std::vector<std::string>& filtered,
const std::vector<std::string>& replaced,
std::size_t threshold, const std::string& search_word,
const std::string& search_result,
std::size_t character_count,
const std::vector<int>& lengths) {
std::ofstream out(filename);
out << "===== SUMO TATAMI <3 <3 <3 =====\n\n";
out << "===== Mini-homework 3 (Variant 1) =====\n\n";
writeStringVector(out, "1. Original lines from input.txt", original);
out << "Threshold for short lines: " << threshold << "\n\n";
writeStringVector(out, "2. After removing short lines", filtered);
writeStringVector(out, "3. After replacing spaces with '_'", replaced);
out << "4. Search for word \"" << search_word << "\":\n";
if (search_result.empty()) {
out << " [no line containing this word]\n\n";
} else {
out << " found in: " << search_result << "\n\n";
}
out << "5. Total characters (excluding '_'): " << character_count << "\n\n";
writeIntVector(out, "6. Line lengths", lengths);
}

void runHomework(const std::string& input_filename,
const std::string& output_filename, std::size_t threshold,
const std::string& search_word) {
auto original = readLinesFromFile(input_filename);
auto filtered = filterShortLines(original, threshold);
auto replaced = replaceSpacesWithUnderscore(filtered);
auto found = findFirstLineWithWord(replaced, search_word);
auto count = countCharactersWithoutUnderscore(replaced);
auto lengths = computeLineLengths(replaced);
writeResultsToFile(output_filename, original, filtered, replaced, threshold,
search_word, found, count, lengths);
}

} // namespace homework
38 changes: 38 additions & 0 deletions sem2/Cherendichenko Ivan/mini_hw3/text_processor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <cstddef>
#include <string>
#include <vector>

namespace homework {

std::vector<std::string> readLinesFromFile(const std::string& filename);

std::vector<std::string> filterShortLines(const std::vector<std::string>& lines,
std::size_t threshold);

std::vector<std::string> replaceSpacesWithUnderscore(
const std::vector<std::string>& lines);

std::string findFirstLineWithWord(const std::vector<std::string>& lines,
const std::string& word);

std::size_t countCharactersWithoutUnderscore(
const std::vector<std::string>& lines);

std::vector<int> computeLineLengths(const std::vector<std::string>& lines);

void writeResultsToFile(const std::string& filename,
const std::vector<std::string>& original,
const std::vector<std::string>& filtered,
const std::vector<std::string>& replaced,
std::size_t threshold, const std::string& search_word,
const std::string& search_result,
std::size_t character_count,
const std::vector<int>& lengths);

void runHomework(const std::string& input_filename,
const std::string& output_filename, std::size_t threshold,
const std::string& search_word);

} // namespace homework