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.

1,073 questions
Filter by
Sorted by
Tagged with
158 views

Noughts and Crosses Terminal Game with AI

I created a game which can play tic-tac-toe with: 0 players (AI vs AI), 1 player (human vs AI) or 2 player (human vs human). Here is the AI function which wraps around my innermost function. This ...
119 views

Recursive brute-force approach to maximum points you can obtain from cards

I came across this question on Leetcode. The question description is as follows: There are several cards arranged in a row, and each card has an associated number of points. The points are given in ...
99 views

Delete a linked list using recursion in Python

Given a Singly Linked-List, write a function to delete the linked list recursively. Below is my attempt. I am confused if I should use del keyword to delete the nodes or below method is better. I don'...
100 views

Recursive function with Promise and setTimeout to ping DOM

I use this function in browser automation. It pings the document in the browser to check if an element we are looking for is available (useful when there is JavaScript generated async content). If it'...
89 views

Max Contiguous Subarray: Divide and Conquer

I am conversant with Kadane's Algorithm. This is just an exercise in understanding divide and conquer as a technique. Find the maximum sum over all subarrays of ...
130 views

Traveling salesman to find beer types from breweries

I am writing a modified travelling salesman problem recursively in Python 3.7. It has to find the best path around breweries to find the most beer types and not to exceed the ...
66 views

recursive file search, C , linux

hi guys this script recursively searches for files with a certain extension and possibly in a directory passed by the terminal ....(In case no directory has been passed, search from the current one) I ...
431 views

Recursive os.scandir() for copying/moving files preserving subdirectories

This is modified from the general recursive solution here: https://stackoverflow.com/a/54544211/42346 to, more specifically, copy/move files with a given extension from one directory to another whilst ...
939 views

Finding The Largest Digit in a Number using a Recursive Function in Python [closed]

I tried it using the code given below. ...
219 views

Speeding up a recursive Cantor pairing function

The Cantor Pairing function is a mathematical function which takes two integers and combines them into a single integer that is unique to that pair. This single integer can later be "unpaired" back ...
107 views

Time and Space Complexity of Leetcode Problem #31. Next Permutation

The question is as follows: Given a collection of distinct integers, return all possible permutations. Example: ...
30 views

How to convert Recursive Approach to iterative one(Super Reduced String)? [closed]

The Question: Steve has a string of lowercase characters in range ascii[‘a’..’z’]. He wants to reduce the string to its shortest length by doing a series of operations. In each operation he selects a ...
841 views

Recursive vs. non-recursive implementations of Fibonacci sequence

