121,486 questions
Advice
0
votes
0
replies
20
views
Clarification on Ford-Johnson (Merge-Insertion) Beta Insertion Order
I’m working on the Ford-Johnson algorithm and trying to nail down the insertion order for betas. I am unsure to apply the Jacobsthal numbers to the insertion indices tied to the original pairing ...
1
vote
1
answer
114
views
Random growth algorithm not working as intended
For a game I'm working on, I'm starting with an 8x8 int array with all values zero. Then I'm randomly placing a single non-zero value in each row/column in such a way that there's only a single non-...
0
votes
2
answers
77
views
Correctness of Algorithm: Convert Sorted Array to Height Balanced BST
I was working on Leetocde 108: Convert Sorted Array to Binary Search Tree.
The input is a sorted array of integers, and the goal is to return a height-balanced binary search tree (BST), meaning that ...
Advice
0
votes
2
replies
71
views
Remesh a surface while preserving exact boundary vertex positions and count in Python (preferably PyVista / trimesh), open to alternate libs
I have a triangulated surface mesh in Python (PyVista Polydata) and a set of pre-defined boundary points — four ordered arrays defining lead edge, trail edge, root, and tip. I want to remesh the ...
0
votes
1
answer
60
views
using reinit() in "when" statements algorithms
I have a piece of modelica code in a long when-elsewhen statement that uses reinit() for a continuous time variable and with if-elseif embedded in an equation section. In some cases, if a specific ...
-7
votes
2
answers
175
views
Lowest common ancestor C++ [closed]
I have been trying to solve this problem as a beginner in C++:
The problem is that, when using AI, it almost always provides an answer that is way too complex in both syntax and logic. When I prompt ...
2
votes
1
answer
107
views
How to properly use the Decorator pattern to add Memoization to recursive algorithms in Python?
I am learning about Dynamic Programming and I've implemented a recursive algorithm to solve the "Climbing Stairs" problem (which is essentially finding the N-th Fibonacci number).
The basic ...
Advice
2
votes
2
replies
106
views
How to implement a Strategy pattern for different pathfinding algorithms in Python?
I am building a simple grid-based game and I want to implement different pathfinding behaviors for different types of NPCs. For example, some should move using A* (for efficiency), while others might ...
Advice
2
votes
5
replies
152
views
How should I continue learning Java programming?
I need your advice.
I am learning Java programming. I know the Java basics, object oriented programming and also Swing and AWT. These days I understand that I have to learn algorithms and data ...
Best practices
1
vote
10
replies
213
views
In a lexer, for n = 48, is linear search or binary search faster?
Context
I'm just getting back into programming as a hobby, and I wanted to try making a compiler. I'm currently making the lexer, and when checking if a token is an operator, I use binary seach to ...
Advice
0
votes
7
replies
93
views
Why is my Perlin Noise algorithm not blending in between grid cells?
I have been trying to replicate Perlin Noise in Python using the Pygame module, the steps I took was this:
1. Window size is (1600, 1200) this window is split into a grid of 40 x 40 pixel Cells, so ...
Best practices
0
votes
3
replies
67
views
Scalable Exact Set Cover / Routing Clustering for ~20,000 Points with Length Constraints
I’m working on a large-scale combinatorial optimization problem that combines aspects of exact set cover and length-constrained routing.
Problem Description
I have ~20,000 points (nodes), each with 2D ...
2
votes
1
answer
227
views
C++ lower_bound [duplicate]
As far as I understand lower_bound is just a >= search operation. However, the following code snippet works:
vector<size_t> udata = {5, 4};
vector<size_t>::const_iterator it = ...
Advice
0
votes
7
replies
131
views
Kahn’s Algorithm with check versus just depth first search
So I am working on a program that needs to detect which nodes (if any) in a directed graph are involved in a cycle.
My question is, is it more efficient/less bug prone to just do Kahn’s Algorithm and ...
Advice
1
vote
6
replies
89
views
Algorithm for selecting random representative subset based on absolute frequencies
Given a long list of (unique) names, as well as an absolute frequency for each name, what is an efficient algorithm to select a random subset that is (more or less) representative of the entire list?
...
3
votes
1
answer
91
views
Low Transposition Table Usage Rate
I'm currently learning to make a Gomoku ai script that uses minimax algorithm along with alpha beta pruning and other methods to improve performance. In this current state, I've implemented a ...
3
votes
2
answers
212
views
Is it possible to determine at translation time the value of CHAR_BIT without using CHAR_BIT (requires <limits.h>)?
In order to cross-compile with Clang a C code which uses CHAR_BIT there is a need to include <limits.h>. To include <limits.h> there is a need to specify both --sysroot and -isystem ...
1
vote
2
answers
203
views
How can I programmatically derive Taylor series?
So I know from physics that position at a given time can be calculated using its derivatives like this:
Now I know this looks awfully similar to Taylor series:
In fact they are exactly the same. ...
Advice
0
votes
5
replies
132
views
Fastest hash function for a sparse key
I have a uint32_t key with 2-4 bits set, and I have a perfect hash map. Right now, with a python script, I brute force to find kMul such that all the keys are in separate locations. The keys are ...
Advice
0
votes
11
replies
103
views
Where do I find scapegoat trees real code?
The original article on scapegoat trees by Galperin and Rivest is quite interesting, and so seem to be all the courses, lectures, and descriptions, like OpenDataStructures and also with a lesson in ...
Advice
3
votes
6
replies
111
views
Are there better algorithms for computing list similarity that this one I wrote, or corrections?
The intent of the system is to identify the place where the person's signature should go in a PDF. I am trying to find the best matches to a complete human name. I'm parsing the PDF using pdfplumber ...
Best practices
5
votes
5
replies
132
views
Optimizing a Gaussian penalty function for HSL color compatibility in PyTorch/NumPy
I am currently developing an AI-driven fashion recommendation system, specifically focusing on a "Multi-modal Context-Aware Decision Model." A critical component of my recommendation engine ...
Advice
1
vote
7
replies
149
views
If I want to build a bus tracking system for an institute using C, what are the others things I should consider?
Suppose there is a bus service operated by an institute. But the employees don't know where the bus currently is. I want to build a system where the employee will know the exact location and estimated ...
1
vote
3
answers
238
views
How can I make my tangent numbers generating class resumable?
I have decided to implement code to generate tangent numbers as a self-imposed programming challenge. I have succeeded in doing so, but not as efficient as I want it to be.
What are tangent numbers? ...
Advice
0
votes
6
replies
115
views
Finding substrings from the string using sets: algorithm's logic
I was solving the algorithm problem: find all unique substrings from the string, using sets.
Here is the model solution, but I can't understand what line 6 means. Why does the end go up to length + 1? ...
Advice
0
votes
5
replies
112
views
Possible solution to prime numbers
P(n) = sum_{k=0}^{100} [ p_k * product_{j=0, j≠k}^{100} (n - j)/(k - j) ] this is called a polynomial interpolation formula which gives a streak of 100 prime numbers higher than Euler's n²+n+41 which ...
5
votes
1
answer
311
views
How can one algorithm add the same fractions faster than another one in the same environment?
I need to add many fractions together. I don't use float because IEEE-754 Double Precision Floating Point has only 53 bits for mantissa thus 53 * log10(2) = 15.954589770191003 accurate decimal digits.
...
Best practices
2
votes
7
replies
152
views
How to add many fractions together as fast as possible?
This is related to this question.
Basically, I need to add many fractions together. Seems simple enough? Except it isn't simple.
I can't use fractions.Fraction class, that thing stupidly does fraction ...
10
votes
2
answers
381
views
Function object returning the nth item of a tuple
With std::ranges::max, it is possible to use a projection as follows
std::vector<std::tuple<char,int,double>> v = {
{'a',2,1.0}, {'c',1,3.0}, {'b',4,2.0}, {'d',3,4.0}
};
auto m = std::...
Advice
1
vote
11
replies
93
views
Find an explicit criterion about a specific class of integer sequences expressed by a recursive predicate
Let a "branching" operation be an operation that, given a finite sequence of integers, increments and duplicates one element in place. For example:
(3,1,4,1,7) → (3,1,5,5,1,7)
A tree ...
Advice
0
votes
31
replies
302
views
I made a simple collision-detection algorithm, can you take a look at it?
Unlike conventional collision detection algorithms (using right-angled triangles), I created an algorithm using the theta value with objects. (Theory, etc., got a little help from the Gemini, but I ...
Best practices
0
votes
8
replies
84
views
How to estimate sub‑sample peak positions for very sharp periodic peaks with few cycles?
I have a sampled 1D signal that contains very sharp periodic peaks. The peaks are narrower than the sampling resolution, and the lower part of the signal contains noise.
The goal is to estimate the ...
10
votes
1
answer
675
views
Is the brute-force solution for "Is Binary Tree Balanced" really O(n^2) with early exit?
I’m looking at the classic “is binary tree balanced” problem (LeetCode 110 style):
Given a binary tree, determine if it is height-balanced.
A binary tree is height-balanced if for every node, the ...
4
votes
1
answer
113
views
DFS Algorithm for undirected graph
I am studying Depth First Search (DFS) on undirected graphs and I am confused about discovery order.
I have attached a figure of an undirected graph with DFS discovery (d) and finishing (f) times ...
1
vote
1
answer
229
views
Can arbitrary precision integer increment in Brainfuck be done in O(1) code size?
I was fiddling with the Brainfuck esolang over the past few days, and tried to implement an increment operation on an N-byte-wide integer in big-endian format. Note that I am imposing a structural ...
2
votes
0
answers
186
views
How to implement anisotropic blur from geometric formulas?
I need to apply a blur/filter to a 2D grid where the blur parameters vary per pixel based on geometric calculations. I have:
Image: 2D grid of values
Viewing angles: 2D grid where each pixel has an ...
0
votes
0
answers
71
views
deleting a node from a red black tree
I have been following the Cormen algorithm for RB trees, but I have a question regarding deletion, specially in this case:
38
31 41
all the nodes are black and I want to delete 38. After ...
0
votes
1
answer
159
views
How to calculate the points on the "perimeter" on a bitmask?
I am making a drawing application and i have to show the borders of the pencil. To know where to place the pixels I use a list of integer offsets from the cursor that make up a shape, so the cursor is ...
Tooling
0
votes
11
replies
126
views
Filtering an inconsistent string
I'm working on a project in which I need to filter out specific strings from an inconsistent API, where the strings are not always what I want. I need to filter out eligibility for different education ...
Advice
2
votes
4
replies
77
views
How does DP provide optimization over backtracking in 0-1 Knapsack problem
0-1 Knapsack problem is often used as a starting problem to understand dynamic programming. This problem can also be solved using backtracking. Now, backtracking explores all possible subsets and uses ...
Best practices
2
votes
17
replies
6k
views
how to separate break conditional logics from while statements for readability, encapsulation and DRY
General question
I wonder how to express any code of the form
while True:
state_before = some_function(my_object)
# algorithmic code goes here
state_after = some_function(...
Advice
0
votes
3
replies
83
views
Big O Notation - data structures
i am learning about big o notation and need a little bit of clarity before i diverge into the wrong path. my understanding is o() is suppose to represent the time it takes for a program to do a task ...
Advice
0
votes
5
replies
104
views
Converting latitude and longitude from degrees to meters
As the title says, how can i convert a value that i have from a longitude or latitude from degrees to meters ? The task is basically to calculate MAE errors for x,y,z of trajectory prediction, MAE Z ...
Advice
2
votes
10
replies
70
views
How to fit a circle onto a weathered pipe?
I have a 3d scan of a metal pipe and I can slice it at different heights. When looking at a cross-section of a new pipe, the data should match the shape of a perfect circle, but due to erosion the ...
Advice
6
votes
12
replies
6k
views
Best algorithm for doing fixed-point euclidean difference?
I need a fast algorithm to compute fixed-point euclidean distance in 3D. I'm really struggling to find a fast solution.
I am working on a 32-bit RISC-V processor where I have fast multiply, but no ...
Best practices
5
votes
6
replies
126
views
Implementing a special case of combination
I have an array of arrays which are not necessary of the same size which I want to combine in the following way.
Each nested array is scanned in the order they appear in the main array. The scanned ...
Best practices
2
votes
1
replies
80
views
Python/GenAI/LLM: Best practices for asking follow‑up questions to clarify user intent in a chatbot
I’m building a chatbot using Python, pydantic_ai, google_genai, and the gemini‑2.0‑flash‑lite model. The bot includes a module responsible for retrieving context from a knowledge base (RAG). The ...
3
votes
1
answer
479
views
Optimize, or correct, a code for interpolation and parameter estimation
This is a simple question, but will be a little long so I can give all the context to what is happening. Essentially, this is a code question/issue and the physical context behind is just to ...
Advice
0
votes
7
replies
128
views
Brute force vs optimal approach to find the second largest number in a slice (Go)
I’m looking to understand both:
A brute force solution
An optimal solution (in terms of time and space complexity)
The input is a slice of integers, for example:
nums := []int{10, 5, 20, 8, 15}
...
Best practices
1
vote
14
replies
6k
views
How to Cover an N × N Square Room with the least Square Tiles? (could be reused)
The problem is:
Cover an N × N square room with square tiles (the tiles do not all need to be the same size) without overlapping, where the side length of each tile is integral and less than N.
What ...