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.

Filter by
Sorted by
Tagged with
3
votes
0answers
18 views

Mutual recursion - Naming the opponent serve - pingpong

Problem statement Ping-pong The ping-pong sequence counts up starting from 1 and is always either counting up or counting down. At element k, the direction switches if k is a multiple of 7 or ...
1
vote
0answers
16 views

I tried to built a Valid Sudoku Generator without Backtracking in JavaScript

I have 120 days worth of experience after discovering GitHub. In other word, I am new to programming. Earlier, I tried to write a valid sudoku solver with backtracking, the performance is ...
2
votes
0answers
38 views

Euler Transform on Leibniz Series of Pi

So i read about simple series for Pi named Leibniz Series for Pi. Here is its Link:- Series . I made a program for it and soon realised that it converges very slows . So i did Euler accelerate for ...
2
votes
1answer
56 views

Python recursive function that receives list of strings, and returns a nested list of anagrams

I'm learning Python and would appreciate any (and all) feedback to improve myself. input/output example: ...
4
votes
1answer
75 views

Shank Transformation to Lebniez series to find Pi recursively

I found a simple series named the Lebniez series for finding Pi. $$1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \dots = \frac{\pi}{4}$$ I made this in Python. I then realised that ...
0
votes
0answers
25 views

SPOJ MIXTURES - Mixtures [closed]

I am trying to create recursion based solution for Mixtures and i am getting correct ans for every possible test case i can figure out but get Wrong answer on spoj, what i am missing ? NOTE: I know i'...
1
vote
0answers
39 views

Tree kind of Object. Adding and Removing children

I am trying to work with a list of Person objects where the Person object has a property called "children" which is an array of same type. I idea is to have methods called “addChildrenToMain”...
3
votes
2answers
120 views

Refactor multiple if-else conditions when condition is a minor change

I'm new programmer and I'm working on Xamarin MVVM app and I have a pin view like So, basically I have numbers from 0-9 if you pick one number its visible then if ...
1
vote
2answers
71 views

Find pair in array with maximum sum using recursion

Not sure the best way to solve this using recursion. Below is what I have tried so far: ...
1
vote
0answers
27 views

Solving the game of 24 recursively in APL

The "game of 24" as I called it in the title is a maths game in which you are given four numbers and have to combine them in an expression using only the four basic arithmetic operations <...
2
votes
0answers
43 views

Linked tree built from (n) leaves

Firstly, here is the relevant code. It is written in kotlin. ...
1
vote
1answer
75 views

Best way to store the sum of all node depths?

To sum up all the nodes' depths in any given binary tree, I've written the following recursive algorithm: ...
1
vote
1answer
41 views

Recursive Function to Produce Constrained List of Integer Partitions

I have modified a recursive function designed to print all the integer partitions of a positive integer to create a function that returns all partitions of the "number" parameter if they ...
3
votes
1answer
47 views
0
votes
1answer
54 views

Recursively displaying json data on website [closed]

I am displaying the data from a json file onto a website, with recursive and iterative tables side by side. i have the iterative code working, but I have no idea on how to make it recursive. Here is ...
5
votes
1answer
71 views

How to merge two descending singly linkedlist into one ascending linkedlist using Recursion without any helper method?

Here is my solution: I want to know if my solution fits the requirement 100%? Or if there is any better solution? Constraint of the question: must be singly linkedlist must use recursion, and no ...
1
vote
1answer
47 views

Recursive Matrix Multiplication Algorithm

Can I improve on this any further. Is there a prettier way of passing the array sizes? ...
4
votes
1answer
91 views

Minimax based Tic Tac Toe

I am attempting to make an unbeatable Tic Tac Toe game using a simplified minimax algorithm. The code looks like this: ...
2
votes
1answer
34 views

What is the most optimal way to concat XML files as one large string/text file?

Python3 - Recursive XML Files to Large Text File I have a recursive function that looks through a directory (there are 6000+ files). For each XML file found - the ...
5
votes
2answers
110 views

How to optimize Karatsuba algorithm (using arraylist and java)

I was using Karatsuba algorithm to multiply two polynomials and return the coefficients and I am using java, we are asked to use arraylists here, however, my code is too complicated and it takes much ...
3
votes
0answers
25 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 ...
1
vote
2answers
58 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 ...
1
vote
0answers
27 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'...
2
votes
1answer
38 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'...
3
votes
2answers
43 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 ...
5
votes
2answers
120 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 ...
1
vote
1answer
51 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 ...
3
votes
1answer
37 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 ...
-4
votes
1answer
67 views

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

I tried it using the code given below. ...
1
vote
1answer
46 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 ...
2
votes
1answer
31 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: ...
-3
votes
1answer
22 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 ...
4
votes
2answers
751 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 ...
3
votes
0answers
58 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 ...
1
vote
0answers
82 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-...
2
votes
1answer
128 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 ...
3
votes
0answers
111 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: ...
2
votes
2answers
53 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 ...
2
votes
3answers
99 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 ...
4
votes
1answer
62 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 ...
4
votes
2answers
116 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 ...
0
votes
1answer
36 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. ...
2
votes
2answers
132 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 ...
3
votes
1answer
65 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 ...
4
votes
2answers
643 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: ...
2
votes
0answers
149 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 ...
0
votes
1answer
150 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 ...
7
votes
1answer
236 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 ...
11
votes
5answers
2k 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 ...
2
votes
2answers
108 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}}$$ ...

1
2 3 4 5
19