diff --git a/2025/02/cracking_code_interview_17_26_sparse_similarity/Makefile b/2025/02/cracking_code_interview_17_26_sparse_similarity/Makefile index ae1f104..03de71b 100644 --- a/2025/02/cracking_code_interview_17_26_sparse_similarity/Makefile +++ b/2025/02/cracking_code_interview_17_26_sparse_similarity/Makefile @@ -1,19 +1,19 @@ BIN_DIR=bin SRC_DIR=src DEST=bin/program -SRC=$(wildcard $(SRC_DIR)/*.c) -BIN=$(patsubst $(SRC_DIR)/%.c,$(BIN_DIR)/%.c.o,$(SRC)) +SRC=$(wildcard $(SRC_DIR)/*.cpp) +BIN=$(patsubst $(SRC_DIR)/%.cpp,$(BIN_DIR)/%.cpp.o,$(SRC)) all: $(DEST) .PHONY: clean $(DEST): $(BIN) - ${CC} $(LDFLAGS) -o $@ $^ + ${CXX} $(LDFLAGS) -o $@ $^ -$(BIN_DIR)/%.c.o: $(SRC_DIR)/%.c +$(BIN_DIR)/%.cpp.o: $(SRC_DIR)/%.cpp @mkdir -p $(BIN_DIR) - $(CC) $(CFLAGS) -c -o $@ $< + $(CXX) $(CFLAGS) -c -o $@ $< clean: rm -rf $(BIN_DIR) diff --git a/2025/02/cracking_code_interview_17_26_sparse_similarity/input.txt b/2025/02/cracking_code_interview_17_26_sparse_similarity/input.txt new file mode 100644 index 0000000..9c2cac8 --- /dev/null +++ b/2025/02/cracking_code_interview_17_26_sparse_similarity/input.txt @@ -0,0 +1,4 @@ +13: {14, 15, 100, 9, 3} +16: {32, 1, 9, 3, 5} +19: {15, 29, 2, 6, 8, 7} +24: {7, 10} diff --git a/2025/02/cracking_code_interview_17_26_sparse_similarity/src/main.c b/2025/02/cracking_code_interview_17_26_sparse_similarity/src/main.c deleted file mode 100644 index dc79d60..0000000 --- a/2025/02/cracking_code_interview_17_26_sparse_similarity/src/main.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -int main() { - return 0; -} diff --git a/2025/02/cracking_code_interview_17_26_sparse_similarity/src/main.cpp b/2025/02/cracking_code_interview_17_26_sparse_similarity/src/main.cpp new file mode 100644 index 0000000..5c12535 --- /dev/null +++ b/2025/02/cracking_code_interview_17_26_sparse_similarity/src/main.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include +#include + +const int MAX_BUFFER = 0x1000; + +using namespace std; + +void process_data(map> &documents) { + for (auto it1 = documents.begin(); it1 != documents.end(); it1++) { + for (auto it2 = documents.begin(); it2 != documents.end(); it2++) { + if (it1->first == it2->first) { + continue; + } + + set intersection; + set_intersection( + it1->second.begin(), it1->second.end(), + it2->second.begin(), it2->second.end(), + inserter(intersection, next(intersection.begin()))); + + if (intersection.size()) { + fprintf(stderr, "Documents %d and %d have %d common elements\n", + it1->first, it2->first, intersection.size()); + } + } + } +} + +void process_input(map> &documents, char *buffer) { + char *it = buffer; + set items; + + int doc_id = atoi(it); + fprintf(stderr, "Got document id: %d\n", doc_id); + + while (*it && *it != '{') { + it++; + } + + ++it; + + do { + auto nr = atoi(it); + fprintf(stderr, "Got number: %d\n", nr); + items.insert(nr); + while (*it && *it != ',' && *it != '}') { + ++it; + } + + if (*it == '}') { + break; + } + + ++it; + + } while(true); + + fprintf(stderr, "The set contains %d elements\n", items.size()); + + documents[doc_id] = items; +} + +int main() { + char buffer[MAX_BUFFER]; + map> documents; + fprintf(stderr, "Provide input: \n"); + while(1) { + if (!fgets(buffer, MAX_BUFFER, stdin)) { + fprintf(stderr, "End of file or error\n"); + break; + } + + if (strlen(buffer) <= 0 || buffer[0] == '\n') { + break; + } + + process_input(documents, buffer); + } + process_data(documents); + fprintf(stderr, "Execution finished\n"); + return 0; +}