Question

In: Computer Science

Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...

Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members:

int List.length returns the length of the list.
void List.push(T n) pushes an element n to the front of the list
T List.pop() pops an element from the front of the list.
List$$ List$$.concat(List$$ other) returns the concatenation of this list with other.

Explain in plain English the reasoning behind your algorithm.

Power Lists should be able to print ([],[1],[2], [3], [1,2], [1,3], [2,1], [2,3], [3,1],[3,2])
NOTE: [1,2] DOES NOT EQUAL [2,1]

Solutions

Expert Solution

The power list basically means we need to take all different combinations of numbers in the list and also consider all its permutations. So we will divide the task into two parts, getting all different combinations of numbers from the list of integers and then taking all the different permutation of that combination.

//the basic idea of generating all the combinations is to initially take an empty list at first
//and then pushing the numbers one in a recursion and calling for the next recursion using backtracking

list curr;   //for storing the current list of interger
list number; //given list of numbers

//curr is initially empty

integer index = -1;

//we will declare a recursive function here which will be called for generating all the
//combinations

powerList(index) 
    if index == number.lenght //if the index value is equal to length of the given list
        return;       //close the function here. you have permuted every number in the list
   
    //permute all the possible arrangements for the curr list 
    permute(curr,0,curr.lenght)
   
    //now we will append the ith number from the given list and call the recursion again
    for i = index+1, i< number.lenght ,i++
        curr.push(number[i])
        powerList(i)
        curr.pop()
   
    return;

//we have used the permute function so we will have to define that too
//this function is used to generate all the permutation of the combinations generated
//The logic of this permute function is to print everything if the first and the last index are same
// if they are not same swap the first number with the ith number and again call permutation on the next index
permutation(list curr, integer s, integer e) 
    if s == e
    print curr
       return;

    for i = s i <= e i++ 
        swap(curr[s], curr[i])
        permutation(curr, s + 1, e)
        swap(curr[s], curr[i])
    
     return;

Related Solutions

Give a recursive algorithm to compute a list of all permutations of a given set S....
Give a recursive algorithm to compute a list of all permutations of a given set S. (That is, compute a list of all possible orderings of the elements of S. For example, permutations({1, 2, 3}) should return {〈1, 2, 3〉, 〈1, 3, 2〉, 〈2, 1, 3〉, 〈2, 3, 1〉, 〈3, 1, 2〉, 〈3, 2, 1〉}, in some order.) Prove your algorithm correct by induction.
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java program.
In pseudo-code, design an algorithm for finding baking a cake.
In pseudo-code, design an algorithm for finding baking a cake.
Use Recursive Algorithm to compute 5^23 Mod 8
Use Recursive Algorithm to compute 5^23 Mod 8
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix....
write code to manage a linked list using recursive approach. (Using this code) C++ IN Unix. // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; void find(LinkedList& list, char ch) {    if (list.find(ch))        cout << "found ";    else        cout << "did not find ";    cout << ch << endl; } int main() {    LinkedList   list;    list.add('x');    list.add('y');    list.add('z');    cout << list;    find(list, 'y');    list.del('y');    cout...
Design in pseudo code a multiple recursive version of Tetranacci calculators. Tetranacci numbers are a more...
Design in pseudo code a multiple recursive version of Tetranacci calculators. Tetranacci numbers are a more general version of Fibonacci numbers and start with four predetermined terms, each term afterwards being the sum of the preceding four terms. The first few Tetranacci numbers are: 0, 0, 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, …
Design in pseudo code a linear recursive version of Tetranacci calculators. Tetranacci numbers are a more...
Design in pseudo code a linear recursive version of Tetranacci calculators. Tetranacci numbers are a more general version of Fibonacci numbers and start with four predetermined terms, each term afterwards being the sum of the preceding four terms. The first few Tetranacci numbers are: 0, 0, 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, …
Parse string java code Write a recursive program that can calculate the value of a given...
Parse string java code Write a recursive program that can calculate the value of a given polynomial in a string, which is not more than the tenth order, for the given x. The polynomial will be given in the following format and should display the value of the polynomial for spaced-out x Using index of for -/+
Given the list values = [], write code that fills the list with each set of...
Given the list values = [], write code that fills the list with each set of numbers below. Note: use loops if it is necessary 1 2 3 4 5 6 7 8 9 10 0 2 4 6 8 10 12 14 16 18 20 1 4 9 16 25 36 49 64 81 100 0 0 0 0 0 0 0 0 0 0 1 4 9 16 9 7 4 9 11 0 1 0 1 0...
2. Give a recursive algorithm to compute the product of two positive integers, m and n,...
2. Give a recursive algorithm to compute the product of two positive integers, m and n, using only addition and subtraction. Java Language...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT