i will switch to urandom
This commit is contained in:
@@ -39,31 +39,27 @@ foreach(dir ${SRC_SUBDIRS})
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
# -----------------------------
|
||||
# Detect all .cpp files
|
||||
# -----------------------------
|
||||
file(GLOB_RECURSE ALL_CPP "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
|
||||
|
||||
# -----------------------------
|
||||
# Detect all main-solution-N files
|
||||
# -----------------------------
|
||||
file(GLOB SOLUTION_MAINS "${CMAKE_CURRENT_SOURCE_DIR}/src/main-solution-*.cpp")
|
||||
|
||||
# -----------------------------
|
||||
# Detect all other .cpp files (common utilities)
|
||||
# -----------------------------
|
||||
file(GLOB_RECURSE ALL_CPP "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
|
||||
list(REMOVE_ITEM ALL_CPP ${SOLUTION_MAINS}) # remove all mains
|
||||
|
||||
# -----------------------------
|
||||
# Create an executable for each main-solution-N
|
||||
# -----------------------------
|
||||
foreach(MAIN_FILE ${SOLUTION_MAINS})
|
||||
get_filename_component(MAIN_NAME ${MAIN_FILE} NAME_WE)
|
||||
|
||||
# Executable name: mock-uil-main-solution-N
|
||||
set(EXE_NAME "${PROJECT_NAME}-${MAIN_NAME}")
|
||||
|
||||
# All CPP files except this executable's main
|
||||
set(CPP_FILES ${ALL_CPP})
|
||||
list(REMOVE_ITEM CPP_FILES ${MAIN_FILE})
|
||||
# Each executable = its main + all common cpp files
|
||||
add_executable(${EXE_NAME} ${MAIN_FILE} ${ALL_CPP})
|
||||
|
||||
add_executable(${EXE_NAME} ${MAIN_FILE})
|
||||
target_sources(${EXE_NAME} PRIVATE ${CPP_FILES})
|
||||
target_link_libraries(${EXE_NAME} PRIVATE ${COMMON_LIBS})
|
||||
target_include_directories(${EXE_NAME} PRIVATE ${EXTERNAL_INCLUDES} ${SRC_INCLUDES})
|
||||
endforeach()
|
||||
|
||||
BIN
exam/packet.pdf
BIN
exam/packet.pdf
Binary file not shown.
@@ -141,6 +141,7 @@ Output:
|
||||
15 18
|
||||
\end{verbatim}
|
||||
\end{comment}
|
||||
\newpage
|
||||
% ========================= Problem 6 =========================
|
||||
\problem{Problem 6(.7): Six Seven}
|
||||
\textit{Difficulty: Easy}
|
||||
@@ -185,6 +186,7 @@ EMPTY
|
||||
\end{verbatim}
|
||||
|
||||
% ========================= Problem 7 =========================
|
||||
\newpage
|
||||
\problem{Problem 7: Limited Jumps}
|
||||
\textit{Difficulty: Medium}
|
||||
|
||||
|
||||
131
src/main-solution-6.cpp
Normal file
131
src/main-solution-6.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
#include "spdlog/fmt/bundled/format.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::string solution(const std::string &S) {
|
||||
std::vector<char> seq;
|
||||
seq.reserve(S.size() * 2);
|
||||
|
||||
// iterate safely through S
|
||||
for (std::string::const_iterator it = S.begin(); it != S.end(); ++it) {
|
||||
char c = *it;
|
||||
|
||||
switch (c) {
|
||||
case '6': {
|
||||
seq.push_back('6');
|
||||
break;
|
||||
}
|
||||
|
||||
case '7': {
|
||||
if (!seq.empty() && seq.back() == '6') {
|
||||
seq.pop_back();
|
||||
} else {
|
||||
seq.push_back('7');
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'A': {
|
||||
// reverse using iterators
|
||||
if (seq.size() > 1) {
|
||||
std::vector<char>::iterator left = seq.begin();
|
||||
std::vector<char>::iterator right = seq.end();
|
||||
--right;
|
||||
while (left < right) {
|
||||
char tmp = *left;
|
||||
*left = *right;
|
||||
*right = tmp;
|
||||
++left;
|
||||
--right;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'B': {
|
||||
// duplicate using iterators; no idx
|
||||
std::size_t n = seq.size();
|
||||
seq.reserve(n * 2);
|
||||
|
||||
if (n > 0) {
|
||||
std::vector<char>::const_iterator it2 = seq.begin();
|
||||
std::vector<char>::const_iterator end2 = seq.end();
|
||||
while (it2 != end2) {
|
||||
seq.push_back(*it2);
|
||||
++it2;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case '#': {
|
||||
seq.clear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (seq.empty()) {
|
||||
return "EMPTY";
|
||||
}
|
||||
|
||||
// Convert vector<char> to string via iterators
|
||||
std::string result;
|
||||
result.reserve(seq.size());
|
||||
for (std::vector<char>::const_iterator it = seq.begin(); it != seq.end(); ++it) {
|
||||
result.push_back(*it);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string generate_testcase(std::size_t length, std::vector<char> charset, std::mt19937 generator, std::uniform_int_distribution<size_t> dist){
|
||||
if(length==0 || charset.size()==0) return "";
|
||||
|
||||
std::string result = {};
|
||||
result.resize(length);
|
||||
|
||||
for (size_t i= {0}; i<length; i++) {
|
||||
result.at(i)=charset.at(dist(generator));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(){
|
||||
spdlog::info("Logging is functional, generating cases for Six-Seven");
|
||||
const std::vector<char> charset = std::vector{'6','7','A','B'};
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::filesystem::path folder = "solution-6-files";
|
||||
try {
|
||||
if(std::filesystem::exists(folder)){
|
||||
std::filesystem::remove_all(folder);
|
||||
}
|
||||
std::filesystem::create_directory(folder);
|
||||
|
||||
spdlog::debug("Folder {} created", folder.string());
|
||||
} catch (const std::filesystem::filesystem_error& e) {
|
||||
spdlog::error("FS error {}", e.what());
|
||||
return 1;
|
||||
}
|
||||
std::random_device random_device = std::random_device{};
|
||||
std::mt19937 generator = std::mt19937{random_device()};
|
||||
std::uniform_int_distribution<std::size_t> dist (0, charset.size()-1);
|
||||
spdlog::info("Solution to testcase 0 is: {}", solution("67A6"));
|
||||
for (size_t i = 0; i < 500; i++) {
|
||||
std::string input = generate_testcase(i,charset, generator, dist);
|
||||
std::string output = solution(input);
|
||||
|
||||
std::ofstream input_file = std::ofstream{fmt::format("{}/{}.in", folder.string(), i)};
|
||||
std::ofstream output_file = std::ofstream{fmt::format("{}/{}.out", folder.string(),i)};
|
||||
input_file << input; input_file.flush();
|
||||
output_file <<output; output_file.flush();
|
||||
input_file.close(); output_file.close();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user