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
0
votes
0answers
14 views
-4
votes
0answers
19 views

How does the following code calculate a cubic spline? [on hold]

I am currently exploring cubic spline interpolation methods and I have come across the following code which interpolates in 3 dimensions, however I am unsure exactly how it works. I've researched ...
-2
votes
0answers
31 views

How to make knn algorithm more efficient in python [on hold]

I'll have to make the following algithm more efficient. The train data set can include up to 20000 point. The test data set has 5000 points. At the moment the algorithm need up to 10 minutes. ...
-4
votes
0answers
25 views

How to replace multiple substrings in a string in C#? [on hold]

I have to replace multiple substrings from a string (max length 32 of input string). I have a big dictionary which can have millions of items as a key-value pair. I need to check for each word if this ...
1
vote
2answers
45 views

Constant Time implementation of String Uniqueness Algorithm

I've written what I think is a constant time implementation of a uniqueness check. I've seen many algorithms online, which run in linear time, but was wondering whether my conclusion that this runs in ...
2
votes
1answer
42 views

Coding Challenge: Kattis

Short problem description: A sequence of numbers are antiarithemtic if there exists no three numbers a_i, a_j, ...
-1
votes
1answer
30 views

HarckerRank 2D Arrays Hourglass challenge

About this 2D Arrays HackerRank problem. The task is to find the maximum sum of a region in a 2D array. Specifically, it is to find the maximum sum of an "hourglass" region, defined as a 3x3 square ...
1
vote
3answers
32 views

`set_includes` (string subsequence-containment) in Python 2

I've implemented C++'s std::includes algorithm in Python, so that I can use it to efficiently implement a Scrabble "can I make this word" function: ...
7
votes
5answers
993 views

Finding all possible pairs of square numbers in an array

I am writing a program that allows me to find all possible pairs of square numbers including duplicates. We can also assume the array elements to be of positive integers only. e.g an array of {5,25,3,...
2
votes
1answer
76 views

Collecting numbers in ascending order

What kind of algorithm wold solve the following problem from https://www.ohjelmointiputka.net/postit/tehtava.php?tunnus=junar ? I have been given a positive integer n. Take the n first positive ...
-3
votes
1answer
67 views

Enhance Performance C# [closed]

I have the below code that performs well until input size reaches 1000. After that, it takes more than 2 milliseconds to finish. Any help? ...
2
votes
1answer
34 views

Trapezoidal rule for set of data

Here is the question from the book of Mark Newman-Computational Physics Exc 5.1 a) Read in the data and, using the trapezoidal rule, calculate from them the approximate distance traveled by the ...
2
votes
1answer
85 views

Create Node tree from list of paths

I have this list of paths: ...
2
votes
1answer
28 views

Find articulation points/ cut vertices in graph

I would like to get some feedback on my completed code that finds and displays the articulation vertices in an undirected graph. I included a test case within the code, it should (and does) print <...
-3
votes
0answers
25 views

almostIncreasingSequence CodeSignal [closed]

I have this issue. 35/38 sample tests passed. Execution time limit exceeded on test 36: Program exceeded the execution time limit. Make sure that it completes execution in a few seconds for any ...
2
votes
0answers
40 views

Java Perlin noise implementation

I've decided to code Perlin noise generator in Java. It works pretty well, I just want to know If I am doing everything right and if it is a valid implementation! PS : The ...
7
votes
3answers
529 views

Cracking the Coding Interview — 1.5 One Away

I would like to know any suggestions about to improve my code solution and your rating of my approach to the problem, I based my code on the fact that if the strings have different length removing one ...
2
votes
1answer
47 views

Find the number of all specific substrings in a string

I would like to ask a code review regarding a concrete exercise. Let's suppose I have to get the number of all specific substrings in a string. We call something specific if any of these conditions ...
8
votes
1answer
80 views

Geektrust: Solve War problem in a OO way in Python

