Questions tagged [algorithm]

An algorithm is a sequence of well-defined steps that define an abstract solution to a problem. Use this tag when your issue is related to algorithm design.

Filter by
Sorted by
Tagged with
1 vote
1 answer
31 views

Greedy approximate algorithm for metric TSP in Java

I have a repository that provides three metric TSP solvers: Brute-force solver, Genetic solver, Greedy approximate solver; solver 3 is the subject matter of this post. The typical output is: ...
  • 26.6k
0 votes
2 answers
74 views

Find the shortest distance between two letters

I've been tasked with finding the distance between two letters in the alphabet. Below is my solution. My main concerns are if there is any shorter way to do this, and if there are any edge cases I'm ...
  • 9,719
2 votes
0 answers
51 views

Snake game with constant time complexity algorithm

https://github.com/speedrun-program/constant_time_snake_game Memory efficient snake game with O(1) algorithm for snake movement and bug placement. Three grids are used: a grid representing the game ...
3 votes
1 answer
82 views

Calculate overlap of two datetime objects

I would like to find out how much time lays in between 22.00 and 6.00 o’clock for a given event per day. So if the event start at 21.00 and ends at 23.59 the result would be 1.59. For a start at 22.00 ...
  • 31
3 votes
1 answer
91 views

Binary Search Tree implementation in C [1]

I have tried to implement my Binary Search Tree in C. The following are the 18 operations defined: create a bst is_empty insert delete clear find find_min find_max height size depth level order ...
  • 423
2 votes
1 answer
62 views

Breaking a string expression in operator and operands

Requesting for a code review for a scala implementation. Problem Given an expression string made of numbers and operators +,-,/,* , break it in a list of Integer or ...
  • 365
3 votes
2 answers
150 views

Detecting Intersections of a Collection of Singly-Linked Lists

I was recently presented with the following coding exercise as part of a job interview... Linked List Intersection Question You are given a collection of singly-linked lists(SLLs). Return true if any ...
  • 487
8 votes
4 answers
160 views

Array List C implementation

I want to show you my implementation of the array list in C. Is there something I can improve or fix? Header ...
  • 161
0 votes
1 answer
55 views

Doubly Linked List of integers

I wrote a working linked list program in C. What can I improve in my code in your opinion? The first thing I think about is doing unit tests. I want to learn TDD. I'm not sure if it is a good idea to ...
  • 161
1 vote
1 answer
44 views

DFS Maze Solver

I am new to Rust Programming so I decided to implement a Maze Solver using DFS. I was wondering if there is any way to optimize this code further ...
  • 11
1 vote
1 answer
64 views

Assignment function for Intervals container

I found the two related questions but they, however, do not answer my question: interval map implementation Where exactly does my code not adhere to the specification of the key and value type? The ...
  • 111
1 vote
0 answers
84 views

Making Robert Tarjan's offline LCA algorithm run (much) faster (Java)

I have produced this GitHub repository that compares the performance of: Robert Tarjan's off-line lowest common ancestors algorithm, An improvement of the above algorithm. Typical demo program ...
  • 26.6k
3 votes
2 answers
355 views

singly linked list optimally defined over operations in C?

I have implemented my singly linked list in C and I tried to do it with the most efficient code as possible. Is there any place for improvement? This post is the second version. First version of this ...
  • 423
1 vote
1 answer
107 views

Text to image steganography

This simple implementation hides text secrets (S) in images by manipulating the least significant bits (lsb) on Pixels. How it works When encrypting I take a three pixel "chunk" for each ...
0 votes
1 answer
101 views

Bookstore program using standard algorithms

This is the exercise, from C++ Primer 5th edition: 10.32: Rewrite the bookstore problem from § 1.6 (p. 24) using a vector to hold the transactions and various algorithms to do the processing. Use <...
0 votes
0 answers
107 views

Program to generate a tree of permutations

There is a computation I'm doing with permutations (permutations of varying length of an entire interval [1,N] for some N) in python that I feel could be faster. ...
6 votes
1 answer
279 views

