text stringlengths 1 2.12k | source dict |
|---|---|
c++, image, template, classes, variadic
Image<ElementT>& operator*=(const Image<ElementT>& rhs)
{
check_size_same(rhs, *this);
std::transform(std::ranges::cbegin(image_data), std::ranges::cend(image_data), std::ranges::cbegin(rhs.image_data),
std::ranges::begin(image_... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
#ifdef USE_BOOST_SERIALIZATION
void Save(std::string filename)
{
const std::string filename_with_extension = filename + ".dat";
// Reference: https://stackoverflow.com/questions/523872/how-do-you-serialize-an-object-in-c
st... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
struct RGB
{
std::uint8_t channels[3];
};
using GrayScale = std::uint8_t;
namespace TinyDIP
{
// recursive_depth function implementation
template<typename T>
constexpr std::size_t recursive_depth()
{
return std::size_t{0};
}
template<std::... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
template<std::ranges::input_range Range,
std::same_as<std::size_t>... Sizes>
Image(const Range& input, Sizes... sizes):
size{sizes...}, image_data(begin(input), end(input))
{
if (image_data.size() != (1 * ... * sizes)) {
... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
template<typename... Args>
constexpr ElementT& at(const Args... indexInput)
{
checkBoundary(indexInput...);
constexpr std::size_t n = sizeof...(Args);
if(n != size.size())
{
throw std::runtime_error(... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
constexpr std::size_t getHeight() const noexcept
{
return size[1];
}
constexpr auto getSize() noexcept
{
return size;
}
std::vector<ElementT> const& getImageData() const noexcept { return image_data; } ... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
// Enable this function if ElementT = RGB
void print(std::string separator = "\t", std::ostream& os = std::cout) const
requires(std::same_as<ElementT, RGB>)
{
for (std::size_t y = 0; y < size[1]; ++y)
{
for (std::s... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
Image<ElementT>& operator*=(const Image<ElementT>& rhs)
{
check_size_same(rhs, *this);
std::transform(std::ranges::cbegin(image_data), std::ranges::cend(image_data), std::ranges::cbegin(rhs.image_data),
std::ranges::begin(image_... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
void Save(std::string filename)
{
const std::string filename_with_extension = filename + ".dat";
// Reference: https://stackoverflow.com/questions/523872/how-do-you-serialize-an-object-in-c
std::ofstream ofs(filename_with_extension, s... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
template<typename ElementT>
constexpr bool is_height_same(const Image<ElementT>& x, const Image<ElementT>& y)
{
return x.getHeight() == y.getHeight();
}
template<typename ElementT>
constexpr bool is_height_same(const Image<ElementT>& x, const Image<E... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
template<typename ElementT>
constexpr void assert_size_same(const Image<ElementT>& x, const Image<ElementT>& y, const Image<ElementT>& z)
{
assert_size_same(x, y);
assert_size_same(y, z);
assert_size_same(x, z);
}
template<typename Elemen... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
The output of the test code above:
Test with 1D image:
3 0 0 0 0
Test with 2D image:
1 1 1 1 1
3 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Test with 3D image:
0 0 0 0 0
0 0 0 4 0
0 0 0 0 ... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
Answer: Apply the techniques you have learned to all of the code
In the past reviews of your questions we have repeatedly stressed that you should simplify the code, reduce code duplication and make it more generic. Those things also usually go hand in hand. However, you are not... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
Simplify the code
The code to calculate the index is still looking a bit more complex than necessary to me. Every time function() is called, you have a for-loop to calculate m from scratch. However, if you move m out of the lambda's body you don't need that loop.
You also don't ... | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, image, template, classes, variadic
std::size_t i = 0;
std::size_t stride = 1;
std::size_t position = 0;
auto update_position = [&](auto index) {
position += index * stride;
stride *= size[i++];
};
(update_position(indexInput), ...);
return image_data[position];
} | {
"domain": "codereview.stackexchange",
"id": 45352,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, image, template, classes, variadic",
"url": null
} |
c++, logging
Title: Header only logging library in C++
Question: Source Code
I've attempted to create a simple logging library in cpp. My goal was to make it lightweight to incorporate and easy to add to the codebase.
/**
* @file herrlog.hh
* @author Saphereye
* @brief Header file only logging library
* @version ... | {
"domain": "codereview.stackexchange",
"id": 45353,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, logging",
"url": null
} |
c++, logging
/**
* @brief Logger implementation providing flexible logging capabilities.
*
*/
class Logger {
private:
static LogType log_type;
static std::string output_file_name;
static std::ofstream output_file;
static bool is_output_to_console;
static const char* datetime_format;
stati... | {
"domain": "codereview.stackexchange",
"id": 45353,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, logging",
"url": null
} |
c++, logging
/**
* @brief Logs a message with specified details to the console or a file.
*
* @tparam Args
* @param name
* @param color
* @param format
* @param args
*/
template <typename... Args>
static void log(const char* name, const char* color, const char* format,
... | {
"domain": "codereview.stackexchange",
"id": 45353,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, logging",
"url": null
} |
c++, logging
/**
* @brief Set the datetime format object, default is "%Y-%m-%d %H:%M:%S"
*
* @param datetime_format
*/
static void set_datetime_format(const char* datetime_format) {
Logger::datetime_format = datetime_format;
}
/**
* @brief Logs messages of type trace.
... | {
"domain": "codereview.stackexchange",
"id": 45353,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, logging",
"url": null
} |
c++, logging
/**
* @brief Logs messages of type warning. Doesn't exit the program, just
* notifies the issue.
*
* @tparam Args
* @param format
* @param args
*/
template <typename... Args>
static void warn(const char* format, Args... args) {
if (log_type & LogType::War... | {
"domain": "codereview.stackexchange",
"id": 45353,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, logging",
"url": null
} |
c++, logging
If the file option is chosen, the script writes to the file every time a logging function is called. My logic was that if it writes all the logs to a temporary buffer and then proceeds to write it to a file at the end of execution, all the logs will be lost in case of premature termination.
Currently, I a... | {
"domain": "codereview.stackexchange",
"id": 45353,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, logging",
"url": null
} |
c++, logging
The code right now grabs the mutex and then invokes the inserter for that object. It should be the other way around to minimize the time that the lock is held. Here's a trace of that program with the mutex as is:
[TRACE 2024-01-01 09:37:00] This is trace message number 1
[ WARN 2024-01-01 09:37:00] 1: c... | {
"domain": "codereview.stackexchange",
"id": 45353,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, logging",
"url": null
} |
c++, logging
In this rewrite, we print instead to a stringstream without a lock and then only grab the lock when we print that string out to either the console or a log file:
template <typename... Args>
static void log(const char* name, const char* color, const char* format,
Args... args) {
auto cu... | {
"domain": "codereview.stackexchange",
"id": 45353,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, logging",
"url": null
} |
c++, logging
Now the log is a little more like what we would expect:
[TRACE 2024-01-01 09:42:15] This is trace message number 1
[ WARN 2024-01-01 09:42:15] 2: count = 10, What a refreshing sleep!
[ WARN 2024-01-01 09:42:15] 1: count = 10, What a refreshing sleep!
[ WARN 2024-01-01 09:42:16] 1: count = 9, What a refres... | {
"domain": "codereview.stackexchange",
"id": 45353,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, logging",
"url": null
} |
c++, logging
Reconsider header-only
The usual expectation of a header-only library is that if I don't invoke anything from it, it should not add any code. However, this puts the LogType operator| and operator& code into the global namespace. Try putting just the #include of this logger into to separate files foo.cpp... | {
"domain": "codereview.stackexchange",
"id": 45353,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, logging",
"url": null
} |
python, matplotlib
Title: Colorbar for Matplotlib 3D patch plot
Question: I am trying to make a 3D grid plot in Python using Matplotlib. The grid consists of hexahedral cells (with qudrilateral surfaces). The surfaces should be colored according to their height, that is: the z coordinate. A colorbar should accompany ... | {
"domain": "codereview.stackexchange",
"id": 45354,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, matplotlib",
"url": null
} |
python, matplotlib
Coding practices
Don't put code in the global namespace, put inside a function.
This protects you from bad practices like using global variables, whether intentionally or by accident.
For example, you could move all the code current code in the global namespace inside a main function:
def main():
... | {
"domain": "codereview.stackexchange",
"id": 45354,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, matplotlib",
"url": null
} |
c++, performance, sorting
Title: Fraudulent activity notifications problem from hackerrank
Question: There's a problem on hackerrank called fraudulent activity notifications: given an array of integers and a value k, I'm supposed to increment a counter if the median of the numbers up until k , multiplied by 2 is grea... | {
"domain": "codereview.stackexchange",
"id": 45355,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, performance, sorting",
"url": null
} |
c++, performance, sorting
Answer: contract
float GetMedian( std::vector<int> sample_days )
This signature is nice enough.
It looks like it describes the API
contract.
We see what caller must present, what will come back,
and the informative GetMedian identifier tells us
what math concept is involved.
Oh, wait!
It ... | {
"domain": "codereview.stackexchange",
"id": 45355,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, performance, sorting",
"url": null
} |
c++, performance, sorting
You probably want expenditure_days instead.
The rest of it looks correct.
(Though it does a lot of work.
It has high compute complexity, leading to contest timeouts.)
algorithm
pqueue
Expenditures are drawn from some arbitrary, unseen distribution.
Size of the D window can potentially be near... | {
"domain": "codereview.stackexchange",
"id": 45355,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, performance, sorting",
"url": null
} |
c++, performance, sorting
So complexity is independent of window size,
and only depends on number of distinct values the distribution can give us.
Recall that "median" means "half the sorted values are to its left"
on the number line, and "half of them are to its right".
We found a "left" cumsum of D / 2.
If we kept g... | {
"domain": "codereview.stackexchange",
"id": 45355,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, performance, sorting",
"url": null
} |
c++, performance, sorting
Switch to HashMap, and run a doubly linked list through the counter nodes.
Defer the increment and decrement operations, noting them in temp variables.
Compute cumsum in O(M) time by traversing the (ordered) linked list.
Along the way, we will encounter the nodes that need {inc, dec},
or we w... | {
"domain": "codereview.stackexchange",
"id": 45355,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, performance, sorting",
"url": null
} |
c++, performance, sorting
all values within a single bin.
For realistic distributions seen by production systems,
the binning approach likely allows significant resource savings,
even when the distribution is not constrained to small set of integers. | {
"domain": "codereview.stackexchange",
"id": 45355,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, performance, sorting",
"url": null
} |
c#, winforms
Title: Is there a more performance-friendly way to check and change the colors of a WinForms app?
Question: I'm a somewhat new programmer developing a WinForms application in C# that has the option to change the theme/colors depending on user input. Due to the complexity of the application and the amount... | {
"domain": "codereview.stackexchange",
"id": 45356,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#, winforms",
"url": null
} |
c#, winforms
foreach (TabPage tp in tabControl1.Controls.OfType<TabPage>())
{ tp.BackColor = Color.WhiteSmoke; }
foreach (TabPage tp in tabControl2.Controls.OfType<TabPage>())
{ tp.BackColor = Color.WhiteSmoke; }
// Labels
foreach (Label... | {
"domain": "codereview.stackexchange",
"id": 45356,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#, winforms",
"url": null
} |
c#, winforms
// TabPages + TabControls
// tabPage1.BackColor = Color.White;
// tabPage7.BackColor = Color.White;
// tabPage3.BackColor = Color.White;
tabControl2.BackColor = Color.White;
tabControl1.BackColor = Color.Whit... | {
"domain": "codereview.stackexchange",
"id": 45356,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#, winforms",
"url": null
} |
c#, winforms
Clearly the conditionals work fine,
at the expense of duplicating the "visit all the nodes" code.
Consider putting Color.WhiteSmoke into a Dictionary entry
that has "light" (rather than "dark") in its key.
Then you could unconditionally just assign a color.
remembering previously applied updates
Do keep t... | {
"domain": "codereview.stackexchange",
"id": 45356,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#, winforms",
"url": null
} |
python
Title: Parsing PDFs into Python structures
Question: Take a resume (or “CV” outside of the US) and return all the text within it in a formatted way. Right now the below script outputs it to a txt file.
I used this guide as inspiration and have looked to improve on it, by using slightly saner control flow and f... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
format_per_line = list(line_formats)
return line_text, format_per_line
def extract_table(pdf_path: str, page_num: int, table_num: int) -> Optional[list[list[str]]]:
"""
Extracts a specified table from a given page of a PDF document.
Parameters:
pdf_path (str): The file path of the PDF ... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
Returns:
str: The string representation of the table.
Example usage:
table = [['Name', 'Age'], ['Alice', '23'], ['Bob', None]]
print(table_converter(table))
"""
converted_rows = []
for row in table:
cleaned_row = [
item.replace('\n', ' ') if item is n... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
for i, table in enumerate(tables):
tx0, ty0, tx1, ty1 = table.bbox
if tx0 <= x0 < x1 <= tx1 and ty0 <= y0 < y1 <= ty1:
return i # Return the index of the table
return None
def process_tables(tables, pdf_path, pagenum, text_from_tables):
# Extracting the tables of the page
... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
process_tables(tables, filepath, pagenum, text_from_tables)
# Find all the elements
page_elements = [(element.y1, element) for element in page._objs]
# Sort all the element as they appear in the page
page_elements.sort(key=lambda a: a[0], reverse=True)
# Find the elemen... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
Answer: Self-analysis
The analysis of your own code is pretty accurate, which is a great start
Style
This code follows PEP8 guidelines for the most part, which is also great
Docstrings
The docstrings are helpful, but some of them are missing. I would argue
that specifying the types in the docstrings is not a go... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
Typing
Using type annotations is great, but it's not more informative than comments or docstrings
if not checked with a static type checker like mypy. For example, the table parameter of table_converter should be annotated with list[list[str | None]]. mypy also detects
that in process_tables, table can be None,... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
In process_text_element, page_text is modified during the execution of the function like line_format and page_content, but is not returned. This function should either not return anything, or return the data that it actually produced. The fact that three arguments out of the four are mutated show that a data st... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
Use of two libraries
Using two libraries to interact with PDFs instead of one can lead to problems that are visible here: multiple file openings, data structure conversions... Sticking to a single one can help a lot. pdfplumber depends on pdfminer.six, but it does not really help here. Have you considered only ... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
Comments
Some of the comments are not that informative:
# Extract the information of the table
table = extract_table(pdf_path, pagenum, table_num)
# Convert the table information in structured string format
table_string = table_converter(table)
# Append the table string into a list
text_from_tables.append(table... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
def extract_text_formats(element: LTTextContainer[LTComponent]) -> list[str]:
"""
Extracts the unique formats (font names and sizes) from a given element.
Parameters:
element: The element from which the formats are extracted.
Returns:
A list of unique formats.
"""
line_... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
Parameters:
table: the table to convert
Returns:
an object that contains the text of the table and its bounding box
"""
x0, y0_down, x1, y1_down = table.bbox
page_y_max = table.page.bbox[3]
return LTTextTable(
text=format_table(table.extract()),
bbox=(x0, pag... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
Returns:
An iterator that yields the sorted text elements contained in this page
"""
page_elements = sorted(lt_page, key=lambda elem: elem.y1, reverse=True)
table_in_page = 0
for element in page_elements:
table_index = find_table_for_element(element, text_tables)
if table... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
Parameters:
filepath: Input path to a PDF
output_path: Path to the .txt file to generate
"""
pages = extract_pages(filepath)
with pdfplumber.open(filepath) as pdf:
text_per_page = list(iter_pdf_pages(pdf.pages, pages))
result = "".join(element.content for element in text... | {
"domain": "codereview.stackexchange",
"id": 45357,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
c++, security, http, windows, https
Title: Reading a date from the start of a text webpage from github to determine if the program is fully updated
Question: #define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <wininet.h>
const char thisVersionDate[] = "2024-01-05";
bool... | {
"domain": "codereview.stackexchange",
"id": 45358,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, security, http, windows, https",
"url": null
} |
c++, security, http, windows, https
// windows API documentation says to call this in a loop
DWORD bytesReadThisLoop = 0;
DWORD totalBytesRead = 0;
while (
totalBytesRead < sizeof(currentVersionDate) - 1
&& InternetReadFile(*hData, ¤tVersionDate[totalBytesRead], sizeof(currentVersionD... | {
"domain": "codereview.stackexchange",
"id": 45358,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, security, http, windows, https",
"url": null
} |
c++, security, http, windows, https
Answer: Overall, the code is structured and easy to follow. Some of the remarks below can probably be ignored in the context of this small program, they only become more important when dealing with a larger codebase, they are still something worth being aware of.
Use Of Pointer Para... | {
"domain": "codereview.stackexchange",
"id": 45358,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, security, http, windows, https",
"url": null
} |
c++, security, http, windows, https
The return value slot is occupied. This means you can't use it to return the actual results of the function to the caller and that you have to use pointers/references (see above) to convey those results. Note that you could use std::optional<> to either return a falsy value or actua... | {
"domain": "codereview.stackexchange",
"id": 45358,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, security, http, windows, https",
"url": null
} |
c++, security, http, windows, https
I guess this code has a very restricted scope. Otherwise, is most recent version begets the question "what?". I'd normally expect some kind of parameter telling the function what or where to check the version.
The location where to check a version is provided by some hardcoded inlin... | {
"domain": "codereview.stackexchange",
"id": 45358,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, security, http, windows, https",
"url": null
} |
c++, algorithm, strings, string-processing
Title: Z-Function/ Algorithms on strings. C++
Question: The problem:
Given a string s. For each i from 1 to |s|, find the number of occurrences of its prefix of length i in the string.
Input:
The first line of input contains an integer q (1≤q≤10⁵) — the number of datasets i... | {
"domain": "codereview.stackexchange",
"id": 45359,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, strings, string-processing",
"url": null
} |
c++, algorithm, strings, string-processing
return 0;
}
Its only issue is that I am exceeding the allowed time on tests, and I do not know how to optimize this algorithm. I suspect that the problem lies in strings of the form aaaaaa...aaabcd, where the number of identical characters is quite large, causing the algorit... | {
"domain": "codereview.stackexchange",
"id": 45359,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, strings, string-processing",
"url": null
} |
c++, algorithm, strings, string-processing
We explicitly set freqs.back() = 1 because z[0] is always 0 instead of being equal to the word length.
If there exists a prefix of length N there also exist prefixes of lengths 1, ..., N - 1. This observation gives us the following cumulative sums
for (int i = freqs.size() - ... | {
"domain": "codereview.stackexchange",
"id": 45359,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, algorithm, strings, string-processing",
"url": null
} |
error-handling, kotlin, language-design
Title: Kotlin: scoped functions with try/catch support
Question: I'm working with Kotlin for some time. Compared to Java it's more concise. Still it has some legacy inherited from Java. For example the try-catch-construct. Though it has been upgraded from a statement in Java to... | {
"domain": "codereview.stackexchange",
"id": 45360,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "error-handling, kotlin, language-design",
"url": null
} |
error-handling, kotlin, language-design
Motivation:
Declaring TryRun and TryLet makes the use cases explicit. Additionally it defines the generic type parameter B of Try and provides thus an easier way to use than Try itself.
I choose to declare a function named on rather than catch. In my opinion it reads better wit... | {
"domain": "codereview.stackexchange",
"id": 45360,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "error-handling, kotlin, language-design",
"url": null
} |
error-handling, kotlin, language-design
// fails with NumberFormatException - returns null as default value
"five".tryLet { DayOfWeek.of(it.toInt()) }
.on(NumberFormatException::class) {
log(it)
null
}.on(DateTimeException::class) {
log(it)
null
... | {
"domain": "codereview.stackexchange",
"id": 45360,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "error-handling, kotlin, language-design",
"url": null
} |
error-handling, kotlin, language-design
Additionally, your code creates a bit of overhead by creating instances of Try and multiple Map instances for every tryRun/tryLet that you do.
Multi-catch
I notice that all your catch statements seem to do the same thing at the moment, if that will be the case then you might wan... | {
"domain": "codereview.stackexchange",
"id": 45360,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "error-handling, kotlin, language-design",
"url": null
} |
algorithm, swift, ios, caesar-cipher, swiftui
Title: Caesar Cipher in Swift (iOS-app)
Question: I have implemented the Caesar Cipher in Swift and incorporated it into an iOS-app.
I guess it's formally correct. Please see the attached images, which show how the usage of the app is meant. | {
"domain": "codereview.stackexchange",
"id": 45361,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "algorithm, swift, ios, caesar-cipher, swiftui",
"url": null
} |
algorithm, swift, ios, caesar-cipher, swiftui
Here's the relevant code:
struct ContentView: View {
@State var input = ""
@State var output = ""
@State var shift = 1
@State var hasSubmitted = false
var body: some View {
VStack {
Text("Caesar Cipher").font(.title)
... | {
"domain": "codereview.stackexchange",
"id": 45361,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "algorithm, swift, ios, caesar-cipher, swiftui",
"url": null
} |
algorithm, swift, ios, caesar-cipher, swiftui
output: $output,
hasSubmitted: $hasSubmitted) { index, count in
((index - shift) + count) % count
}
Spacer()
}
... | {
"domain": "codereview.stackexchange",
"id": 45361,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "algorithm, swift, ios, caesar-cipher, swiftui",
"url": null
} |
algorithm, swift, ios, caesar-cipher, swiftui
struct ChoiceButton: View {
var caption: String
let alphabet = ["a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x",
"... | {
"domain": "codereview.stackexchange",
"id": 45361,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "algorithm, swift, ios, caesar-cipher, swiftui",
"url": null
} |
algorithm, swift, ios, caesar-cipher, swiftui
which makes things much clearer and simpler. It also allows to add unit test for the encryption function.
Even better, this refactoring makes the ChoiceButton obsolete: we can replace
ChoiceButton(caption: "Encrypt",
input: $input,
output: $output... | {
"domain": "codereview.stackexchange",
"id": 45361,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "algorithm, swift, ios, caesar-cipher, swiftui",
"url": null
} |
algorithm, swift, ios, caesar-cipher, swiftui
The .disabled(input.isEmpty) replaces your test for an empty input field. In addition, it makes it visible to the user whether the buttons are active or not.
The encryption code itself can also be simplified a bit. If we declare alphabet as an array of Character then alph... | {
"domain": "codereview.stackexchange",
"id": 45361,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "algorithm, swift, ios, caesar-cipher, swiftui",
"url": null
} |
c++, template-meta-programming
Title: (C++) Template code for adding tagging types to add additional context
Question: Below is a piece of template code which allows for adding a Tag to structure or class types.
#include <type_traits>
#include <concepts>
//Source: https://stackoverflow.com/questions/31171682/type-tr... | {
"domain": "codereview.stackexchange",
"id": 45362,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, template-meta-programming",
"url": null
} |
c++, template-meta-programming
For example this could be used in the context of rendering where a point can be specified in world, screen, uv, camera, etc coordinates.
Example:
template< typename T >
struct WorldSpace: TagProxy< WorldSpace, T > {};
template< typename T >
struct ScreenSpace: TagProxy< ScreenSpace, T >... | {
"domain": "codereview.stackexchange",
"id": 45362,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, template-meta-programming",
"url": null
} |
c++, template-meta-programming
But while you went to great lengths to copy the cv qualifiers, the resulting tagged_point is not const. This is because cv qualifiers don't really make sense when you return something by value; after all, the caller can always copy them into some variable with different qualifications.
T... | {
"domain": "codereview.stackexchange",
"id": 45362,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, template-meta-programming",
"url": null
} |
c++, template-meta-programming
But even better, make it look more like the standard library, and consider naming it make_tagged<>() and tag_cast<>().
Avoid unnecessary moves and copies
While your code will move objects where possible, a move still might have some overhead. It also requires that the object is movable. ... | {
"domain": "codereview.stackexchange",
"id": 45362,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, template-meta-programming",
"url": null
} |
c++, template-meta-programming
Just compile it and look at the assembly output! Note that while the size of the tagged object is the same as the untagged object, there might be subtle issues that cause differences in performance. For example, depending on the inner object, the compiler might not be able to optimize aw... | {
"domain": "codereview.stackexchange",
"id": 45362,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, template-meta-programming",
"url": null
} |
python, datetime, comparative-review, time
Title: Subtract two lap times (m:ss:fff)
Question: I have two lap time strings I need to subtract, the format is minutes, seconds, milliseconds. This is to compare a new world record time to the previous holder's time. The result will always be positive.
Example input:
x = "... | {
"domain": "codereview.stackexchange",
"id": 45363,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, datetime, comparative-review, time",
"url": null
} |
python, datetime, comparative-review, time
2nd solution:
from datetime import datetime, time, timedelta
# Takes two m:ss.fff time strings
# Example: 1:
def getTimeDifference(t1, t2):
wrTime = convertTimeString(t1)
time1 = timedelta(
minutes=wrTime["minutes"],
seconds=wrTime["seconds"],
... | {
"domain": "codereview.stackexchange",
"id": 45363,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, datetime, comparative-review, time",
"url": null
} |
python, datetime, comparative-review, time
return converted
x = "1:09.201"
y = "0:57.199"
print(getTimeDifference(y, x))
```
Answer: Figure out what your requirements are; your two variations actually perform different operations: the first one returns the difference as a float of seconds, the second one as a format... | {
"domain": "codereview.stackexchange",
"id": 45363,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, datetime, comparative-review, time",
"url": null
} |
python, datetime, comparative-review, time
if __name__ == '__main__':
print(format_difference(get_time_difference('1:09.201', '0:57.199')))
Edit: as pointed out in the comments, formatting can be simplified a lot by using the total_seconds method of the timedelta class:
def format_difference(difference: timedelta... | {
"domain": "codereview.stackexchange",
"id": 45363,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python, datetime, comparative-review, time",
"url": null
} |
java, stream, lambda
Title: Find all line numbers of prefix duplicates with streams
Question: All line indices of lines from a text that begin with the same line prefix should be found. The prefixes and the corresponding line numbers should be returned. Streams and lambdas should be used as techniques if possible.
Th... | {
"domain": "codereview.stackexchange",
"id": 45364,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, stream, lambda",
"url": null
} |
java, stream, lambda
System.out.println(duplicates); // print all duplicates line numbers
}
Could this code be simplified?
Answer: First off, the description of your code is quite unclear and leaves things out. In your comment you mention "at least 3 characters followed by an underscore", however that is not wha... | {
"domain": "codereview.stackexchange",
"id": 45364,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, stream, lambda",
"url": null
} |
java, stream, lambda
System.out.println(findDuplicatePrefixes(s.lines().toList()));
}
private static record Line(int lineNumber, String text) {}
private static Map<String, List<Integer>> findDuplicatePrefixes(List<String> lines) {
return IntStream.range(0, lines.size())
.mapToObj(i -> new Line(i + ... | {
"domain": "codereview.stackexchange",
"id": 45364,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "java, stream, lambda",
"url": null
} |
c#, enum
Title: BOOL×BOOL→ENUM mapping
Question: Does this part look clean enough? Any suggestions on how to make it cleaner?
if (isCheck)
{
if (isStuck)
{
return GameState.Mate;
}
else
{
return GameState.Check;
}
}
else
{
if (isStuck)
{
return GameState.Stalema... | {
"domain": "codereview.stackexchange",
"id": 45365,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#, enum",
"url": null
} |
c++, strings, reinventing-the-wheel, floating-point
Title: Calculate the maximum number of decimal places with which all floating point numbers can be represented in a range in C++
Question: I'm looking for a function in C++ that can determine, how accurate all floating point numbers in a given range can be represent... | {
"domain": "codereview.stackexchange",
"id": 45366,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, reinventing-the-wheel, floating-point",
"url": null
} |
c++, strings, reinventing-the-wheel, floating-point
int main(int argc, char const *argv[])
{
// Some examples:
std::cout << get_max_precision_in_range_for_float(1, 2) << " digits accuracy\n";
std::cout << get_max_precision_in_range_for_float(7, 8) << " digits accuracy\n";
std::cout << get_max_precision... | {
"domain": "codereview.stackexchange",
"id": 45366,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, reinventing-the-wheel, floating-point",
"url": null
} |
c++, strings, reinventing-the-wheel, floating-point
Also consider that there are several ways to convert floating point values to strings. Instead of hardcoding which method to use, you can let the caller pass in the conversion function they want tested, or use a default function if they don't provide anything. It cou... | {
"domain": "codereview.stackexchange",
"id": 45366,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, reinventing-the-wheel, floating-point",
"url": null
} |
python
Title: OpenAI API: Return structured data from text input
Question: Given a txt input which represents extracted resume (or “CV” outside of the US) data, pass the text to an AI model and return a JSON version of the original input.
I have created the below script, which tries to prompt gpt3-turbo in order to p... | {
"domain": "codereview.stackexchange",
"id": 45367,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
Work Experience
Sep-07 – Aug-10
Official Company Name
Job title
Please use 3-4 bullets maximum to describe your job function &
City, Country
responsibilities
Concentrate on your achievements, and what you have distinctly
contributed to in each role, using quantitative examples where possible
Examples ... | {
"domain": "codereview.stackexchange",
"id": 45367,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
Achievements:
List academic or other achievements here, for example
First Class Honours, Previous University
Study abroad scholarship (selected 3 out of 600 students)
Principal Cellist of London Youth Orchestra
Nationality:
your nationality, dual nationality, and any additional work authorization if
applicable... | {
"domain": "codereview.stackexchange",
"id": 45367,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
def parse_cv(client: OpenAI) -> ChatCompletion | Stream[ChatCompletionChunk]:
conversation = [
{
"role": "system",
"content": (
"You are a sophisticated resume parser. "
"Your task is to analyze, extract, and structure data from resumes. "
... | {
"domain": "codereview.stackexchange",
"id": 45367,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
if __name__ == "__main__":
result = gpt_verify()
str_result = result.choices[0].message.content
json_result = json.loads(str_result)
print(json_result) | {
"domain": "codereview.stackexchange",
"id": 45367,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
Current Output
{
"name": {
"forename": "Forename",
"surname": "SURNAME",
"confidence": 0.9
},
"contact_information": {
"email": "professional email address",
"phone": "UK landline or mobile",
"confidence": 0.8
},
"education": {
"items": [
{
"start_date": "2000... | {
"domain": "codereview.stackexchange",
"id": 45367,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
],
"location": "City, Country",
"confidence": 0.8
},
{
"start_date": "Mar-04",
"end_date": "Jun-05",
"company": "Official Company Name",
"job_title": "Job title",
"responsibilities": [
"Try to ensure your CV is easy to scan, start bul... | {
"domain": "codereview.stackexchange",
"id": 45367,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
"languages": "languages other than English and ability level eg. German (fluent)",
"confidence": 0.7
}
} | {
"domain": "codereview.stackexchange",
"id": 45367,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
Answer: Current code
The parse_cv function should already take the CV text as an input argument
There should be no variables declared in the global scope to avoid redeclarations and conflicts: everything
under if __name__ == __main__: should be moved to a def main() -> None: function
Data validation
With your... | {
"domain": "codereview.stackexchange",
"id": 45367,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
class Candidate(BaseModel):
name: Name
contact_information: ContactInformation
education: Sequence[EducationItem]
work_experience: Sequence[WorkExperienceItem]
additional_information: AdditionalInformation | {
"domain": "codereview.stackexchange",
"id": 45367,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
Some fields can be defined as optional to make the validation more flexible to missing information in the resume,
and adding Field descriptions and docstrings might help ChatGPT for the data extraction.
The Candidate model can be instantiated with the following JSON (slightly modified from your version):
{
... | {
"domain": "codereview.stackexchange",
"id": 45367,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
python
def parse_cv(cv_text: str) -> str:
candidate_def = Path("candidate.py").read_text(encoding="utf-8")
# candidate_def can be injected in the prompt
...
return json_result
def main() -> None:
cv_text = ...
json_result: str = parse_cv(cv_text)
candidate = Candidate(**json.loads(json_res... | {
"domain": "codereview.stackexchange",
"id": 45367,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "python",
"url": null
} |
c++, beginner, rock-paper-scissors
Title: Beginner Rock-Paper-Scissors game
Question: As a beginner, this is my second game in C++. It is a very simple rock-paper-scissors game. User or computer wins the game after a given number of rock,paper or scissors matches. I'd appreciate any comment improving or criticising ... | {
"domain": "codereview.stackexchange",
"id": 45368,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, beginner, rock-paper-scissors",
"url": null
} |
c++, beginner, rock-paper-scissors
return 0;
}
// ask for user input
char getUserInput()
{
char userInput{' '};
std::cout << "Rock, paper or scissors? (r-p-s): \n";
std::cin >> userInput;
if (userInput != 'r' && userInput != 'p' && userInput != 's')
{
std::cout << "Please enter a valid cha... | {
"domain": "codereview.stackexchange",
"id": 45368,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, beginner, rock-paper-scissors",
"url": null
} |
c++, beginner, rock-paper-scissors
This will make the code easier to read. Also this matches up to zero/one/two so it makes accessing arrays easier (see below).
Codereview
This looks like a C interface. Did you not want to write a C++ application?
char getUserInput();
char getCompInput();
void showValue(char x);
void ... | {
"domain": "codereview.stackexchange",
"id": 45368,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, beginner, rock-paper-scissors",
"url": null
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.