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.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
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.
Given a BST and a sum, write pseudo code to determine if the tree has a...
Given a BST and a sum, write pseudo code to determine if the tree has a root- to-leaf path such that adding up all the values along the path equals the given sum. Given the below BST and sum = 49, the array is (8, 4, 10, 1, 0, 3, 9, 15, 16). Return true, as there exist a root-to-leaf path 8− > 10− > 15− > 16 which sum is 49.
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of comparable objects L, and a new item x, find the location (index) of x or indicated that x is not in L. 5a) Give a recursive algorithm for binary search. No, while, for, repeat etc statements are allowed. Only if, if else and assignment statements are allowed. 5b) Write a difference equation for the running time of your Binary Search algorithm. Solve your equation...
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, …
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT