text stringlengths 1 2.12k | source dict |
|---|---|
java, programming-challenge
break;
case 1:
top = 0;
break;
default:
Arrays.fill(candidates[rank], offsets[rank], k, nonTopT);
keep(rank+1, offsets[rank]);
top = k_;
}
candidates[t_][k_t+rank]... | {
"domain": "codereview.stackexchange",
"id": 45340,
"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, programming-challenge",
"url": null
} |
java, programming-challenge
public static void main(String[] args) {
TopByOrderK<String> topper = new TopByOrderK<>(3, 5, null);
String many[] = {
"for", "int", "offset", "n", "k", "order", "apply", "many",
// "runnersUp", "top", "candidates",
// "offsets", "tr... | {
"domain": "codereview.stackexchange",
"id": 45340,
"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, programming-challenge",
"url": null
} |
java, programming-challenge
meaningful identifier
int down = k_ - keep;
The meaning of the index down at this point is obscure.
Add a // comment, or choose a more informative name.
In general, I'm looking for a loop variant in the keep() routine.
The promise was to "distribute" values.
There's no assert at th... | {
"domain": "codereview.stackexchange",
"id": 45340,
"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, programming-challenge",
"url": null
} |
java, programming-challenge
Up in the ctor you told us that top is a synonym for t,
and indeed we might mentally pronounce the latter as "top".
But here we're using same identifier in a different scope
to represent a different concept.
Choose a new name for the new concept.
comments lie!
This is just wrong.
/** pl... | {
"domain": "codereview.stackexchange",
"id": 45340,
"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, programming-challenge",
"url": null
} |
c++, image, template, classes, variadic
Title: Multi-dimensional Image Data Structure with Variadic Template Functions in C++
Question: This is a follow-up question for Three dimensional data structure in C++. I am trying to implement multi-dimensional image data structure with variadic template functions. For exampl... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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(const std::size_t a, const std::size_t b, const std::size_t c, const std::size_t d, const std::size_t e):
image_data(a * b * c * d * e)
{
size.reserve(5);
size.emplace_back(a);
size.emplace_back(b);
... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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": 45341,
"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 const& at(const Args... indexInput) const
{
checkBoundary(indexInput...);
constexpr std::size_t n = sizeof...(Args);
if(n != size.size())
{
throw std::ru... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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 getDimensionality() const noexcept
{
return size.size();
}
constexpr std::size_t getWidth() const noexcept
{
return size[0];
}
constexpr std::size_t getHeight() const noexcept
... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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": 45341,
"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": 45341,
"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": 45341,
"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>
void checkBoundary(const Args... indexInput) const
{
constexpr std::size_t n = sizeof...(Args);
if(n != size.size())
{
throw std::runtime_error("Dimensionality mismatched!");
... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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
throw std::out_of_range("Index out of range!");
if (get_from_variadic_template<2>(indexInput...) >= size[1])
throw std::out_of_range("Index out of range!");
if (get_from_variadic_template<3>(indexInput...) >= size[2])
... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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 T, typename ElementT>
concept is_Image = std::is_same_v<T, Image<ElementT>>;
}
Full Testing Code
The full testing code:
// Three dimensional data structure in C++
// Developed by Jimmy Hu
#include <algorithm>
#include <cassert> // for assert
#incl... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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::size_t index, typename... Ts>
using get_from_variadic_template_t = typename get_from_variadic_template_struct<index, Ts...>::type;
}
// image.h
namespace TinyDIP
{
template <typename ElementT>
class Image
{
public:
Image() = default;
... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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(const std::vector<ElementT>& input, std::size_t newWidth, std::size_t newHeight)
{
size.reserve(2);
size.emplace_back(newWidth);
size.emplace_back(newHeight);
if (input.size() != newWidth * newHeight)
{
... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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": 45341,
"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 const& at(const Args... indexInput) const
{
checkBoundary(indexInput...);
constexpr std::size_t n = sizeof...(Args);
if(n != size.size())
{
throw std::ru... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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 getDimensionality() const noexcept
{
return size.size();
} | {
"domain": "codereview.stackexchange",
"id": 45341,
"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 getWidth() const noexcept
{
return size[0];
}
constexpr std::size_t getHeight() const noexcept
{
return size[1];
}
constexpr auto getSize() noexcept
{
return size;... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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": 45341,
"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": 45341,
"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": 45341,
"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>
void checkBoundary(const Args... indexInput) const
{
constexpr std::size_t n = sizeof...(Args);
if(n != size.size())
{
throw std::runtime_error("Dimensionality mismatched!");
... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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
throw std::out_of_range("Index out of range!");
if (get_from_variadic_template<2>(indexInput...) >= size[1])
throw std::out_of_range("Index out of range!");
if (get_from_variadic_template<3>(indexInput...) >= size[2])
... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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 T, typename ElementT>
concept is_Image = std::is_same_v<T, Image<ElementT>>;
}
namespace TinyDIP
{
template<typename ElementT>
constexpr bool is_width_same(const Image<ElementT>& x, const Image<ElementT>& y)
{
return x.getWidth() == y.g... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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_height_same(const Image<ElementT>& x, const Image<ElementT>& y, const Image<ElementT>& z)
{
assert(is_height_same(x, y, z));
}
template<typename ElementT>
constexpr void assert_size_same(const Image<E... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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
int main()
{
auto start = std::chrono::system_clock::now();
multidimensionalImageTest<double>();
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::time_t end_time = std::chrono::system_clock::to_tim... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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
Now you can also see that you can make a generic one that takes a vector of elements of arbitrary dimensions as well. And if we want to be really generic, allow any range of elements:
template<std::ranges::input_range Range,
std::same_as<std::size_t>... Sizes>
Image(con... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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
Then you can also use concepts to restrict the number of arguments allowed:
template<std::same_as<std::size_t>... Sizes>
requires (sizeof...(Sizes) == Rank)
Image(Sizes... sizes): size{sizes...}, image_data((1 * ... * sizes))
{}
And to avoid having to explicitly specify the num... | {
"domain": "codereview.stackexchange",
"id": 45341,
"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++
Title: Checkup for a block of memory, containing a repeated single byte
Question: I was looking at this question on SO, because I needed something similar. There are eight answers to it, and I wasn't satisfied with any of them - so I've written my own solution (please see below). It's in C++, so I decided not to ... | {
"domain": "codereview.stackexchange",
"id": 45342,
"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++",
"url": null
} |
c++
Answer: This doesn't account for alignment
Consider that PTR might not be aligned correctly for a T. You have to account for this first, for example by comparing the first few bytes individually until PTR is suitably aligned.
Depending on T and your CPU architecture, misaligned reads might either work fine, work b... | {
"domain": "codereview.stackexchange",
"id": 45342,
"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++",
"url": null
} |
python, programming-challenge, string-processing
Title: Advent of Code 2023 - Day 9: Mirage Maintenance
Question: Part 1:
The task involves analyzing an environmental report from an oasis using the
Oasis And Sand Instability Sensor (OASIS). The report consists of multiple
histories, each containing a sequence of valu... | {
"domain": "codereview.stackexchange",
"id": 45343,
"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, programming-challenge, string-processing",
"url": null
} |
python, programming-challenge, string-processing
0 3 6 9 12 15 18
3 3 3 3 3 3
0 0 0 0 0
So, the next value of the first history is 18.
#!/usr/bin/env python3
from pathlib import Path
from typing import Iterable
import typer
def find_next_val(line: str) -> int:
# N1 N2 N3 N4 ..... | {
"domain": "codereview.stackexchange",
"id": 45343,
"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, programming-challenge, string-processing",
"url": null
} |
python, programming-challenge, string-processing
def find_next_val(line: str) -> int:
# N1 N2 N3 N4 ...
seqs = [list(map(int, line.split()))]
while True:
seqs.append(
[-1 * (seqs[-1][i] - seqs[-1][i + 1]) for i in range(len(seqs[-1]) - 1)]
)
if all(val == 0 for val in ... | {
"domain": "codereview.stackexchange",
"id": 45343,
"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, programming-challenge, string-processing",
"url": null
} |
python, programming-challenge, string-processing
But there is no need for this calculation to update the seq list. These statements can be simplified to:
last = 0
for seq in reversed(seqs[:-1]):
last += seq[-1]
return last
Refactor Your Code to Handle Both Parts
The find_next_val implementations f... | {
"domain": "codereview.stackexchange",
"id": 45343,
"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, programming-challenge, string-processing",
"url": null
} |
python, python-3.x
Title: TUI Slot Simulator
Question: Wrote a slot machine for a larger casino TUI project I'm creating. Would like some general feedback, along with some specific things:
Coloring: Currently I have three loops that are almost identical, only differing in the index where they stop. Is there an easie... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
class Slot:
def __init__(self, options: dict[str, list[float]], dim: tuple[int, int], player: Player) -> None:
self.options = options
self.w, self.h = dim
self.player = player
self.result = [[''] * self.w] * self.h
self.total_winnings = 0.0
self.r... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
def __check_lines(self) -> float:
win = 0.0
for idx, line in enumerate(self.lines):
front = line[0]
raw_line = self.rawlines[idx]
if len(set(line)) == 1: # all
win += self.bet * self.options[front][0]
for idx, val in... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
if __name__ == '__main__':
main()
Answer: Questions
Currently I have three loops that are almost identical, only differing in the index where they stop. Is there an easier way to do this?
Extract the stop-index as a variable, and then issue a single call to colored.
Did my best to separate s... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
Don't accept width and height. Your rawlines (which I call win_database and your reference calls win lines) should imply the dimensions.
The good
You've written some type hints (you could use more).
You've managed to avoid globals.
You have a properly-written main function and __main__ guard.
You've... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
if termcolor.termcolor._can_do_colour():
yield termcolor.colored(text=f' {symbol} ', color='green')
else:
yield f'[{symbol}]' | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
Suggested
Demonstrating some of the above with less mutation, a little bit more OOP, etc.: this behaves identically to your old code. It took a lot of effort to figure out how the old code worked, and I suspect that there's still a bunch of simplification to the win-condition algorithm that can be d... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
symbols: SymbolRow
win_class: WinClass
@classmethod
def map(cls, symbols: Iterable[Symbol]) -> 'WinCandidate':
"""
From a mapped symbol row, create a WinCandidate instance.
"""
symbols = tuple(symbols)
# Business logic used to determine the win-c... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
def _get_winnings(
self,
win_coefs_by_symbol: dict[Symbol, CoefRow],
) -> Iterator[float]:
"""
An iterator of winning money. Call sum() on this to get the total winnings for this spin.
"""
for candidate in self.candidates:
coef = win_co... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
def format_lines(self, player: Player) -> Iterator[str]:
sep = '|---'*self.width + '|'
yield sep
for y, result_row in enumerate(self.result):
middle = '|'.join(self._format_row(y=y, result_row=result_row))
yield f'|{middle}|'
yield sep
... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
spin = Spin(
win_coefs_by_symbol=self.win_coefs_by_symbol,
win_database=self.win_database,
result=tuple(self._spin_rows()),
bet=bet,
)
self.total_winnings += spin.winnings
player.win(spin.winnings)
return spin
def _... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
while True:
try:
spin = machine.spin(player=player, bet=1.00)
except InsufficientFundsError:
if visible:
print('Insufficient funds')
break
output = '\n'.join(spin.format_lines(player=player))
if visible:
... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
|---|---|---|---|---|
| 7 | 9 | Q | 9 | 7 |
| 7 | 8 | A | 9 | Q |
| 9 | 7 | K | 8 | A |
|---|---|---|---|---|
Balance: $43.20 | Bet: $1.00 | Win: $0.00
Total Winnings: $0.20
|---|---|---|---|---|
| K | 8 | K | 8 | A |
| A | 8 | A | 9 | 7 |
| 9 | 8 | A | 9 | 9 |
|---|---|---|---|---|
Balance: $42.20... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
|---|---|---|---|---|
| A | 8 | 8 | 9 | 8 |
| 7 | 9 | Q | 9 | K |
| 9 | A | A | Q | 8 |
|---|---|---|---|---|
Balance: $32.20 | Bet: $1.00 | Win: $0.00
Total Winnings: $0.20
|---|---|---|---|---|
| 8 | Q | 9 | A | A |
| 7 | K | Q | 9 | K |
| Q | K | A | K | 9 |
|---|---|---|---|---|
Balance: $31.20... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
|---|---|---|---|---|
| Q | 7 | 8 | 8 | A |
| 7 | Q | 9 | Q | A |
| Q | A | A | 9 | 9 |
|---|---|---|---|---|
Balance: $22.38 | Bet: $1.00 | Win: $0.00
Total Winnings: $1.38
|---|---|---|---|---|
| A | 9 | Q | Q | 8 |
| Q | 7 | 9 | Q | 9 |
| Q | A | 8 | 9 | 8 |
|---|---|---|---|---|
Balance: $21.38... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
|---|---|---|---|---|
| 9 | A | K | 8 | 9 |
| 9 | A | 8 | Q | 8 |
| 8 | K | 8 | K | 7 |
|---|---|---|---|---|
Balance: $16.28 | Bet: $1.00 | Win: $0.00
Total Winnings: $6.28
|---|---|---|---|---|
| A | 7 | A | K | 9 |
| 9 | Q | A | K | K |
| 8 | 7 | 9 | A | 8 |
|---|---|---|---|---|
Balance: $15.28... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
|---|---|---|---|---|
| K | Q | 8 | A | 7 |
| A | Q | 8 | A | 8 |
| 7 | Q | A | A | A |
|---|---|---|---|---|
Balance: $6.12 | Bet: $1.00 | Win: $0.00
Total Winnings: $7.12
|---|---|---|---|---|
| 8 | Q | 7 | Q | A |
| Q | 8 | 8 | A | K |
| Q | A | 7 | 7 | K |
|---|---|---|---|---|
Balance: $5.12 |... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
python, python-3.x
Insufficient funds
Profiling
This profile is after fixing up the low-hanging fruit on WinCandidate.win_class. There are other optimisations possible, especially if this is intended for headless simulation.
5538495 function calls (5464053 primitive calls) in 2.292 seconds
Ordered by: in... | {
"domain": "codereview.stackexchange",
"id": 45344,
"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, python-3.x",
"url": null
} |
c++, functional-programming, c++20
Title: Is it valid to pass struct through std::fold_left to persist state?
Question: I am exploring functional programming design with C++. The code below uses a struct passed to ranges::fold_left for processing. The final result is returned in the struct.
**Updated to remove a toy ... | {
"domain": "codereview.stackexchange",
"id": 45345,
"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++, functional-programming, c++20",
"url": null
} |
c++, functional-programming, c++20
auto out_sd = [&](char const tag, LineTotal s_d) {
auto [total, top, bot] = s_d;
std::cout << tag << '\t' << total << '\t' //
<< top.digits_sum << '\t' << top.have_symbol << '\t'
<< bot.digits_sum << '\t' << bot.have... | {
"domain": "codereview.stackexchange",
"id": 45345,
"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++, functional-programming, c++20",
"url": null
} |
c++, functional-programming, c++20
bottom = {calc_value(bottom.digits_sum, bot_char),
bottom.have_symbol or ispunct(bot_char)};
}
return sum_data;
};
// process two lines of aoc_data
return sum + fold_left(lines, LineTotal { }, e... | {
"domain": "codereview.stackexchange",
"id": 45345,
"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++, functional-programming, c++20",
"url": null
} |
c++, functional-programming, c++20
Similarly, you define lambdas that capture by reference, so they technically are functions with side-effects, again that's also not very FP.
Even just adding something to an existing variable can be considered to be non-functional. In functional programming languages you then have to... | {
"domain": "codereview.stackexchange",
"id": 45345,
"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++, functional-programming, c++20",
"url": null
} |
c++, functional-programming, c++20
But why use a lambda at all? You can just write:
// Do some stuff
…
return sum + fold_left(lines, LineTotal { }, extract_values).total;
Naming things
I have a hard time understanding what the code is supposed to do. I'm guessing it's one of the Advent of Code problems. But which day... | {
"domain": "codereview.stackexchange",
"id": 45345,
"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++, functional-programming, c++20",
"url": null
} |
c, console, game-of-life
Title: Conway's Game of Life Terminal Visualization in C
Question: I'm currently learning C, and decided to practice what I've been working on with a project, namely implementing Conway's Game of Life as a terminal visualization. This is my first project in C.
The result:
and my code:
#inclu... | {
"domain": "codereview.stackexchange",
"id": 45346,
"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, console, game-of-life",
"url": null
} |
c, console, game-of-life
void print_grid() {
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
if (grid[y][x] == alive) {
move_to(y, x);
int n = count_neighbors(y, x);
if (n < 2) {
// underpopulation
... | {
"domain": "codereview.stackexchange",
"id": 45346,
"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, console, game-of-life",
"url": null
} |
c, console, game-of-life
I'd like some feedback/suggestions on how I can improve it.
The main issues I can identify are:
Blinking (probably due to the amount of printf statements I am making)
Lack of proper program termination
Possibly leaking memory as a result of problem 2
Usage of preprocessor directives (DEAD isn... | {
"domain": "codereview.stackexchange",
"id": 45346,
"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, console, game-of-life",
"url": null
} |
c, console, game-of-life
These are all good things that you should definitely strive to continue.
Possible improvements
These are some suggestions for how you might improve this program.
Gracefully exit the program
You're right that the atexit functions are not called because there's no way to gracefully exit the prog... | {
"domain": "codereview.stackexchange",
"id": 45346,
"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, console, game-of-life",
"url": null
} |
c, console, game-of-life
Tell the compiler when no arguments are expected
For most of these functions that don't take arguments, instead of writing void next() write void next(void). Otherwise the compiler will accept any number of arguments to that function, and that's probably not what you want.
Avoid using global ... | {
"domain": "codereview.stackexchange",
"id": 45346,
"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, console, game-of-life",
"url": null
} |
c, console, game-of-life
Add a bit of variation
By design, rand() starts with a seed value of 0 unless otherwise told, which means that you will get the same pattern every time the program is run. To make it different, but controllable by the user, you could seed with a user-supplied number by modifying the first par... | {
"domain": "codereview.stackexchange",
"id": 45346,
"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, console, game-of-life",
"url": null
} |
c, console, game-of-life
Use more modern C
If you are using a compiler that supports at least C11, there are a lot of improvements that you can make without sacrificing readability. Step by step, here are the changes.
Use structures to group data
The height and width and the two cell arrays are central to this progra... | {
"domain": "codereview.stackexchange",
"id": 45346,
"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, console, game-of-life",
"url": null
} |
c, console, game-of-life
First, unless you are using a C23 compiler, which has it as a keyword, you'll need to #include <stdbool.h> to get bool.
This line uses syntax that is unfamiliar even to many C programmers, so I'll explain it in some detail:
cell_t (*pGrid)[2][grid->dim.height][grid->dim.width] = malloc(sizeof(... | {
"domain": "codereview.stackexchange",
"id": 45346,
"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, console, game-of-life",
"url": null
} |
c, console, game-of-life
We could alternatively use a pointer and simply increment it to the end of the array, but this way shows the intent a bit more clearly and we're not that concerned about performance here, since it's only done once.
Pass pointers to avoid global variables
Using this same syntax as described abo... | {
"domain": "codereview.stackexchange",
"id": 45346,
"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, console, game-of-life",
"url": null
} |
c, console, game-of-life
return count;
}
Here the [static restrict 1] means that the pointer, grid in this instance, must have at least one valid instance (static) and further that it must have exactly that many (restrict). This can help the compiler generate optimal code and clearly signals the intent to a p... | {
"domain": "codereview.stackexchange",
"id": 45346,
"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, console, game-of-life",
"url": null
} |
beginner, c, bitwise
Title: Reading/writing single bits from/to FILE
Question: I'm implementing a Huffman encoding/decoding program and I need a way to deal with single bits.
I tried to keep things as simple as possible, I "pack" bits inside a single byte and I have only one index to worry about (of course this is a... | {
"domain": "codereview.stackexchange",
"id": 45347,
"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": "beginner, c, bitwise",
"url": null
} |
beginner, c, bitwise
/**
* bitbuf_flush: flushes the buffer (a byte) to the underlying FILE pointer and
* returns true.
*
* If the buffer is not full, it will be written "0 padded".
* If the buffer is empty, nothing will be written and true will
* be r... | {
"domain": "codereview.stackexchange",
"id": 45347,
"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": "beginner, c, bitwise",
"url": null
} |
beginner, c, bitwise
bool bitbuf_write_bit(struct bitbuf *bw, bool bit)
{
assert(bw->is_writer);
if (bw->idx == BITS_IN_BUF && ! bitbuf_flush(bw)) {
return false;
}
if (bit) {
bw->buf |= (0x80 >> bw->idx); /* 0x80 is 0b10000000 */
}
... | {
"domain": "codereview.stackexchange",
"id": 45347,
"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": "beginner, c, bitwise",
"url": null
} |
beginner, c, bitwise
int main(void)
{
FILE *fp = fopen("test_sequence_file", "w");
if (fp == NULL) {
perror("Error");
return 1;
}
struct bitbuf *bw = bitbuf_new_bit_writer(fp);
test_write_bit(bw, N);
bitbuf_free(bw);
freopen(NULL... | {
"domain": "codereview.stackexchange",
"id": 45347,
"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": "beginner, c, bitwise",
"url": null
} |
beginner, c, bitwise
bitbuf_close(br);
bitbuf_close(bw);
}
Here some testing output:
bitbuf$ cc -std=c11 -Wall -Wextra -Werror -c -o bitbuf.o bitbuf.c
bitbuf$ cc -std=c11 -Wall -Wextra -Werror -o test_sequence bitbuf.o test_sequence.c
bitbuf$ cc -std=c11 -Wall -Wextra -Werror -o test_copy bitbuf.o test_copy.... | {
"domain": "codereview.stackexchange",
"id": 45347,
"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": "beginner, c, bitwise",
"url": null
} |
beginner, c, bitwise
Answer: For a beginner, this is pretty decent! Here are some things that could be improved.
Naming
Many of your variable names are 2 or 3 characters long. That's difficult to read and understand. I recommend using longer names that are descriptive. fp could be file; buf should be buffer or better ... | {
"domain": "codereview.stackexchange",
"id": 45347,
"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": "beginner, c, bitwise",
"url": null
} |
beginner, c, bitwise
Performance
You mention "this is a bit inefficient". I recommend reading in a larger buffer of around 1-4k and getting bits out of that. You should probably profile to figure out the ideal size, but that's probably a good place to start. Having 1 extra index will only add 4-8 bytes to the structur... | {
"domain": "codereview.stackexchange",
"id": 45347,
"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": "beginner, c, bitwise",
"url": null
} |
javascript, react.js, typescript
Title: How to encapsulated javascript code using imutable method?
Question: I have this code, but I need to return pure functions instead the mutating functions.
Is it possible? Perhaps using OOP? I don't know how to improve it.
I tried to return the values in each function, but I hav... | {
"domain": "codereview.stackexchange",
"id": 45348,
"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": "javascript, react.js, typescript",
"url": null
} |
javascript, react.js, typescript
if (operationType) {
updatePendenciesCount(pendenciesCount, status, operationType)
}
}
processOverviewPendencies(pendencies, pendenciesCount, countedIds, id)
}
}
const processOverviewData = (
data: OverviewModelType[],
groupBy: GroupFiltersType,
statusCou... | {
"domain": "codereview.stackexchange",
"id": 45348,
"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": "javascript, react.js, typescript",
"url": null
} |
javascript, react.js, typescript
Use Array.flatMap to un-nest the tree.
Use lodash uniqBy (or write your own) on the flattened tree to get unique pendency items before counting them
Use lodash countBy (or write your own) to count by status
Replace if (pendencies?.length) with pendencies ?? [] to reduce branching (and ... | {
"domain": "codereview.stackexchange",
"id": 45348,
"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": "javascript, react.js, typescript",
"url": null
} |
javascript, react.js, typescript
You can then map to a common structure for all your arrays:
const pendencyCountItems = [
...overviews.filter(o => o.operationType).map(o => ({ key: o.status, subKey: o.operationType })),
...components.filter(o => o.operationType).map(o => ({ key: o.status, subKey: o.operationTy... | {
"domain": "codereview.stackexchange",
"id": 45348,
"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": "javascript, react.js, typescript",
"url": null
} |
python, programming-challenge, string-processing
Title: Advent of Code 2023 - Day 15: Lens Library
Question: Part 1:
The task involves initializing the Lava Production Facility using an
initialization sequence. The sequence consists of steps, each requiring the
application of the Holiday ASCII String Helper algorithm... | {
"domain": "codereview.stackexchange",
"id": 45349,
"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, programming-challenge, string-processing",
"url": null
} |
python, programming-challenge, string-processing
def total_sum(lines: Iterable[str]) -> int:
return sum(map(hash, lines))
def main(initialization_sequence: Path) -> None:
"""Solves Part 1 of Day 15 Advent of Code.
See: https://adventofcode.com/2023/day/15"""
with op... | {
"domain": "codereview.stackexchange",
"id": 45349,
"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, programming-challenge, string-processing",
"url": null
} |
python, programming-challenge, string-processing
Inside each box, there are several lens slots that will keep a lens correctly positioned to focus light passing through the box. The side of each box has a panel that opens to allow you to insert or remove lenses as necessary.
Along the wall running parallel to the boxe... | {
"domain": "codereview.stackexchange",
"id": 45349,
"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, programming-challenge, string-processing",
"url": null
} |
python, programming-challenge, string-processing
If there is not already a lens in the box with the same label, add the lens to the box immediately behind any lenses already in the box. Don't move any of the other lenses when you do this. If there aren't any lenses in the box, the new lens goes all the way to the fron... | {
"domain": "codereview.stackexchange",
"id": 45349,
"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, programming-challenge, string-processing",
"url": null
} |
python, programming-challenge, string-processing
After "rn=1":
Box 0: [rn 1]
After "cm-":
Box 0: [rn 1]
After "qp=3":
Box 0: [rn 1]
Box 1: [qp 3]
After "cm=2":
Box 0: [rn 1] [cm 2]
Box 1: [qp 3]
After "qp-":
Box 0: [rn 1] [cm 2]
After "pc=4":
Box 0: [rn 1] [cm 2]
Box 3: [pc 4]
After "ot=9":
Box 0: [rn 1] [cm 2]
... | {
"domain": "codereview.stackexchange",
"id": 45349,
"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, programming-challenge, string-processing",
"url": null
} |
python, programming-challenge, string-processing
# Loop over a copy to avoid modifying the list whilst iterating over it.
for slot in boxes[box_idx][:]:
if label == slot[0]:
boxes[box_idx].remove(slot)
break
def add_slot(csv: str, boxes: list[list[list[str | int]]]) -> None:
# ... | {
"domain": "codereview.stackexchange",
"id": 45349,
"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, programming-challenge, string-processing",
"url": null
} |
python, programming-challenge, string-processing
Answer: Part 1
Your code produces the correct answers, so well done! Consequently, I will be commenting mostly on style and choice of names for variables and functions.
As I mentioned in a comment to your post, you should avoid assigning names to variables and (especial... | {
"domain": "codereview.stackexchange",
"id": 45349,
"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, programming-challenge, string-processing",
"url": null
} |
python, programming-challenge, string-processing
The way I interpret this description is that function total_hash is really the function to be called to solve the problem and it is passed the initialization sequence, which is a comma-separated list of steps, i.e. a string. That this string is coming from an input file... | {
"domain": "codereview.stackexchange",
"id": 45349,
"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, programming-challenge, string-processing",
"url": null
} |
python, programming-challenge, string-processing
typer.run(main)
Part 2
Comments I made about using safer and more descriptive function and variable names apply here.
What Is The Optimal Functional Decomposition?
I don't know if there is an optimal; it's a balance between maintainability and readability, which are no... | {
"domain": "codereview.stackexchange",
"id": 45349,
"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, programming-challenge, string-processing",
"url": null
} |
python, programming-challenge, string-processing
But csv is terminated by the minus sign. Simpler (and better) would be:
label = csv[:-1]
Your boxes variable is a list of lists with each sub-list representing the contents of a box. Consequently, your remove_slot function has to perform a linear search to determine wh... | {
"domain": "codereview.stackexchange",
"id": 45349,
"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, programming-challenge, string-processing",
"url": null
} |
python, programming-challenge, string-processing
return sum (
box_number * slot_number * lens
for box_number, box in enumerate(boxes, start=1)
for slot_number, lens in enumerate(box.values(), start=1)
)
if __name__ == "__main__":
with open('test.txt') as f:
print(pa... | {
"domain": "codereview.stackexchange",
"id": 45349,
"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, programming-challenge, string-processing",
"url": null
} |
c++, calculator
Title: Quadratic equation solution solution searcher
Question: Here is a simple Learner practice piece of Code, which supposses to comply and give solutions to a Quadrant Equity problem (requires to give it values of a, b and c for a Quadrant function). Please any tips and Tricks highly welcome, and s... | {
"domain": "codereview.stackexchange",
"id": 45350,
"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++, calculator",
"url": null
} |
c++, calculator
Answer: Let's start with one formal advise: Your formatting is inconsistent. In particular, placement of spaces around operators is inconsistent. Use your IDE's autoformatter.
Then, there are a few cases where comments basically just reflect the code. For example, cout<<"No solutions!"; //no soultion F... | {
"domain": "codereview.stackexchange",
"id": 45350,
"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++, calculator",
"url": null
} |
css, react.js
Title: Icon styling in Tailwind/React
Question: This is a React icon component, with a few Tailwind classes.
This icon component looks as follows in the UI. The name LongIcon is to differentiate with smaller, fully round icons that are used elsewhere.
Normal:
Hovered:
I'm using flex to center the icon... | {
"domain": "codereview.stackexchange",
"id": 45351,
"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": "css, react.js",
"url": null
} |
c++, image, template, classes, variadic
Title: An Updated Multi-dimensional Image Data Structure with Variadic Template Functions in C++
Question: This is a follow-up question for Multi-dimensional Image Data Structure with Variadic Template Functions in C++. Considering the suggestion from G. Sliepen:
Make everythi... | {
"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(std::vector<ElementT>&& input, std::size_t newWidth, std::size_t newHeight)
{
size.reserve(2);
size.emplace_back(newWidth);
size.emplace_back(newHeight);
if (input.size() != newWidth * newHeight)
{
... | {
"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 const& at(const Args... indexInput) const
{
checkBoundary(indexInput...);
constexpr std::size_t n = sizeof...(Args);
if(n != size.size())
{
throw std::ru... | {
"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 print(std::string separator = "\t", std::ostream& os = std::cout) const
{
if(size.size() == 1)
{
for(std::size_t x = 0; x < size[0]; ++x)
{
// Ref: https://isocpp.org/wiki/faq/input-output#... | {
"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
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.