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.
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...
Describe in pseudo-code, a linear-time algorithm for reversing a queue Q. To access the queue, you...
Describe in pseudo-code, a linear-time algorithm for reversing a queue Q. To access the queue, you are only allowed to use the basic functions of the queue ADT defined as follows (Hint: Using a stack, the basic stack functions defined in the textbook and in the class). class Queue { public: int size(); bool isEmpty(); Object front(); void enqueue(Object o); Object dequeue(); };
PYTHON: Write a recursive function named linear_search that searches a list to find a given element....
PYTHON: Write a recursive function named linear_search that searches a list to find a given element. If the element is in the list, the function returns the index of the first instance of the element, otherwise it returns -1000. Sample Output >> linear_search(72, [10, 32, 83, 2, 72, 100, 32]) 4 >> linear_search(32, [10, 32, 83, 2, 72, 100, 32]) 1 >> linear_search(0, [10, 32, 83, 2, 72, 100, 32]) -1000 >> linear_search('a', ['c', 'a', 'l', 'i', 'f', 'o', 'r',...
Write a recursive algorithm replace (start) to replace the value of each element of A with...
Write a recursive algorithm replace (start) to replace the value of each element of A with that of the next element in A. A is a singly linked list.
Implement and complement the recursive code to compute the product of two positive integers, x and...
Implement and complement the recursive code to compute the product of two positive integers, x and y, using only addition and/or subtraction. The function will take as its arguments two integers to multiply together ( x x y ) and will return the product. Hint: consider the following: 6 x 1 = 6 6 x 2 = 6 + (6 x 1) 6 x 3 = 6 + (6 x 2) = 6 + [6 + (6 x 1)] =...
A customer in a grocery store is purchasing three items. Write the pseudo code that will:...
A customer in a grocery store is purchasing three items. Write the pseudo code that will: • Ask the user to enter the name of the first item purchased. Then ask the user to enter the cost of the first item purchased. Make your program user friendly. If the user says the first item purchased is milk, then ask: “What is the cost of milk.” [This should work no matter what item is entered by the user. I might buy...
Could you please write "linkedlist.cpp" using recursive approach to manage a linked list for the given...
Could you please write "linkedlist.cpp" using recursive approach to manage a linked list for the given files below as well as an updated makefile. // 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 << list; find(list, 'y'); list.del('x'); cout <<...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT