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
votes
0answers
15 views
Redundant_map (Recursion) [on hold]
enter link description here """
Complete redundant map, which takes a tree t and a function f, and applies f to the node (2^d) times, where d is the depth of the node. The root has a depth of 0....
0
votes
1answer
38 views
Print inverse right angle using recursion
I wrote a program to print the following pattern using recursion to hands on recursion and my output is perfect but I want to know another and the most optimized approach for this.
This is the sample ...
0
votes
1answer
26 views
Questions on F#'s loop and cont [closed]
The following tail-recursion example is taken verbatim from Chris Smith's Programming F# 3.0. If this is not the appropriate forum for this post, I will gladly move the post to the appropriate place. ...
6
votes
1answer
74 views
Using a bool flag to determine whether in root-level or not
I have several situations in which I need to write a recursive function that does things. I'm asking about one special pattern that I use in recursive methods from time to time.
But first an example, ...
0
votes
1answer
38 views
Compute all possible attendance records with length n, which will be regarded as rewardable
Given a positive integer n, return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may be very large, return it after \$\mod 10^9 + 7\$.
...
4
votes
2answers
113 views
Find all words in a dictionary that can be made with a string of characters (Recursion/Binary Search)
I'm working on an algorithm that could take in a string of 20 random characters, and display to the user every word in a dictionary that can be successfully made with those letters, regardless of ...
4
votes
1answer
62 views
Convert Ternary Expression to a Binary Tree
This is programming question I came across (geeksforgeeks) :
Given a string that contains ternary expressions. The expressions may be nested, task is convert the given ternary expression to a binary ...
0
votes
1answer
44 views
Getting path array for given value in nested Object
I'm wondering how I can improve my getPath function (I found it on SO and adjusted it to my needs - biggest change was returning an array, used to be a string).
...
4
votes
1answer
62 views
Golfed Inverse Ackermann Function [closed]
The following was posted on PPCG:
Your job is to create the slowest growing function you can in no more than 100 bytes.
I did some Googling, and I was interested in the Inverse Ackermann Function:
...
6
votes
2answers
56 views
Recursive function and memorization to find minimum operations to transform n to 1
I'm a new, self-taught programmer working on the Google FooBar challenge. I've submitted my answer (code at bottom) and it was accepted, but I'd like suggestions on how to improve my solution.
...
3
votes
5answers
128 views
Finding large Fibonacci Number in Python
I'm doing Project Euler at 1000-digit Fibonacci number - Problem 25 which says
The Fibonacci sequence is defined by the recurrence relation:
Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
...
-3
votes
0answers
41 views
Recursion function state space reduction
Problem link: SPOJ ACTIV
I have written a recursive solution for this problem and it is working fine for a small n. But it is getting TLE on large which is obvious ...
3
votes
1answer
153 views
Optimizing Recursion in Knight's Tour
I'm trying to write a program for a university project. It should count all possible open tours (i.e. the start and end fields are not the same) recursively on a chessboard depending on board size and ...
6
votes
2answers
90 views
Finding strings in a matrix
Looking for feedback especially regarding design/structure/naming but also anything else that can be improved upon.
Task: Given a (hardcoded) matrix, find out if a given string can be found without ...
4
votes
1answer
67 views
KenKen puzzle solver
I made a program that solves KenKen Puzzles using graphics.py, and I was just wondering if my code was reasonably Pythonic.
neknek.py
...
15
votes
2answers
308 views
A KenKen puzzle/solver in Python
I've written a simple KenKen puzzle/solver in Python. I'd love some feedback on the design of the puzzle, as well as the architecture for the solver.
To model the puzzle, I have the following classes:...
5
votes
1answer
67 views
Bowling game Kata in Scala - pattern match
This is a functional approach to problem described by standard Bowling Game Kata - to implement the rules of calculating a score in a bowling game. The inspiration was the implementation by Uncle Bob: ...
5
votes
2answers
102 views
Stack based recursive arithmetic expression parser
I recently started to learn C++ and came across this problem in a book "Object Oriented Programming in C++" by Robert Lafore.
The author has a program in the book which creates a Parser. I found a ...
5
votes
1answer
65 views
Longest common subsequence length and backtracking the string
Problem: Given two sequences, print the longest subsequence present in both of them. A subsequence is a sequence that appears in the same relative order, but not necessarily contiguous. For example, “...
2
votes
5answers
111 views
Get an array of strings separating a string on N character incrementally
So I asked a question over here, and even though I placed it in the wrong place (it should have been here), Redu was nice enough to give me a nice answer to my question.
Here is the original question ...
1
vote
4answers
199 views
Split a long string using recursive function
I have executed a program which reads long string and divide the string on basis of specified input.
Well I am getting the correct output but would try it to be more efficient and readable.
Opinions ...
1
vote
3answers
83 views
Exercise - count binary-tree nodes with 1 leaf and 1 internal node as children
I'm currently working through "Algorithms in java" by Robert Sedgewick (3rd edition, german version) on my own and am trying to solve one of the exercises there.
The exercise
Write a program that ...
0
votes
1answer
72 views
Permutations of string in Java [closed]
This is a program about finding all the permutations of an string. I want to be able to make it faster and more efficient like eliminating the recursion maybe. Please advise.
What is intended is to ...
4
votes
2answers
102 views
Tower of Hanoi solver in Ruby
Solved the classic Tower of Hanoi problem in Ruby, using recursion. Would love your feedback on this.
...
5
votes
1answer
53 views
Print combinations of balanced parentheses
Solved the below problem. Would love to hear your feedback, especially on performance improvements (granted, the solution should remain recursive).
Especially concerned about the ...
0
votes
0answers
20 views
Follow-up 1: Compare 2 unordered, rooted trees for shape-isomorphism
The previous post can be found here.
In the previous post I tried to solve an exercise but found through the answer of Peilonrayz a bug in the code and 2 more while fixing it. These have now been ...
2
votes
2answers
65 views
Compare 2 unordered, rooted trees for shape-isomorphism
I'm currently working through "Algorithms in java" by Robert Sedgewick (3rd edition, german version) on my own and am trying to solve one of the exercises there.
Edit: This question now also has a ...
3
votes
0answers
121 views
Recursive function to compare xml files using MSXML DOM
I have put together this function which compares two XMLs. On debugging, I found out that there are lot of useless loops which might cause performance problems for me if the files to compare are very ...
3
votes
1answer
70 views
Parsing n lines to count vowels - HackerEarth
I have written a haskell program for the following 'Code Monk' Challenge at HackerEarth.
Here is the challenge description. Basically, we are looking for the number of vowels in a string. The first ...
2
votes
2answers
55 views
Exercise - Break an array into n equal or almost-equal sized parts recursively to find maximum
I'm currently working through "Algorithms in java" by Robert Sedgewick (3rd edition, german version) on my own and am trying to solve one of the exercises there.
The exercise (translation):
Change ...
12
votes
2answers
209 views
N-Queens, bitwise approach
I've recently solved the famous N-Queens problem:
Can you place \$N\$ chess queens on a \$N × N\$ board in such a way that no one
attacks another queen?
The idea here is based on the ideas from ...
4
votes
1answer
77 views
Filtering Gantt chart data involving hierarchical DTOs
I have a bunch of code that deals with modifying a large nested set of IEnumerable<DTO's>
Basically, this is the structure of the ...
2
votes
1answer
94 views
Recursive statement with linq
I have a recursive statement that removesactivities that don't have a resource type for a certain ...
5
votes
1answer
50 views
Given a binary tree, \$T\$ whose nodes have positive values, find the value of the maximal path
Problem
Problem is Maximum sum path from https://firecode.io
Given a binary tree, \$T\$, whose nodes have positive values find the value of a maximal path. A path is a simple path from a node to ...
0
votes
1answer
94 views
Fill 2D array recursively
I'm currently working through "Algorithms in java" by Robert Sedgewick (3rd edition, german version) on my own and I'm trying to solve one of the exercises there.
Problem Statement
The exercise ...
2
votes
2answers
98 views
Asymptotic complexity of multiple binary search
Since binary search is \$O(log(n))\$. Consider this example where I am searching for the existence of e and l and ...
2
votes
1answer
59 views
How many ways we can make the change of the amount using the coins given
Given a set of coins and amount, write an algorithm to find out how
many ways we can make the change of the amount using the coins given.
Example:
Amount = 5
...
6
votes
0answers
60 views
Convert Iterable tree to string
I code this function to convert list of dict to a specific string to export last in a row table string field.
...
1
vote
1answer
52 views
Recursive Preorder Traversal
For today, I tackled this coding challenge question:
Given a binary tree, write a method to recursively traverse the tree in the preorder manner. Mark a node as visited by adding its data to the list -...
6
votes
1answer
73 views
Ackermann function in Python 2.7
I'm relatively new to Python. I just want some constructive feedback on how to improve my code efficiency, style, error handling, etc.
In this case, I've programmed the Ackermann function, but it won'...
4
votes
4answers
255 views
Multiplying 2 numbers without using * operator in Java
I saw this interview question and decided to solve using recursion in Java.
Write a multiply function that multiples 2 integers without using *
...
3
votes
0answers
56 views
2
votes
0answers
114 views
Recursive matrix multiplication in C++
I'm working through CLRS, and I'm hoping to get some pointers (ha!) on my C++ coding style and the way I've implemented recursive matrix multiplication. The code right now seems a bit complex for ...
2
votes
1answer
33 views
trying out possible combinations of varying formula with recursion
I wrote a program in c what looks for integer solutions of different formulas. The program asks the user for a dimension which defines the formula. For example, the formula of the third dimension has ...
21
votes
4answers
3k views
Repeatedly multiplying digits until a single digit is obtained
I've written code that solves a problem from codewars.com. My code passes all the tests on the codewars site. It's a simple enough problem to solve naïvely, but I would love to improve the structure.
...
2
votes
1answer
107 views
Parent-child relationship
I am implementing a parent-child relationship, represented by hashes that look like this:
...
3
votes
1answer
73 views
A recursive function that performs a binary search
I created a recursive function that uses binary search to just return true if it finds the value and false if it does not.
I'm new to recursion and binary search so please let me know where I can ...
0
votes
0answers
57 views
Ternary tree preoder traversal (recursive implementation)
Problem statement:
Print out to console the ternary tree preorder traversal. Suppose that the tree can have at most 3 children, left, middle and right. Preorder traversal is to visit the middle ...
3
votes
2answers
128 views
Print all possible codes for a given string
My question is to print all possible codes for the string.
Assume that value of a=1, b=2, c=3, d=4, .... z=26.
E.g. for “1123” possible codes are aabc, kbc, alc, aaw, kw
Here's what I could come ...
1
vote
0answers
123 views
Hackerrank > Woman's CodeSprint 2 > stone division, Revisited ( recursive function)
Problem statement
You have a pile of n stones that you want to split into multiple piles, as well as a set, S, of ...