I have 2 functions to get the n-th fibonacci number. The 1st one uses recursive calls to calculate the power(M, n), while the 2nd function uses iterative approach for power(M, n). Theoretically (at ...
103 views

Simulating the orbits of the Earth, Moon and Sun together in Fortran

This has been racking my brain for hours now. I will try to explain as much as I can. I'm aware this code is horrendously ungraceful, but I'm new to Fortran and making things more efficient is not ...
113 views

Recursion - Max of integers in slice

Logical processors available on my machine: CPU(s): 4 Thread(s) per core: 2 So, 2x4 = 8 logical processors. But am currently using only 4 go-...
351 views

JavaScript Sudoku Recursive Solver

Thanks for all the great feedback in Part 1. I implemented a lot of it. Here is version 2. I am looking for feedback on: Recursive solve algorithm. It's too slow. I used Chrome DevTools Performance ...
144 views

N-Queens Puzzle

The N-Queens puzzle can be solved in many ways. One is by a depth-first-search backtracking algorithm. Below is my generalized version using mutual recursion: ...
58 views

Function for iterating out an unordered list of custom taxonomy terms

I've created a function that generates an <ul> of my wordpress custom taxonomy terms, displaying ...
836 views

Counting the number of digits with a recursion algorithm in c

I'm learning recursion and I think that I can finally wrap my head around it. I had to write a program that given an integer would print out its number of digits and I wanted to know if it is ...
465 views

Recursively Convert Roman Number to Integer

I'm practicing recursive functions and I would be glad to receive some feedback for the following Python code I wrote. I did several tests and the function yielded correct results but I wonder if it ...
487 views

Sudoku solver recursive backtrack in Java

I am implementing recursive backtracking algorithm for sudoku. Below is the code to create board with random filling. I wonder if I can make it better in any terms of readable coding especially ...
321 views

DFS with recursion and without stack

this is my code to implement DFS with recursion but no stack. I ran few test cases which turned out to be positive but wanted to check the efficiency of the code. ...
142 views

"Smart" sudoku brute-forcer

After running my C-brute-forcer for two days on a 25x25 sudoku, I decided to re-write in Java so that it would solve it in an acceptable time frame. The idea: Make smart fields that contain all ...
156 views

Longest Substring without Repeating Characters Problem - Kotlin Recursion

I am practicing coding questions on leetcode. I have been an OOP my whole career, and I am trying to wade into the dark abyss that is functional programming. So I am trying to do things purely ...
3k views

Permutations with replacement in Python

Many a times, I've had the need to use a permutations with replacement function. So, I've written a function to do just that: ...
172 views

generic implementation approaches for a recursive function

Let's take the famous fibonacci problem as an example but this is a generic problem. Also, taking scala as the language as it's rather feature-rich. I want to know which solution you'd prefer. we all ...
955 views

Factorial number in Rust for learning purposes

I'm currently trying to learn the basics of the Rust programming language, to do this my first piece of code after the classic 'Hello World' was a simple recursive Factorial function. This is the ...
830 views

Printing the number of ways a message can be decoded

My below solution to the following problem is exceeding the maximum recursion depth. I am looking for any improvements which i can make. Problem Alice and Bob need to send secret messages to each ...
3k views

Python: pass "mutable integer" in recursion

I'm working on some code to find the max depth/height in a tree. I came up with a recursive solution, and I find myself needing to pass an integer "by reference". Currently, I'm using a mutable ...
264 views

Continued fraction with relative precision in Python

I have the following recursive function to compute the continued fraction: $$s_i = \frac{a_{i}}{b_{i} + s_{i+1}}$$ The relative error is defined as: $$\text{Delta} = \frac{s_n - s_{n-1}}{s_{n}}$$ ...
2k views

Binary Search Tree Using Templates in C++

I have made this BST using templates Node.h ...
109 views

I was recently tasked to write an implementation of the Berlekamp–Massey algorithm (for GF2) from a given inductional proof and decided to fresh up on my Haskell, since converting a proof by induction ...
421 views

Sudoku Solver questions on style and algorithm

I am working towards a CSC degree right now and wanted to try and practice writing good code that could be easily maintained and read in industry. I wrote the following program in Java that takes a ...
316 views

Recursive function to get all Zendesk tickets

I'm developing a Python library to consume the Zendesk search API. Problem is, Zendesk limits search results to 1000 items. Thus in order to get all the results if they are greater than 1000 I need to ...
62 views

I just wanted to ask the following question. I have solved Kadanes algorithm using the recursive approach as shown below. The implementation works ...
1k views

Minesweeper - Python 3 (beginner)

I’ve just created Minesweeper game, which work perfectly fine (for me). Any suggestions on how to improve this code would be greatly appreciate, in terms of: Object-oriented Array Error handling ...
691 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 ...
2k views

Recursion function for prime number check

I have this code: ...
230 views

Recursive Mergesort using goroutines

When learning a new language, one of my first tasks I like to go through is writing a few different sorting algorithms in that language to help familiarize myself with the syntax and see if there are ...
51 views

recursive function improvement

I am creating routes from an array of menuItems. I have written a recursive function, which works fine but I think the code can be improved. So, I need you guys to review my code and suggest me some ...
247 views

Recursion on graphs - networkx VS adjacency lists with native lists, sets or numpy vectors ( python )

Hope someone can help me out in reviewing this code to make it faster. My goal is to create a graph recursively: query for a node and its neighbors from a db (not present here) add it and its ...
50 views

Don't include root directory in HTML tree

I've written a function to create an HTML tree from JSON. ...
48 views

interpre function implementation

Here is the description of the problem. Write a simple interpreter which understands "+", "-", and "*" operations. Apply the operations in order using command/arg pairs starting with the initial ...
15k views

Brute-Force algorithm in C++

I wrote a brute-force algorithm which shall find all possible combinations of ASCII-values that can sum up to a specific value (int hashval). The algorithm is ...
92 views

Fibonnaci sequence calculation [closed]

Below are two ways to calculate an element of the Fibonacci series. Can someone please help me understand why fib1 is so much faster (over 60x) than ...
61 views

Recursively find files of certain types and log their paths (C++)

I've written a function that takes a list of extensions and recursively finds files of those types, and logs their paths to a text file. Usage example (finding image files in a home directory): <...
36 views

File and folder filter for a "last-updated explorer"

Sometimes I save a file somewhere but can't find it after or I install something and want to see what files were added. For this reason, I created a winforms app which only displayes files created/...
71 views

User defined function call on a stack based interpreter for concatenative language

I would like to receive some advice on how to improve a small concatenative stack-based interpreter, executing a joy-like programming language. It is really minimal, and is in fact a subset of a ...