Implementation of DFS algorithm as described by Algorithms - Dasgupta, Papadimitrious, Umesh Vazirani

I just want feedback regarding C++ coding style and if in any way I can optimize my code (e.g. to use a different data structure). I'm only trying to use up to C++ 14 standard code. Any other ...
  • 205
9 votes
3 answers
2k views

Euler's brick generator in C++

I stumbled upon this unsolved problem of math named Euler's brick. I wrote a program in C++ to generate solutions for Euler's brick problem. It searches from ...
2 votes
1 answer
99 views

K-Way Partitioning QuickSort

I have implemented the K-Way Partitioning algorithm which is based on the Peter Taylor's solution. Which works but i want fill the q array which is the borders of ...
user avatar
6 votes
2 answers
245 views

A selection sort implemented in Python

I'm not proficient at Python. My question is twofold: whether this selection sort algorithm implementation for number lists could be considered clean code (in the sense of being modular and ...
  • 257
0 votes
1 answer
59 views

Parsing shortcodes out of a string

I wrote this shortcode parsing and it runs in \$O(N^2)\$. Is there a way to better optimize this? ...
  • 151
3 votes
1 answer
203 views

Prime factorization algorithm by using the multiprocessing module of Python

I may introduce a Python code of prime factorization which is from my personal project that I'm working on. ...
4 votes
2 answers
662 views

Implementation of Hash Function in C language

I'm new to programming. and I'm learning a C language. Currently working on Harvard's CS50 problem set (this one: https://cs50.harvard.edu/x/2022/psets/5/speller/). In that problem, they want us to ...
  • 143
5 votes
5 answers
512 views

Find the K most-frequent values in a vector

I am solving this problem that requires returning the most frequent K elements, and even though I have a solution, I am wondering if there's an improvement that can be made. It runs O(N) to store the ...
  • 458
1 vote
1 answer
79 views

Calculate result of an election using the D'Hondt method

I have learned about list comprehensions a week or so ago and understood that they can save a lot of screen space. Though, I have been told that this is bad practice only to use them, even if it ...
3 votes
1 answer
79 views

Node graph implementation for a material system

I'm working on a material/shader graph system. I came up with a simple solution, but nodes that weren't connected were used in some situations. So, I thought of using Kahn's algorithm to execute nodes'...
  • 309
1 vote
0 answers
25 views

Optimizing search algorithm for guessing list of strings via function returning bool if substring is in list [duplicate]

I trying to figure out the most effective way to accomplish this task: A function, check(), contains a list of strings. Calling the function with a string as ...
user avatar
2 votes
3 answers
141 views

Most effective search algorithm for guessing list of strings via function returning bool if substring is in list using Python

I trying to figure out the most effective way to accomplish this task: A function, check(), contains a list of strings. Calling the function with a string as ...
  • 21
-1 votes
1 answer
153 views

Getting three numbers input interactively and process them

I'm new to Python and programming in general. Let's imagine I have a simple task: Given 3 numbers from user input, I need to count negative ones and print the result. Considering what I know about ...
0 votes
1 answer
156 views

subsets in array. Algorithm (No Linq)

Task: A subset is a continuous segment of an array. Two integer arrays are given with elements from 1 to 100: of N and n (N > n). For every subset of the first array of n length find out the next ...
0 votes
0 answers
37 views

Highlighting text with custom colors based on many regex

After quite a few attempts I've got a working solution with a Highlight react component. I'm not sure the algorithm is very clean or efficient mostly when it comes ...
  • 121
0 votes
0 answers
80 views

Searching for all paths between two terminal nodes in an undirected, unweighted graph using DFS in Java

I have this small program for building undirected, unweighted graphs and finding all the paths between two given terminal nodes: ...
  • 26.6k
6 votes
1 answer
108 views

ASCII art smoke wisp generator

I've written a Python function that generates a smoke wisp/genie-tail shape using all 26 (and only 26) letters of the alphabet. Here are some examples of the kind of ASCII art I'm trying to produce: ...
1 vote
3 answers
129 views

Convert camelCase function name to snake_case

I have the following code: ...
  • 11
2 votes
1 answer
97 views

C# Get All Diagonals Jagged Array

How can I improve this? The idea is to store every left to right diagonals in a list Not looking for a more efficient algorithm instead something readable, LINQ perhaps? input: 3, 1 2, 5, 7 1, 5, 8, 3,...
  • 113
2 votes
2 answers
96 views

Get objects from list based on object's field

I have a method which takes List<Document> documents as parameters. I created unit test for it: ...
  • 145
1 vote
0 answers
51 views

How to simplify Tarjan's algorithm implementation in JavaScript and make it non-recursive (i.e. iterative)?

I adapted this Tarjan algorithm in JavaScript to be a little more functional and not pollute the Vertex object with temporary metadata, and arrived here: ...
  • 351
3 votes
1 answer
293 views

How can I optimize double for loop in matrix

I am trying to optimize a function called place_block. It is meant to give the best row, col coordinate in the matrix to place a given block of row and column size. ...
2 votes
1 answer
120 views

Finding the 4 squares that sums up to a given number

I wrote this algorithm in JS with the main goal that it will give an answer for 10^750-10^1000 in 2-3 seconds but it solves 10^150 in 2-3 seconds so I am a little far from my goal, the problem is ...
  • 129
4 votes
1 answer
471 views

Find center of an undirected graph

Task: implement an algorithm to find graph center \$Z(G)\$ given undirected tree \$G\$. This is my first time programming in C++ so any (elementary) feedback is appreciated. The way I did it is: Run ...
2 votes
2 answers
81 views

Find differences between two arrays

I have to identify added and deleted items between two objects with the same structure. ...
0 votes
0 answers
188 views

Integer hash function for concurrent map implementation golang

I am using concurrent map from this repo which only uses string as the key and it doesn't have any implementation for key as integer (int64) so I tried implementing it by just replacing all ...
  • 1,647
2 votes
1 answer
37 views

Optimising function to strip honorifics from names

Problem I have a list of around ~1000 honorifics, see below for a sample. Given an input string of a name, for example ...
  • 233
0 votes
0 answers
33 views

Java pathfinding in general trees - Part 2/4: algorithms

Java pathfinding in general trees - Part 1/4: model classes Java pathfinding in general trees - Part 2/4: algorithms (this post) Java pathfinding in general trees - Part 3/4: demonstration Java ...
  • 26.6k
2 votes
2 answers
75 views

Check if a date is in an interval defined by partial/periodic dates

I can define an interval with start and end in the format YYMMDD, but they can also be ...
4 votes
1 answer
39 views

Optimizing code for this function that filters a JSON file

I made a function that turns all child from a JSON file to parents (or brothers?). It adds the parent name to the key, so there are no duplicates. It works ok, but I would like to ask if there are ...
  • 41
1 vote
1 answer
127 views

Euler's totient function for large numbers

I made this algorithm to compute Euler's totient function for large numbers. A sieve is used. ...
0 votes
1 answer
83 views

Newton-raphson to find the square root of an arbitrarily sized number with arbitrary precision

I read here that "Furthermore, for a zero of multiplicity 1, the convergence is at least quadratic (see rate of convergence) in a neighbourhood of the zero, which intuitively means that the ...
  • 309
6 votes
2 answers
748 views

Find all combinations of length 3 whose sum is divisible by a given number

I came up with a suitable solution to a HackerRank problem that failed because the execution took longer than 10 seconds on lists that were of very large size. The problem: Given a list ...
4 votes
2 answers
325 views

Calculate sum of largest sequence of decreasing odd ints

I wrote a method that finds the maximum sum of consecutive decreasing sequence of odd integers. For example: if sequence is 13 9 7 12 13 15 13, then sum is 29 (13 + 9 + 7). I don't think it's as good ...

1
2 3 4 5
101