# Questions tagged [recursion]

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.

952 questions
Filter by
Sorted by
Tagged with
7k views

### Print all permutations with repetition of characters

Given a string of length n, print all permutation of the given string. Repetition of characters is allowed. Print these permutations in lexicographically sorted order Examples: Input: AB ...
11k views

### Recursive implementation of palindrome string checker

I've tried to refactor my program to single return statement at the end of the program, however, it ruins the end statements of the recursion because I want to return from function in specific line ...
3k views

### xml is XML is xml is XML

I had to write some code for an interview, to print out the name attribute of each node. I started off going in the wrong direction, so I didn't get to finish. I wanted to finish writing the code, so ...
18k 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 ...
1k views

### Recursive Fibonacci with Generic delegates

Here is a fast recursive Fibonacci-like for loop. How can it be more readable, and is it possible remove TArgs? ...
4k views

### Printing permutations of a given string

I want some feedback on the program I developed in C for printing the permutations of a given string. ...
27k views

### Recursive function that generates the permutations of a string

I am looking for a review of my recursive function that generates the permutations of a string. Are there better ways to do this? ...
847 views

### Finding path in Maze

I recently gave an interview and was asked the following question. I posted this here Finding path in Maze also, and someone suggested I should try Code Review. So here it goes. A maze is a group of ...
542 views

### Sudoku solver: pencil marks & recursive patience

Here is my attempt at Weekend Challenge #3. Out of scope: It only will do 9*9 It will stop at the first solution Could have been smarter at re-using the cache when recursively calling ...
2k views

### Arranging 8 queens on an 8x8 board

Print all the ways of arranging eight queens on an 8x8 board. Rules: Queens should not share the same row, column, of any diagonal. Any comment on my solution? I didn't write unit tests but I ...
273 views

### Undo format when format disappears

I was posed a question as follows: Given a sentence like "John is a bad man", lets say all the formatting disappears and you are left with one string "johnisabadman". Given a dictionary of words, ...
148 views

### Queens placement

The code below is intended to be a learning material for high school intermediate programming class, as an introduction to recursion and backtracking. I was thinking of rook placement and found it too ...
401 views

### You need to diversify your strings

Challenge: Write a program which prints all the permutations of a string in alphabetical order. Specifications: Your program should accept a file as its first argument. The file contains ...
3k views

### Project Euler #14 — longest Collatz sequence

I was reading this question and realized that I didn't know Python well enough to critique the algorithm. So I wrote a Java solution instead, which is inappropriate for that question. Since there's ...
3k views

### Counting number of eights

I was doing a few basic Java recursion problems to refresh my memory with recursion (honestly never had to use it in a long, LONG, time), along with using it as a useful way to prepare for any ...
3k views

### Efficient pathfinding without heuristics?

Part of my program is a variable-sized set of Star Systems randomly linked by Warp Points. I have an A* algorithm working rather well in the grid, but the random warp point links mean that even though ...
8k views

### Format an integer with thousands separator (recursive implementation)

The function accepts a number n and displays it recursively with commas. As well as a general code review, I'm interested in suggestions for alternatives to ...
5k views

### Base 26 (letters) and Base 10 using recursion

I was messing around with base conversion between decimal and Base26 (alphabet) with no 0th case because I needed it for the Google Sheets API in order to find specific cell values from a 3 ...
3k views

### Generate all valid combinations of n pair of parentheses

Implement an algorithm to print all valid (e.g. properly opened and closed) combinations of n pair of parentheses. Following is my implementation and I think it works. Is the algorithm efficient? ...
3k views

### Recursive search on Node Tree with Linq and Queue

I've created a Node class which contains two important properties: public Node Parent { get; private set; } ...
2k views

### Merge sort using sentinels

This is my implementation of recursive merge sort, using sentinels: ...
8k views

### Path finding algorithm using recursion in Python

Here is a code I developed some time ago. I wonder if there is anything I can do to improve it. It works for non-loopy mazes which was already my goal. In the maze defined, ...
327 views

### Number in words: recursive function

I would appreciate some feedback on the below code I wrote for a Project Euler problem. The answer comes out correct but I'd like feedback on the design of the function and implementation of ...
245 views

Recursive version. The only interesting function: ...
199 views

