Tagged Questions
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.
-2
votes
0answers
20 views
How can I speed up the following function in R?
I wrote the following function in R. I want to iterate it, say 50000 times. I used "sapply" in my function but it runs slowly in R.My PC is still working about 20h now and I have no idea about the run ...
0
votes
1answer
23 views
Generating variables to be named, filled in and operated on the fly in python
I am very new to python and trying to refactor my code which is ripe for error.
Currently I manually name a bunch of arrays:
...
2
votes
1answer
40 views
Counting all substrings with exactly k distinct characters
Problem Statement : Given a string of lowercase alphabets, count all possible substrings (not necessarily distinct) that has exactly k distinct characters. Example:
Input: abc, k = 2 Output: 2 ...
4
votes
2answers
30 views
Finding an equilibrium index in an int array
Here is a programming challenge from codility
A zero-indexed array A consisting of N integers is given. An
equilibrium index of this array is any integer P such that 0 ≤ P < N
and the sum ...
5
votes
1answer
95 views
The Palindromic Odometer Puzzler: A Programmatic Solution
That is a programmatic solution to a puzzler from Car Talk, its summary is:
"I noticed that the last 4 digits were palindromic. I drove a mile,
and the last 5 were palindromic. I drove another ...
3
votes
1answer
56 views
My Quick sort examination
I had a test on sorting using quick sort algorithm, which I have implemented. It's working fine.
But my examiner is not satisfied. They say that the solution which I provided is not a proper ...
-4
votes
0answers
20 views
Testing if the arc between two (lat,lon) seapoints, intersect landmass [on hold]
I have the following problem: I have a bunch of points (latitude,longitude) that I know are located in the mediterranean sea. I have to check if the great circle path among each pair intersect the ...
2
votes
1answer
28 views
Dynamic Official Receipt increment algorithm
I made a function that would automatically generate official receipt for POS. it is able to accept format such as xxx, xxx-xxx, xx-xx.
But my code is really messy and horrible and it needs review. I ...
2
votes
1answer
43 views
Total number of stops for an elevator to serve certain people
I have seen this question asked around a bit, such as this Code Review question in Java:
Write an optimal program that would determine the total number of stops
a elevator has taken to serve X ...
0
votes
0answers
18 views
Real Coded Genetic algorithm performance issue [on hold]
I am pretty new to genetic algorithms, and wrote a real valued genetic algorithm with an sbc function used for crossover. Currently I was trying to use the class below to train the weights for ...
2
votes
0answers
59 views
Solving the Mining algorithm from HackerRank
I was working on this problem for a few hours last night and finally came up with a brute-force solution. The task is to report the minimum work necessary (sum of weight × distance) to relocate gold ...
4
votes
1answer
56 views
“Similar Destinations” challenge
I am currently solving the Similar Destinations challenge on HackerRank and am in need of some assistance in the code optimization/performance department. The task is to take a list of up to 1000 ...
7
votes
1answer
84 views
Cipher text using a 2D array
I have a simple program which ciphers text using a 2D array. You specify what letters to replace with what and it does it.
I understand the code is very repetitive and that I should probably use ...
6
votes
2answers
120 views
Accepting user defined functions for custom map reduce functionality in C++
I am implementing map and reduce - style functions for processing geospatial raster datasets.
I would like the ...
10
votes
2answers
446 views
Project Euler #2: Even Fibonacci numbers
Each new term in the Fibonacci sequence is generated by adding the
previous two terms. By starting with 1 and 2, the first 10 terms will
be:
\$1, 2, 3, 5, 8, 13, 21, 34, 55, 89...\$
By ...
6
votes
0answers
85 views
+50
Heapsort implementation in C++14
Please review the following implementation of heapsort and pay attention to the following:
Have I correctly chosen the names InputIt and ...
4
votes
2answers
82 views
Merge sorting a singly-linked list in Java - follow-up
In this iteration, I incorporated all but one points made by forsvarir in the previous iteration. Now my code looks like this:
LinkedListNode.java:
...
2
votes
3answers
67 views
Java number formatting in accountant style
I have written a string number formatter. I would like to know if this can be improved and any suggestions with how it's done.
The purpose of this function is to convert a double that has been ...
3
votes
2answers
68 views
Merge sorting a singly-linked list in Java
I have this nifty merge sort implementation for sorting singly-linked lists. Its running time is \$\Theta(n \log n)\$, yet it uses only \$\Theta(\log n)\$ space (the stack). See what I have below.
...
1
vote
2answers
58 views
Selection sort algorithm examination
I had an algorithm test.
I was asked Selection sort algorithm to program.
I coded it in javascript and shown to examiner.
But he was not satisfied, he said its too complicated.
I think I have ...
5
votes
2answers
51 views
Finding longest common prefix
I have been trying to solve a modification of the Longest Common Prefix problem. It is defined below.
Defining substring
For a string P with characters P1, P2,…, Pq, let us denote by P[i, j]
...
2
votes
2answers
40 views
Implementation of insertion sort in JavaScript
I have written an insertion sort algorithm in JavaScript:
...
10
votes
2answers
187 views
Displaying all substrings of some given string
I solved exercise 8 from Core Java for Impatient, chapter 1. This exercise is to implement a displayAllCombinations method to display all substrings of the given ...
6
votes
3answers
383 views
Save the Prisoner
A jail has prisoners, and each prisoner has a unique id number, S, ranging from 1 to N. There are M sweets that must be distributed to the prisoners.
The jailer decides the fairest way to do this is ...
5
votes
3answers
85 views
A command-line calculator using infix to postfix in C++
I was in the mood for some C++ and decided to write a command line calculator, which understands addition, subtraction, multiplication and parentheses. See what I have:
main.cpp
...
3
votes
2answers
37 views
Getting the smallest snippet from content containing all keywords
This returns the smallest snippet from the content containing all of the given keywords (in any order). This provides the correct solution but I would like to know if it can be made more efficient.
<...
1
vote
1answer
31 views
LRU cache in Python
Here is my simple code for LRU cache in Python 2.7. Appreciate if anyone could review for logic correctness and also potential performance improvements.
A confusion want to ask for advice is, I am ...
4
votes
1answer
40 views
Stampcalculator - Given a set of stamps, what combinations are there to reach a certain amount?
Background
My mother has a hobby of buying and reselling books via online trading sites. After a price is agreed on, the books have to be put into an envelope for mailing. On this envelope, stamps ...
0
votes
0answers
30 views
HackerEarth Prateek and his Friends non-contiguous
How to solve this problem for non-contiguous range?
Prateek wants to give a party to his N friends on his birthday, where each friend is numbered from 1 to N. His friends are asking for a gift to ...
3
votes
2answers
104 views
Quick Sort in Java
I recently started learning about algorithms and data structure's. I decided that for every algorithm I learn about I'll implement it without looking at any pseudo code or actual code.
I have ...
2
votes
0answers
31 views
Unbounded knapsack solution with items of equal value
My solution correctly solves the problem, but I would like to optimize its performance. For a capacity of 7 and bars = [ 4, 1, 1, 2, 1], the output is 7.
...
1
vote
0answers
31 views
Comparing a recursive and iterative traveling salesman problem algorithms in Java
This snippet is about two (brute-force) algorithms for solving the traveling salesman problem. Since there is plenty of boiler-plate code, I arranged a GitHub repository for the entire program, in ...
2
votes
2answers
48 views
AVL tree insertion and deletion of nodes in C. 2.0
I asked a question yesterday, based on the answers to that question and some personal insights I was able to update the original code. which I am posting here to get reviewed. I also thought about ...
4
votes
1answer
67 views
Stack implementation in ES6
Finally happy to see that now we can have iterators and generators. I also need to try to have private properties too so that we have well encapsulated data structures and I see that there are Proxies ...
3
votes
1answer
58 views
Knapsack algorithm in JavaScript - Integer weights and values
My version of Knapsack works only when the weights or values of items are whole numbers.
Restrictions
You are given an array of objects each of which contains a weight and value.
You are also given ...
1
vote
1answer
81 views
C++ Maze Generator
I have made a random maze generator that allows for custom sizes via command arguments. It uses depth-first search and is written is C++. Could you look over my code and suggest any improvements ...
2
votes
3answers
74 views
C++ Tree Class implementation
I am trying to implement a class for the Node of a Tree in C++ in order to represent an HTML structure.
It is not complete by a shot, but i'd still like an opinion.
TreeNode.h :
...
0
votes
0answers
39 views
Longest palindrome O(n^2)
After getting comments on my previous implementation, I have tried to implement with DP. But without using matrix. I am not doing any boundary checks on the string just wanted to see if I am missing ...
4
votes
4answers
636 views
Find Watson's integer
Problem Statement:
Watson gives Sherlock an array \$A_1\$, \$A_2\$ ... \$A_N\$.
He asks him to find an integer \$M\$ between \$P\$ and \$Q\$ (both inclusive), such that, \$\min \{|A_i-M|, 1 \le i \...
1
vote
3answers
370 views
Bubble sort examination
I solved bubble sort in an exam, but the examiner wasn't satisfied with the solution. Please tell me why my solution is unsatisfactory.
...
3
votes
2answers
70 views
Largest palindrome in the string
There are a lot similar questions out there. However, I wanted to get comments on my code. My implementation (as I believe) is simpler than the other complex ones as mine is \$O(n^2)\$.
My approach ...
4
votes
2answers
100 views
Print tree in a zigzag manner
I want to print a tree in a zigzag manner level-by-level. I'm wondering if there are any logic issues in my code and any further performance improvement ideas by using a smarter algorithm ...
3
votes
1answer
36 views
Matlab implementation of Needleman-Wunsch algorithm
This code (an implementation of the path finding Needleman-Wunsch algorithm), given two sequences, correctly aligns and calculates the similarity of them. For example, given two sequences:
AAA,CCC
...
1
vote
1answer
52 views
Phishing Project UserIterator Implementation
As I stated in an answer comment on my previous question, the UserIterator has a much larger footprint than I think most people realize. This question is here to further explain the project, the ...
8
votes
1answer
88 views
I CAN HAS EULR 14, PLZ?
A few days ago I found myself learning lolcode. It turns out it's (almost) a real language and capable of handling (pseudo-)serious problems. So, let's try some (pseudo-)serious code this time.
...
4
votes
0answers
27 views
Princeton KMP implementation
I implemented a Python version of KMP followed by Princeton Prof Robert Sedgewick's discussion.
However, I got a Time Limit Exceeds error. Per leetcode's explanation, TLE indicates that the solution ...
3
votes
0answers
19 views
The Miller-Rabin Primality Test in Clojure
I am new to clojure and I wanted to test out my skills by taking an existing implementation written in Python and writing it in Clojure.
Concerns
My main concerns are where I use ...
0
votes
1answer
36 views
Find numbers that are both Triangle numbers and Square numbers
I've written a simple JavaScript function to find numbers that contain both the attributes of a Triangle (\$ n(n+1)/2 = x \$) and a Square (\$ n^2 = x \$) number.
The challenge was given in Matt ...
1
vote
2answers
81 views
Excerpt from a brute force Rubik's Cube algorithm
I have a program that brute force searches for a combination of inputted array elements by the user and when the program is searching all the for loops are nested for the brute search then I have this ...
2
votes
0answers
38 views
Shell Sort code
I have a working shell sort code. I had already written a code for insertion sort, which I have used as underlying algorithm for doing shell sort on sub lists. So if you pass value of ...