[Solved] How can I convert my for loop into a recursive algorithm?

You do it by actually using the i parameter. private static final String ALPHABET = “abcdefghijklmnopqrstuvwxyz”; public static void binary(int n, String str, int i) { if (i == ALPHABET.length()) return; if (n == 0) { System.out.println(str); return; } binary(n – 1, str + ALPHABET.charAt(i), 0); // next letter starts at beginning of alphabet binary(n, … Read more

[Solved] A program which is difficult

Essentially, your question is this: given a graph with nodes represented as indices and edges as index pairs, and given an index i that represents a node, find all nodes which are connected to the given node. UnionFind algorithm to find connected components over nodes with indices: Initialize an array father of size number of … Read more

[Solved] C# Dynamic for-loop

It can be done without recursion. var s = “A,B,C|1,2|a|u,v,w”; var u = s.Split(‘|’).Select(v => v.Split(‘,’)).ToList(); var buffer = new List<string>(); buffer.Add(“COMMAND “); while (u.Count > 0) { var t = from a in buffer from b in u.First() select a + ‘ ‘ + b; buffer = t.ToList(); u.RemoveAt(0); } The buffer list will … Read more

[Solved] Python recursion facts

1) A recursive solution has a few benefits. Generally it is more readable, and smaller in terms of lines of code. When it doesn’t work, it’s almost always harder to debug. It’s usually preferable when it runs faster than a non-recursive solution (see merge sort). 2) By definition. 3) True — the point of recursion … Read more

[Solved] Operations on Strings

If two strings are equal to each other. When comparing two strings, you have to pass in the strings. public static boolean isEquals(String a, String b) { if (a.length() != b.length()) return false; return isEquals(a, b, 0); } private static boolean isEquals(String a, String b, int index) { if (index >= a.length()) return true; if … Read more

[Solved] does cout before return statement of recursive function stops it?

The body of an if-statement can be either a compound statement, which is a list of statements surrounded by {}, or it is the single statement following the if‘s condition. That means that this code: if(n>=2) cout<<“number of times the function called: “<<endl; return n*factorial(n-1); is completely equivalent to: if(n>=2){ cout<<“number of times the function … Read more

[Solved] bits set by lookup table – Recursive macro [duplicate]

The idea is “recursively defining the problem down to 2-bit values”: 00, 01, 10, 11. They’re not recursive macros, but does represent the technique of recursive decomposition of the problem. The arrangement of macros as a cascade attacks the problem of generating the table of 2^8 values by solving the problem for 2-bits (generating 4 … Read more

[Solved] Algorithm (especially for c++) to show Every Permutation

You want all permutations of each member of the powerset of the input. permSub(“abc”, “”) func permSub(input, perm) print perm if input = “” return for i = 0 to input.length-1 permSub(input[0..i]+input[i+1..input.length), perm+input[i] end end Where input[i..j] represents the sub string of input from i(inclusive) to j(exclusive), and + is string concatenation. Note that this … Read more

[Solved] How do I write a recursive function in Javascript to add up all of the string values of a deeply nested object?

Here’s a very simple implementation that should work for simple objects like this: var walkProps = function(obj) { var s = “”; for(var x in obj) { if(typeof obj[x] === “string”) s += obj[x]; else s += walkProps(obj[x]); } return s; } Demonstration Note, though, that that depends on the order in which for-in visits … Read more