I solved this programming challenge whose objective is to come up with an OO solution to the problem. The problem statement is quite verbose and the tl;dr version would be: Given an enemy army(...
2
votes
1answer
67 views

Find A Series of Numbers Who when Squared and Summed are equal to a given Square

I am working on a CodeWars titled 'Square into Squares. Protect trees!' it can be found here: https://www.codewars.com/kata/54eb33e5bc1a25440d000891/train/javascript I have a working solution, the ...
0
votes
0answers
98 views

Minimum sum of elements, taken 2 at a time

Update: I had not recalled the question correctly and ended up with the wrong question. I have now updated the question and the solution: The question is: Given an array of integers say [8,4,6,12,10] ...
2
votes
1answer
26 views

Calculate implicit dates

I need help to simplify this code about implicit date calculation. The context: I have sentences with implicit and explicit dates. For instance: "From the 5th to the 7th of September 2017, the ...
2
votes
0answers
30 views

Program to draw ASCII art based on commands

I want to know how to improve the design of Canvas drawing implementation. Canvas Drawing This solution is to implement a console-based canvas drawing application. Problem Statement Description ...
4
votes
2answers
105 views

Determining the maximum value that can be obtained from a loot

A thief finds much more loot than his bag can fit. We have to find the most valuable combination of items assuming that any fraction of a loot item can be put into his bag. For example has a bag ...
1
vote
1answer
97 views

Python: Heapsort

I am beginner in python and I am learing about syntax and other functions in python by writing Algorithm and Data Structure programs. How can this code be improved? ...
2
votes
1answer
65 views

Kruskal MST algorithm

I came up with the following code by following Prof. Sedgewick's lecture on Coursera. Please review this code and let me know if there is anything that I got wrong in implementing Kruskal's ...
7
votes
5answers
181 views

String combinations of 0 and 1

I was asked this question in an interview: Given a string s consisting of 0, 1 and ...
7
votes
6answers
1k views

Find max number you can create from an array of numbers

Was asked this in an interview yesterday: You are given an array of numbers (not digits, but numbers: e.g. 9, 23, 184, 102, etc.) - you need to construct the largest number from it. For example: you ...
2
votes
1answer
49 views

Find minimal number of days to meet all your friends

I was asked this in an interview yesterday: You get an array of segments (segment contains 2 numbers, representing days count (say in a month, but could be in a year, etc.): start, end). You need to ...
1
vote
1answer
51 views

String Rotation

I have solved a previous year question of 2018 codevita (link) in Python. Problem Description: Rotate a given String in the specified direction by specified magnitude. After each rotation make a ...
4
votes
2answers
60 views

Finding a vertex of a tree such that every subtree rooted at that vertex is of a uniform color

I'm trying to optimally solve codeforces problem 763A: Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the n its vertices, so ...
4
votes
1answer
59 views

Comparing objects in a list against each other

I have a list of ~27000 objects. Each object represents a line from a file where each line is one record of a measurement from some instrument. The most import aspects of the object are: Instrument ...
3
votes
1answer
49 views

Calculating all flight connections

I am working on an API to return all the possible flight route between point A and B. I have a set of data which includes the flight number, airline, departure airport, destination airport, ...
1
vote
0answers
49 views

Python: Quick Sort

How to improve this code? In C++ we use templates to write function for all data types. How to write such code in python? ...
1
vote
1answer
51 views

Python: Merge sort

How can I improve this code? ...
3
votes
1answer
38 views

A Tumbler, or a format-preserving-encryption shuffle

I wanted to implement a simple algorithm for encoding id of entries in a database to avoid: Storing the UID (unique id) in the ...
0
votes
1answer
37 views

Python: Bubble Sort

After implementing suggestions from an earlier, related question (Python: Selection sort), I have written this code. Could you help me to improve this code? ...
1
vote
1answer
46 views

What are the downsides of my Mergesort implementation?

Could I ask you to evaluate my implementation of mergesort? I think that it's consistent with \$\mathcal{O}(n \log n)\$ complexity but has a downside that I create a new ...
-1
votes
2answers
89 views

Cracking the code interview: Problem 1 .6 basic string compression

I created a GitHub repository containing my code solutions for some of the exercises proposed in the book. I updated my solution for the problem 1.6 here. Description A function that compress the ...
3
votes
1answer
77 views

Find the largest product of an array

This task is taken from www.interviewbit.com Given an array of integers, return the highest product possible by multiplying 3 numbers from the array Input: ...
1
vote
1answer
61 views

Python: Selection sort

After implementing suggestions from an earlier, related question (Python: Insertion Sort), I have written this code. Could you help me to improve this code? ...
4
votes
2answers
74 views

Edge detection in a canvas

I have one function in my code to detect edges in a canvas but it's the slowest part of my code and is used everywhere. If I could just optimise this, my whole program would speed up considerably. ...
6
votes
2answers
59 views

Sums of all sublists

The parts_sums function in the code takes a list as parameter and then returns sum of all sub-lists present in the list. e.g. if, ...
2
votes
1answer
41 views

Culling strings from a list that are substrings of other list elements

I'm bothered by my answer to this SO question. I'm pretty sure it's more efficient than the sort-and-cull implementations, but I'm having trouble expressing that in a way that I trust. Also, it's ...
0
votes
1answer
70 views

Find missing element in an array of unique elements from 0 to n

The task is taken from LeetCode Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. ...
2
votes
1answer
36 views

Length of the longest common sub sequence bottom up

Could I get some feedback on this code? I included a test case as well. This code computes the longest common sub sequence given paired data, it was not part of any challenge I just did it to learn ...
1
vote
0answers
62 views

An implementation of an AVL tree with unique_ptr

I've implemented an AVL tree using unique_ptr. The code has been stress-tested and there are no crashes or segmentation faults. I left the program running at night and stopped it manually the next ...
1
vote
2answers
53 views

Maximum contiguous sum in an array

The following code is my solution for the following Daily Coding Challenge Given an array of numbers, find the maximum sum of any contiguous subarray of the array. For example, given the ...
5
votes
3answers
492 views

Find smallest index that is identical to the value in an array

The task is taken from LeetCode Given an array A of distinct integers sorted in ascending order, return the smallest index i that satisfies A[i] == i. Return -1 if no such i exists. ...
6
votes
2answers
92 views

Find matching dictionary words given a Scrabble tray

This is a question I was asked in an interview, below is a cleaned-up copy of the answer I gave. Apparently this answer was not satisfactory. How can it be improved? Question: Given a dictionary of ...