### Better or more idiomatic way to apply a sequence of functions in Haskell

This is one of my Haskell solutions to a variation of the N-Queens problem, where the queens can move like knights in addition to their normal moves. It has been optimized somewhat to avoid ...
127 views

### Tower of Hanoi with helper function

Here is my solution to the Tower of Hanoi problem using Python ...
499 views

### Animate and play sounds in Simon Says 'the Angular way'

I am a newbie coder and I'm trying to learn good coding habits. I'm making a Simon Says game challenge from Free Code Camp in Angular JS. Codepen I have an array that keeps random integers between ...
3k views

### “What is the maximum that Player 1 can win?”

This is a question that I encountered in one of the competitive coding tests. The question goes as follows: A 2-player game is being played. There is a single pile of stones. Every stone has an ...
769 views

### Function to build a string based on the recursion depth

I was doing a JavaScript challenge to test myself and I succeeded in finding a solution but the code is very ugly. I was hoping to get some opinions on what a better solution would look like. The ...
2k views

### Entry-level recursive Sudoku solver

I've been asked to attach a C++ code sample to my application for an entry-level C++ programmer position in a videogame company. I've created a simple, yet complete console application to solve ...
6k views

### A beautiful and easy way of permutation with recursion

I want to share a better version of permutation with you. Yesterday, when I was reading Horowitz's Fundamentals of Data Structures In C++, and I noticed that the result of the code, at page 32, in ...
2k views

### Python traverse a directory recursively and print contact numbers

I am writing a python code to recursively traverse a directory containing only text files and then print the 10 digit Indian phone number and all its variants. I am very new to python and I have ...
2k views

### Stock Pipe Cutting using Recursion

This problem is from the Stanford Open Course CS106B Programming Abstractions Assignment #3 Problem number 6 here. "You are charged with buying the plumbing pipes for a construction project. Your ...
15k views

### Recursive binary search in Python

I have implemented a recursive binary search in Python and tried to implement some verification to my code. Other than that, is there any optimization I am missing? ...
1k views

### Counting Increasing Subsequences of size K recursive

The original description of the problem can be found here, its input here and its expected output here. The problem: Given a integer sequence a1 ... an (-10000 $\leq$ ai $\leq$ 10000). Where ...
6k views

### Travelling Salesman in Qt

I am writing a recursive function based on the Travelling Salesman Problem. Is this a correct way of doing things, or am I creating memory leaks? Prerequisites are: a two dimensional matrix ...
1k views

### Fibonacci in linear time by using an extra pointer

I have a function to find the nth number in a Fibonacci sequence, in which I am recursively calling the function. The sum is stored in a class variable and I have an extra pointer I increment every ...
535 views

Okay, so I just started learning Haskell around a week ago and this is my first real program that I worked on all of yesterday with a lot of help from IRC. I know that using indicies and arrays is not ...
5k 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 ...
4k views

### Recursive string reverse function

I'm studying C on K&R and now I'm facing a recursion exercise that states: Write a recursive version of the function reverse(s), which reverses the string s ...
939 views

### Tic-Tac-Toe Recursive Solver

I am attempting to create a Tic-Tac-Toe game that uses a recursively checks every possible game board, and finds which boards result in a victory for X. (I ultimately plan to turn this into an AI ...
2k views

### N-Queens Recursive Implementation in C++11

I created a recursive, optimized, N-Queens implementation in CPP. Any suggestions for improvement? ...
7k 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 ...
323 views

### Recursive parser from Binary to JSON output

Background I got this interview test and got declined due to not meeting their expectations, but never got a reason on what was bad and how they solved it. I want to improve and learn something out ...
812 views

### Finding permutations of a word

This was a basic anagram problem. Take an input word, find all its permutations, then check a dictionary file to see what if any of those are real words. Dictionary file My input data: ...
6k views

### Recursively evaluate neighbors in a two dimensional grid

I'm creating a puzzle mini game where there is a board of gems, varying in color, and chosen at random. This is what my code generates: When you click on a gem the game should traverse the board and ...
816 views

### Recursive search for combinations of words that have a specified MD5 hash

This code solves a challenge problem that a local company is hosting to attract/screen applicants. The link to the challenge is here. In brief, we are given a wordlist containing approximately 100,...
165 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 ...