Recursion in computer science is a method of problem solving where the solution to a problem depends on solutions to smaller instances of the same problem.
5
votes
2answers
197 views
QuickSort of Comparable[]
Here is the code for the QuickSort class that I created.
public class QuickSort {
public static void sort(Comparable[] a) {
quicksort(a, 0, a.length-1);
}
...
5
votes
2answers
180 views
Relevant performance difference C#-VB.NET - Integer Division
C# code
static int KEY_NOT_FOUND = -1;
private void Form1_Load(object sender, EventArgs e)
{
int[] A = createArray(1, 100000);
Stopwatch sw1 = performCalcs(1, A);
Stopwatch sw2 = ...
2
votes
2answers
44 views
Algorithms for traversing unordered tree
I have been doing some programming 'exercises'. I'm trying to come up with the most efficient algorithm for tree traversal.
Consider the following function signature:
CNode * CNode::find(int ...
8
votes
1answer
65 views
Recursive calculator in C
This is a calculator I wrote for a student's project.
It still has a problem (one that I know of), where it can't handle inputs of the form 2*2*2... (more than one * or / sign). The case 2*2 was ...
7
votes
2answers
89 views
Math expression solver
Recently I've stumbled upon an interesting challenge for me.
You should develop C application, that solves math expressions. Operations such as (+,-,*,/) should be supported, as well as (cos, sin, ...
-1
votes
0answers
24 views
Continuous Fractions - [closed]
My understanding of continuous fractions was that the will always give a representation of a decimal in fraction form. I thought that continuous fraction would always return value less than or equal ...
6
votes
3answers
87 views
Iteration to recursive function
In my first iterative algorithm, I do this
for (auto &widget : controls.getWidgets())
{
if (!widget->visible) continue;
widget->draw();
for (auto &widget_component : ...
4
votes
2answers
58 views
Numeric expression parser - calculator
In particular, if someone could describe a better way to go from the tokenized list to the Expression tree, it would be super helpful. I would like to get rid of the casting in the parser but am not ...
12
votes
5answers
1k views
Identifying first negative number in array of doubles
I was asked to create a function that checks if an array of doubles has all negative numbers or none. If these 2 (all negative, all positive) are false, check the first negative number, using ...
6
votes
1answer
171 views
Tower of Hanoi solver
Requesting code review, best practices, optimizations for this solution to the Tower of Hanoi problem.
final class PlatePair {
private final String source;
private final String destination;
...
1
vote
3answers
109 views
To check if a string C is an interleaving of A and B Code
This is my code to check if a string C is an interleaving of Strings A and B. Please suggests optimizations, and where I can improve.
#include <vector>
#include <list>
#include ...
13
votes
4answers
558 views
Displaying each number of an integer in a sequence
One of the books I'm currently reading to learn C# asked me to write a method that takes an integer between 1 and 99999 and displays each number in that integer in a sequence, separating each digit by ...
0
votes
1answer
21 views
Generic sequence splitter in Common Lisp
I wrote a function split-seq-by-n which accepts a sequence and a number and splits the sequence into subsequences of length n (the last subsequence getting the remaining elements). It works on all ...
10
votes
6answers
564 views
Help refactor this to be less procedural and more recursive
Reference: Is this a faithful rendition of the selection sort algorithm?
I'm working through an elementary book on sort and search algorithms, and writing some sample code to test my understanding. ...
7
votes
2answers
128 views
Is this non-recursive mergesort efficient and a good way to simulate the recursive version?
My teacher assigned us to do the recursive version of mergesort, yet I heard that the non-recursive version (bottom up) is a bit tougher, so I decided to this one as well. My main concerns are:
...
5
votes
2answers
109 views
Optimize disk enumeration and file listing
I am writing C++ code to enumerate whole HDD and drive listing. However, it takes more than 15 minutes to complete the disk enumeration of all drives (HDD capacity 500GB) and compile the response in ...
2
votes
3answers
80 views
Python script to touch files of a particular pattern in all folders of a given name
The intent of this script is to hunt down all "*/testResults/*.xml" files and touch them, so that our build system doesn't issue errors that the files are too old (it's entirely intentional that tests ...
4
votes
3answers
80 views
Pair matching elements from two lists
Here is a method that takes two lists (l1 and l2) and pairs up elements that "match". As you can see at the end of the code block these lists consist of matching elements but in different indexes. The ...
4
votes
2answers
77 views
Recursive reverse function
Write a recursive version of the function reverse(s), which reverses the string s in place.
Here is my solution:
void reverse(char a[], int i, int j) {
char tmp;
if(i >= j) {
...
7
votes
3answers
108 views
Integer-to-string conversion using recursion
Adapt the ideas of printd to write recursive version of itoa; that is, convert integer into a string by calling a recursive routine
Here is the implementation of printd:
void printd(int n) {
...
-1
votes
1answer
52 views
Recursively calculating powers of numbers [closed]
In this program, I understood everything except power(x1,y1-1). I want to know how the power(x1,y1-1) function exactly works.
#include<iostream.h>
#include<conio.h>
int ...
1
vote
1answer
42 views
replacing explicit recursion with a fold
as an exercise I wanted to rewrite this toy python code in haskell.
def f(x):
return abs(42-x)**2
def improve(x):
newX = x + 0.1
return newX, f(newX)
def optimize(f, goal):
x = 0
...
7
votes
1answer
125 views
Suggestions needed for alternative methods of solving sudoku
I am new to programming as well as this site. I am learning Java as my first programming language. I have written a bit of code (of course after struggling a lot). I would love to read expert reviews ...
0
votes
0answers
49 views
REST Response: Checking for Success and Error
I thought this was pretty slick - maybe not, I just wanted to share and maybe get some feedback:
As part of a REST client class:
Accepts the JSON response from REST or REST-like service, searches ...
3
votes
3answers
203 views
Reversing a string in-place - recursion or iteration?
I am practicing my recursion skills as I think I am really weak in this area.
I have written the following Java class whose purpose is to reverse a string but to do it 'in-place' - i.e. no additional ...
4
votes
1answer
43 views
Using a function recursively to calculate higher-order derivatives
I am writing a code to do some numerical task using the routines of Numerical Recipes book. One of my objectives is to calculate the second derivative of a function and I have a routine that ...
3
votes
2answers
84 views
Custom UI - Seeing which control has focus
I'm trying to make a simple UI to better understand how interfaces and the System.Windows.Forms Controls behave.
Like the above namespace, I will be able to get and set the Focus on a particular ...
5
votes
1answer
78 views
A Program that Finds Empty Folders
I wrote a program that finds empty folders and subfolders in a directory. It's the first program I've ever written in Java, and I'd like some critique. I feel like I'm not utilizing classes in a very ...
4
votes
1answer
62 views
Printing all strings in a Boggle board
I am trying to print all the strings given in a N x N Boggle board. Its basically a N x N scrabble-like board where words can be made from a position - horizontally, vertically and diagonally.
Here ...
3
votes
1answer
85 views
Recursively convert a list of lists into a dict of dicts
As a challenge to myself, I wrote a solution to this Stack Overflow question. For completeness, the bulk of the question is as follows:
Problem
Input:
[
['key1', 'value1'],
['key2',
[
...
4
votes
2answers
146 views
Hanoi towers time exeeded
I am having problem with a simple Hanoi tower program. I need to do something to reduce its time.
#include <stdio.h>
#include <fstream>
using namespace std;
int hanojus(int n, char a, ...
8
votes
1answer
168 views
Sudoku Week-End Challenge - Brute-Force Recursive solver
This is part of my attempt at the Week-End Challenge #3. The overall problem is larger than will fit in one question. This is a well-contained subset of my larger program.
The goal of this part is to ...
3
votes
3answers
137 views
String Extension improvement
I am creating a string extension to validate some input.
My scenario is when we have a string it will format it according to following guideline.
If the sample string is “One Two Three Four Five” and ...
2
votes
1answer
48 views
lexical scope v let in scheme functions
I've been chewing through a bit of The Little Schemer and the Peano examples got me thinking about operation size and time.
I got some help on making Peano Multiplication linear -- however (time ...
1
vote
1answer
40 views
Linear Peano multiplication in Scheme?
I've been running through The Little Schemer, and have hit the example of Peano multiplication. The solution is given in TLS (reproduced below) -- however what interests me is the order of the ...
4
votes
2answers
138 views
Count possible paths through a maze
You have to find a path through which the rat move from the starting
position (0,0) to the final position where cheese is (n,n). List the
total no of possible paths which the rat can take to ...
1
vote
1answer
75 views
Checking user permission, where permission is granted for specific EmployeeGroup that has self reference
The users get permissions to do specific things for a specific group of employees. These employee groups have a self reference. So an employee group (C) can be a child of group (B), and group (B) is ...
3
votes
1answer
86 views
Better way to create samples
I've done this piece of code for creating all possible samples without repetitions, but I think that it is a so heavy algorithm because every time I call this recursive function I create a new vector ...
2
votes
1answer
52 views
Given set of cubes can we get a given word?
Given some cubes, can cubes be arranged such that an input word can be formed from the top view of the cubes?
For example: assume an imaginary cube with only 3 surfaces where cube1: {a, b, c} and ...
6
votes
1answer
879 views
Find all subsets of an int array whose sums equal a given target
I am trying to implement a function below:
Given a target sum, populate all subsets, whose sum is equal to the target sum, from an int array.
For example:
Target sum is 15.
An int array is { ...
3
votes
5answers
206 views
count all array[index] == index occurrences
The method foo gets a sorted list with different numbers as a parameter and returns the count of all the occurrences such that: i == list[i] (where i is the index 0 <= i <= len(list)).
def ...
3
votes
1answer
58 views
I've written remainder(x,y) in OCaml. Is there more efficient than O(n)?
Here's my code:
let rem x y =
let rec aux acc i n =
if i=n then acc else
if acc+1=y then aux 0 (i+1) n else
aux (acc+1) (i+1) n in
aux 0 0 x;;
I'm just learning OCaml and I wonder:
...
1
vote
0answers
192 views
How to optimize this maze solver?
I've written C# code for solving a maze problem. It works, but I want to optimize it so that it could give me all paths. To solve the maze, I walk on the zeros and assumed that the final is reaching ...
4
votes
0answers
57 views
Optimize RecursionArray interface
I created a class a while ago that I named a "Recursion Array". It is a way to create dynamically memoized sequences such as the Fibonacci sequence or the factorial numbers sequences. The concept is ...
3
votes
1answer
69 views
Is a Recursive-Iterative Method Better than a Purely Iterative Method to find out if a number is prime?
I made this program in C that tests if a number is prime. I'm as yet unfamiliar with Algorithm complexity and all that Big O stuff, so I'm unsure if my approach, which is a combination of iteration ...
2
votes
2answers
57 views
Move Line across Plane
I want move a Vertical line segment across a plane or another way sweep the plane, from Left to right.
The figure illustrates how the segment is moving at the X-axis. When x1 >= X beginning and i ...
1
vote
1answer
55 views
Recursive GCD using a lambda
This works just fine, however, the assignment was to write a recursive "function". I'm curious to see if this should count.
Any comments / suggestions/ stuff I should watch out for are appreciated. ...
2
votes
2answers
365 views
Finding a route in maze matrix
Maze puzzle
A 1 in input matrix means "allowed"; 0 means "blocked". Given such a matrix, find the route from the 1st quadrant to the last (n-1, n-1).
I would like to get some feedback to ...
2
votes
2answers
62 views
Optimizing odds calculator
I am trying to setup an odds calculator for a best of 7 series, given independent odds for each event.
The following code works, but I would like to add recursion to simplify the end.
public class ...
2
votes
2answers
852 views
Boggle solver in Java
This is a boggle solver in Java. I would like code reviewers to optimize the code, suggest better coding practices, and help make it cleaner.
I am also unsure of the complexity and would appreciate ...