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.
1
vote
1answer
33 views
Non-Contiguous Substrings
Problem:
A non-contiguous substring of String s is a sequence of k >= 0 characters in s, in the order in which they occur in s. For instance, the set of all ...
4
votes
1answer
19 views
Test whether target string is substring of source string
The code is self-explanatory. It has \$O(n)\$ complexity, and it tells whether a given string is the substring of the source string. Are there any cases where this algorithm fails? Is there anything ...
3
votes
3answers
197 views
Merge two list and discarding duplicates
I am trying to implement a function that merges two ordered lists into a third (ordered) one, but duplicates have to be discarded (basically the last step of a mergesort).
I think that this code can ...
2
votes
1answer
20 views
My own implementation of an hashtable
This is my own implementation of an hashtable. I would like to receive some reviews or some feedbacks.
...
6
votes
2answers
68 views
Sudoku game with varied difficulty level
I created a Sudoku game with varied difficulty level.
The Sudoku board creation process consist three stages:
Creation of an empty board
Filling the board with numbers
Making holes in the filled ...
1
vote
1answer
42 views
Check if a Binary Tree is full
Problem statement
Given a node check whether the sub-tree rooted at given node is full binary tree or not.
Definition
A full binary tree (sometimes proper binary tree or 2-tree) is a tree in ...
2
votes
3answers
57 views
Creating a tuple from a CSV file
I have written code that reads in a CSV file and creates a tuple from all the items in a group. The group ID is in column 1 of the table and the item name is in column 2. The actual datafile is ~500M ...
6
votes
4answers
185 views
Add one to a very large integer
I was asked to implement this in an interview a while back. I didn't pass the interview, mostly likely because it took me far too long to do. However, I'm interested to know how well implemented the ...
3
votes
1answer
44 views
Finding most common contiguous sub-lists in an array of lists
Objective: Given a set of sequences ( eg: step1->step2->step3, step1->step3->step5) ) arranged in an array of lists, count the number of times every contiguous sub-lists occur
Where I need your help:
...
2
votes
2answers
422 views
Efficient algorithm for counting frequency of a numbers in intervals
I need to build a bar graph that illustrate a distribution of pseudorandom numbers that determined by linear congruential method
$$X_n+1 = (a X_n + c) \mod m$$
$$U = X/m$$
on the interval [0,1]
...
2
votes
1answer
64 views
Inversion count via divide and conquer
I'm happy to hear thoughts and ideas on structure/performance/testing/whatever and multi-threading, which I haven't gotten into yet with Python.
Full code here. Assignment file and a test file ...
4
votes
1answer
61 views
Knights Tour — Something I am doing wrong
I have an assignment to write a knights tour backtracking program in Java where the professor says to:
Eliminate backtracking time by:
...
5
votes
2answers
69 views
Knuth–Morris–Pratt string match algorithm
The Knuth–Morris–Pratt string search algorithm is described in the paper Fast Pattern Matching in Strings (SIAM J. Computing vol. 6 no. 2, June 1977). The initial step of the algorithm is to compute ...
-3
votes
0answers
25 views
k permutations of n integers without repetitions [on hold]
I already have code for k combinations of n elements. How can it be edited to produce k permutations of n elements without repetitions (\$0 \le k \le n\$)?
How can I make minimal changes to the exact ...
5
votes
1answer
49 views
Project Euler no. 17: Counting letters to write the numbers from 1 to 1000
I'm very new to programming and am admittedly embarrassed sharing my code for critique. This code works and produces the correct answer to the Project Euler Problem 17, which asks how many letters ...
12
votes
5answers
422 views
+100
Algorithm to transform one word to another through valid words
I have been practicing backtracking and I wanted to know how I can improve my code. For eg, I don't want to use global. Also, have I am not sure if my code will work for all the cases.
...
8
votes
4answers
384 views
Simple GCD utility in Java
I previously discussed the performance concerns about different GCD algorithms. I wrote a simple Java class that implements the Binary GCD algorithm. It is a really tiny class that has only two ...
-3
votes
0answers
24 views
Length and sum of longest increasing subsequence [closed]
I wanted to count sum and length of the longest subsequence in the given array t.
...
4
votes
1answer
42 views
Find values in list which sum to a given value within tolerance
This is really a follow on from a question I asked earlier this year on Stack Overflow. I received a great answer to the following problem:
I'm trying to code up something simple and pythonic to ...
4
votes
2answers
80 views
Sort array of Numbers with some values always first
The first number will be dynamically selected and remaining array should be sorted ascending
...
5
votes
2answers
57 views
Reorder a list in Python to avoid repeats
I am trying to check if a list has any consecutive repeating elements and then reorder it such that the repeats are avoided. If that is impossible, then return False. For example:
...
-3
votes
0answers
22 views
Reversing the words in a string. [closed]
I am having trouble understanding why my program isn't working as intended. Here is the code :
...
7
votes
3answers
79 views
1
vote
1answer
40 views
Array search algorithms: performance comparison
I'm making a program that compares multi-key sequential search and Interpolation search in a sorted array by the number of array accesses for my assignment.
...
4
votes
2answers
124 views
Find co-occurrence of elements
My input consists of a list of lists (not necessary of the same length), like
...
1
vote
3answers
73 views
Merging two sorted linked lists C++
Routine to merge two linked lists together and return a new merged list. One optimization I can think of is passing by reference instead of value. Any other suggestions ? Is there a shorthand to make ...
2
votes
2answers
80 views
Gluing pieces of scrambled text (challenge on CodeEval)
I'm trying to solve this challenge on CodeEval. Quoting:
For example, you can put pieces together and get the original text:
evil pl
vil pla
il plan
The answer is ‘evil plan’.
Your ...
7
votes
2answers
236 views
Generating 3 combinations in Python
I have been experimenting around with generating all the n-combinations of an array. This code quickly generates all \$\text{k-combinations}\$ of a given array. I am testing my own implementation ...
3
votes
3answers
59 views
Max and Min in array using minimum comparisons
Is this the most robust and fastest way for finding the min and max out of an array without using STL functions? How can I improve it?
...
1
vote
1answer
37 views
Kruskal's algorithm
I'm working through Coffeescript implementing simple algorithms (started with Prim's as reviewed in this previous post) and wrote out Kruskal's algorithm as below, with a few helper functions. I'd ...
4
votes
0answers
58 views
Directed graph isomorphism in Java
Given two input directed graphs \$G_1 = (V_1, A_1)\$ and \$G_2 = (V_2, A_2)\$, the problem of isomorphism asks whether the two directed graphs are isomorphic, or in other words, whether the two input ...
2
votes
0answers
30 views
Are AVL trees equal? - revision 3
The original question
Given two binary trees, return true if they are structurally
identical, and false otherwise.
Are AVL trees equal?
Are AVL trees equal? - revision 2
This revision on ...
4
votes
1answer
94 views
How to tweak Tape Equilibrium assignment on Codility to be of complexity O(N)?
Why Codility detects my solution for Tape Equilibrium to have complexity of \$O(n^2)\$. How can I make my code better?
...
3
votes
2answers
44 views
Caesar Encryption Tool
I'm newbie in Python and I wrote a tool called Caesar Encryption Tool. At the moment it only encrypts. It takes a plain text and a key from the user then encrypts the plain text using the key. How can ...
5
votes
1answer
82 views
Calculating count of square numbers between two numbers
The problem is to find out the squares between two numbers, inclusive of the numbers. The two numbers are in the range between 1 and 109.
...
2
votes
3answers
63 views
Prim's algorithm in CoffeeScript
I'm just starting to learn CoffeeScript and am trying to work through simple examples, this one being Prim's algorithm. I'd like feedback on everything but especially on making this script take ...
4
votes
3answers
123 views
Kadane's Algorithm for 2D array with known boundaries
I asked this question first on StackOverflow but I didn't get an answer and was advised to try here too. So here we go.
I have implemented Kadane's algorithm for a 2D array in Python 2 with known ...
13
votes
3answers
158 views
What kind of Wall should it be? (for an RPG)
I'm creating an RPG type game, and now I am working on procedural town generation. The algorithm for that creates some roads going horizontally and vertically, and then attempts to place buildings in ...
0
votes
1answer
30 views
Taking a node in rootA and finding a clone node in other tree
I was asked this as an interview question. I was also asked to assume that there is a function which can compare and say two nodes are identical. I would like to implement that function as well. I'm ...
2
votes
3answers
69 views
Reverse part of a linked list
I'm learning data structures. I'm working on linked lists at the moment, and I'd like to have a strong grasp on them. Below is the last problem I solved. I'd like to know what you think of the ...
4
votes
4answers
130 views
Dividing a range into N sub-ranges
Introduction
The function divides a range given by a begin_iterator and an end_iterator into ...
4
votes
2answers
61 views
Find if the prefix of the string exists in the values of the hash table
I have a hash map which maps to some strings which serve as prefixes and are of small length (max length is 6):
...
5
votes
1answer
58 views
Find magnitude pole of an array
Please review this code, considering I put emphasis on constraining space complexity at the expense of execution speed (accepting additional branching).
Can you see cases that prove the algorithm ...
3
votes
2answers
41 views
Rosalind string algorithm problems
I've been starting to learn Rust by going through some of the Rosalind String Algorithm problems.
If anyone would like to point out possible improvements, or anything else, that would be great. There ...
1
vote
0answers
19 views
First Element not being extracted right in a Max Binary Heap [closed]
I implemented a Max Binary Heap. However, When I extract the values, the first element extracted is wrong and the values after that are extracted correctly. As a matter of fact, the first element ...
4
votes
4answers
90 views
“Tractor” Python implementation
This is the Tractor problem from the Kattis problems archive.
Problem Statement
Bessie the Cow has stolen Farmer John’s tractor and is running wild on the coordinate plane! She, however, is a ...
3
votes
2answers
70 views
“Cut the sticks” Python implementation
Problem Statement
You are given N sticks, where the length of each stick is a positive integer. A cut operation is performed on the sticks such that all of them are reduced by the length of the ...
1
vote
0answers
10 views
Forth to the past - mergesort preps
Still oiling my Forth. Below are few essential algorithms for in-place merge. Please review.
Some introductory comments:
>swap< is not a cute smiley. I am ...
6
votes
3answers
942 views
“Angry Professor” Python implementation
Problem Statement
The professor is conducting a course on Discrete Mathematics to a class of N students. He is angry at the lack of their discipline, and he decides to cancel the class if there ...
4
votes
1answer
82 views
Ad-hoc lexical scanner
Challenge:
Build an ad-hoc scanner for a calculator language.
Specifications:
The tokens for the language are as follows:
assign → :=
plus → +
minus → -
times → *
div → /
...