Tagged Questions
An algorithm is a precise plan (in the format of a sequence of steps) on how to solve a particular problem.
3
votes
4answers
153 views
Is there a better version for verbosing the output of the euclidean method?
Here is my implementation of the Euclidean Algorithm. My question is how to make it more "professional". It's working right, but isn't this too newbie?
...
11
votes
4answers
604 views
“Two-pass algorithm” implementation in Java
I'm rather new to Java and to algorithms in general. I implemented the so called Two-pass algorithm, and want to know if there are improvements to my way of implementing it. What it does is it takes a ...
3
votes
0answers
83 views
Genetic Algorithm in Python
I'm a new programmer, so any help is advised. Preferably to make it faster, avoid heavy memory usage and so on.
EDIT:
Updated the code, now including a functional test program.
Fixed the PEP-8 ...
3
votes
3answers
843 views
3
votes
0answers
62 views
String matching algorithm
I have been going through algorithms and data structures and recently came across string matching algorithms. I have not yet wrapped my head around the significance KMP algorithm so I came up with ...
2
votes
1answer
36 views
Performance of Codility MaxCounter challenge solution
I'm doing some of the Codility challenges using Ruby. The current challenge is "The MaxCounter," which is described as:
Calculate the values of counters after applying all alternating operations: ...
2
votes
0answers
33 views
Implementation of merge sort and bubble sort
I'm new to Go and looking for a review of my merge sort and bubble sort implementations. What can be done better? Can my code be cleaner/clearer?
...
2
votes
1answer
35 views
Detect a complete binary tree
Detect if a tree is complete binary tree or not. Looking for code review, optimizations and best practices.
...
4
votes
1answer
32 views
Implement stack using a linked list
I am trying to implement Stack using a linked list. Looking for code-review, best practices and optimizations.
...
7
votes
1answer
40 views
Find the Next Greatest Element
Given an array, print the Next Greater Element (NGE) for every element. The Next Greater Element for an element x is the first greater element on the right side of x in array. Elements for which no ...
0
votes
2answers
35 views
Partition array into 2 sets, such that different of sums in both subsets is minimal
Given a set of \$n\$ integers, divide the set in two subsets of \$\frac{n}{2}\$ sizes
each such that the difference of the sum of two subsets is as minimum
as possible. If \$n\$ is even, then ...
2
votes
1answer
20 views
Determine if subset in int array adds up to a given input integer
Given a set of non-negative integers, and a value sum, determine if there is a subset
of the given set with sum equal to given sum. On similar lines, Partition problem is to determine whether a given ...
18
votes
3answers
264 views
+150
Analyzing Minesweeper Probabilities
Calculating probabilities in Minesweeper might sound like an easy task, but I've seen so many probability calculators that's either incorrect, horribly slow, or with ugly code (or all of them) so I ...
8
votes
2answers
117 views
C++(11/14) intercalate implementation
I wrote a simple generic intercalate function (that should be semantically equivalent to the Haskell intercalate).
...
8
votes
5answers
464 views
Prime Number Sieving Algorithm
Edit:
Newly Updated Code here A Fast Approach to Prime Sieving (Array Implementation - Non-Threaded)
Original Thread
Last semester in my Masters Program I developed this code for sieving.
Is this ...
2
votes
1answer
38 views
Given suffix array and a pattern, search if given pattern exists
Given a suffix array for a word, check if a pattern (consecutive chars) exists.
Example:
A suffix array constructed for "ameya" should return true for "ame" but false for "foo" or false ...
2
votes
0answers
35 views
Implementing Simple Diff in Rebol
I've taken a crack at implementing Simple Diff in Rebol (versions 2 and 3). Simple Diff works by finding the longest common sequence in two series, then recursively applies itself either side of ...
4
votes
2answers
60 views
The Three-machine proportionate flow shop problem with unequal machine
As part of an Academic project I wrote the following code, according to the algorithm (verbal). When I checked the rest of the article I notice that the efficiency should be \$O(n^2)\$ and I wrote ...
1
vote
1answer
79 views
13
votes
2answers
196 views
Calculate the intersection between two intervals on the same circle
I want to calculate the ratio of intersection between two intervals; the length of the intersection divided by the length of the shorter interval.
If two intervals do not intersect, the ratio is 0. ...
3
votes
1answer
56 views
Quick Hull Algorithm implementation for higher dimensions
I tried to implement the Quick Hull Algorithm for computing the convex hull of a finite set of D-dimensional points. Here is the repository. Algorithm itself:
...
3
votes
1answer
74 views
Point in a polygon algorithm
I am implementing a Point in polygon algorithm.
Inputs:
M, N: size of the matrix
poly: a ...
4
votes
1answer
54 views
Segment tree implementation
I'm learning segment trees and their implementation. For the purpose, I tried solving the problem on CodeChef.
Full code here
My tree implementation is as follows:
...
3
votes
2answers
67 views
Optimizing graph analysis
I've implemented the Floyd Warshall algorithm in this problem. However, as this is my first time in handling with adjacency list, I was not able to make it memory efficient.
I used a 1000*1000 and ...
5
votes
0answers
149 views
Puzzle game with sequence numbers
I am currently developing a puzzle game that has sequence numbers. The player has to fill the grid with sequence numbers in ascending order. Starting from 1 the player can move horizontally or across ...
10
votes
1answer
91 views
Sieve of Atkins algorithm
I got the inspiration to write this algorithm from this question. The OP cited the Sieve of Atkin as one of the fastest ways to generate primes. So I figured I would give it a try:
...
8
votes
2answers
576 views
Calculating how many Pomodori fit into a period of time
I've written some code to calculate how many Pomodori can fit into a period of time, including long and short breaks. I'm looking for alternative solutions because what I have at the moment doesn't ...
5
votes
2answers
121 views
Z-Algorithm for pattern matching in strings
I was trying to refactor the following Python code (keeping the same time-complexity) which is an implementation of Z-Algorithm for pattern matching in strings.
...
1
vote
4answers
632 views
2
votes
4answers
57 views
Base adder, given base and two numbers of that base, it adds them
Nothing more to add than the title. Looking for code review, optimizations and best practices.
...
2
votes
1answer
47 views
M coloring problem
Given an undirected graph and a number m, determine if the graph can be colored with at most m colors such that no two adjacent vertices of the graph are colored with same color. Here coloring of a ...
0
votes
1answer
85 views
7
votes
3answers
311 views
Operations on strings in Java
I have a program that makes operations on strings and this is the principal function of one of those operations. This works perfectly, but it is not efficient:
...
7
votes
4answers
294 views
Ruby-ize for loop - counting all the n-digit numbers that contain the digit 5 anywhere
I wrote this a while back when my fiance was taking a Number Theory class. I wrote about it here and it recently came back to my attention. Anytime I write a for ...
4
votes
1answer
71 views
Wordgenerator algorithm using Java collection
Problem Statement
Prompt the user for the order
statistic n: 1, 2, 3, etc.
Read a file of tokens, building a map
...
1
vote
1answer
78 views
Is this a true backtracking algorithm?
So my project was to make an algorithm that finds a path out the maze using "backtracking" to help. This is my first time attempting to solve anything with backtracking so I want to be sure if what I ...
9
votes
3answers
170 views
Calculating infix expression with no parenthesis
This is my Java implementation (\$O(n)\$) about how to calculate infix expression with no parenthesis.
For example, when we input "3 + 4 * 4 / 8", we will get a double type answer is 5.0. Here to ...
0
votes
0answers
9 views
High-level strategy for distinguishing a regular string from invalid JSON (ie. JSON-like string detection) [migrated]
Disclaimer On Absence of Code:
I have no code to post because I haven't started writing; was looking for more theoretical guidance as I doubt I'll have trouble coding it but am pretty befuddled on ...
7
votes
3answers
480 views
Multiply two hexadecimal values
I've written an algorithm which can multiply two hex values and return a hex as a result. What do you think about the below solution?
...
2
votes
1answer
37 views
Two squares or not two squares - constant time optimization
I have been trying to solve this problem, and after a bit of googling, I realised a \$O(\sqrt{n})\$ time complexity works fine (though the algorithm suggested is different from that I have used). My ...
3
votes
2answers
65 views
Haskell Collatz Conjecture
In an attempt to begin properly programming in Haskell, I wrote two functions for calculating numbers and sequences with Collatz's conjecture. I also commented the program quite a lot, because it ...
10
votes
7answers
2k views
Find the duplicate in a sorted array In less-than O(n)
Assumptions:
The array is sorted.
There is only one duplicate.
The array is only populated with numbers [0, n], where n is the length of the array.
Example array: [0,1,2,3,4,5,6,7,8,8,9]
...
2
votes
0answers
59 views
implementation of a new algorithm for sklearn
In the python library sklearn is implemented the algorithm for SparsePCA.
I have written the code for a another version of this algorithm that is much faster in some situations. I have not enough ...
7
votes
2answers
113 views
Iterate from “0” to “ZZZZZ”
I was given an assignment, it was as follows:
Give a JS code snippet which writes to the console from:
"0" to "ZZZZZ" all the combination of the characters of a-z, 0-9 and A-Z.
Is my solution ...
4
votes
1answer
50 views
Find element yielding the largest value w.r.t. custom function
I made a function to find the element that yields the largest value when put into a custom functor.
I think this is missing from the STL. I can think of three ways to do this in STL:
Transform the ...
4
votes
1answer
66 views
Image Processing codes running too slow
Situation
I've written a simple black and white image filter application with JavaScript using html5 canvas. Here is an example of the app . (I don't know why it does not work in jsfiddle, so I gave ...
3
votes
1answer
50 views
Conversion from postfix to infix in C
Just over half way through in K&R, came across an exercise to convert from postfix ("reverse-Polish") notation to infix (standard) notation.
Basically I'm posting here to see if there is ...
6
votes
1answer
125 views
Escaping the prison - finding the shortest way
I have given an assignment where I have to escape a labyrinth. The goal is to find the shortest way. I have done some research and there seem to be two strategies to solve the problem: the Depth-first ...
4
votes
2answers
66 views
Reverse sublists of a linked list
This code reverses each consecutive n elements of a linked list, and for spare nodes, leaves them as they are.
For example, the linked list
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
for an interval of 3 ...
4
votes
1answer
100 views
Optimize a zscore algorithm
I wrote a zscore algorithm in Ruby that runs fine, but is incredibly slow when I have 8000+ entries to score. Can anyone help me figure out a way to improve my code, please?
...