python code
The power set of a list L is a list containing all possible combinations of the elements of L. For example, the power set of [1, 2, 3] is [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]. Section 9.3 of your textbook describes a (subtle) algorithm for generating the power set.
• Read and understand the structure of the code in Figure 9.6
• Write (your own) function powerset(L) that generates and returns the power set of a list;
• What is the complexity of your algorithm?
def powerset(L): # set_size of power set of a set pow_set_size = (int)(math.pow(2, len(L))) counter = 0; j = 0; print("[]") for counter in range(1, pow_set_size): print("[",end=" ") for j in range(0, len(L)): # Check if jth bit in the # counter is set If set then # print jth element from set if ((counter & (1 << j)) > 0): print(L[j], end=" ") print("]") # Driver program to test printPowerSet L = [1, 2, 3] powerset(L)
Complexity is O(n2).
/* PLEASE UPVOTE THANK YOU IN ADVANCE. IF YOU SATISFY WITH THE ANSWER IF ANY QUERY ASK ME IN COMMENT SECTION */
Python code The power set of a list L is a list containing all possible combinations of the eleme...
Write a Python function powerSet() that takes in a finite list object AA and returns P(A)P(A), the power set of A. The output should be only in the form of list objects. Note: Make sure you are familiar with some of Python's basic built-in list functions and operators, as they will make completing this problem far easier. For example: Test Result A = [0, 1, 2] output = set([frozenset(a) for a in powerSet(A)]) expected = [[], [0], [0, 1], [2],...
(Python)Implement the function deep_list, which takes in a list, and returns a new list which contains only elements of the original list that are also lists. Use a list comprehension. def deep_list(seq): "" "Returns a new list containing elements of the original list that are lists. >>> seq = [49, 8, 2, 1, 102] >>> deep_list(seq) [] >>> seq = [[500], [30, 25, 24], 8, [0]] >>> deep_list(seq) [[500], [30, 25, 24], [0]] >>> seq = ["hello", [12, [25], 24],...
In python Programming Exercise 1 Write a function that will have a list as an input, the task of the function is to check if all the elements in the list are unique,( i.e. no repetition of any value has occurred in the list), then the function returns true otherwise it returns false. Your program should include a main method that call the method to test it. Algorithm Analysis For the function you implemented in part 1, please calculate T(n),...
Python recursive function: Given an ordered list L. A permutation of L is a rearrangement of its elements in some order. For example (1,3, 2) and (3, 2, 1) are two different permutations of L=(1,2,3). Implement the following function: def permutations (lst, low, high) The function is given a list 1st of integers, and two indices: low and high (lows high), which indicate the range of indices that need to be considered The function should return a list containing all...
Q2. Answer the following questions assignment, you will develop a well-documented pseudocode that generates all possible subsets of a given set T (i.e. power set of T) containing n elements with the following requirements: your solution must be non-recursive, and must use a stack and a queue to solve the problem. For example: ifT- {2, 4, 7,9; then your algorithm would generate: U, t2), (4;, {7), {9;, 12,4), 12,7;, 12.9;, 14,7), 14,9), 17,9;, ...etc. (Note: your algorithm's output need not...
Write a python function that returns a list or array of tuples containing every permutation of numbers 1 to n. For example: permutate(1): [(1,)] permutate(2): [(1, 2), (2, 1)] permutate(3): [(1, 2, 3), (1, 3, 2), (3, 1, 2), (3, 2, 1), (2, 3, 1), (2, 1, 3)] #here is some starting code def permutate(n): result = [] return result
7. (20 points) Assume L is a list, and assume that int n=L length() returns the number of elements in the list, and Bubblsort(L, 0,i) sorts the list from 0 to i usin the g the Bubble sort algorithm. Determine asymtotic running time as function of n, e(T(n), for the average case time for the following code fragments a) for( int i = 1;i < n; i* 2) Bubblsort (L,0,i); for( int i=0;i 7. (20 points) Assume L is a...
python Make another code block that creates a list using range( ) with 8 values starting with 1. print out your list Create a set from each of the three lists. Find the elements in common between the three sets.
Python code that: Accepts a list of integers and an integer, and returns the index of the SECOND occurrence of the integer in the list. It returns None if the number does not occur two or more times in the list. Example 1: second_index([2,34,3,45,34,45,3,3], 3) returns 6 Example 2: second_index([2,34,3,45,34,45,3,3], 45) returns 5 Example 3: second_index([2,34,3,45,134,45,3,3], 134) returns None Example 4: second_index([2,34,3,45,134,45,3,3], 100) returns None
python code,please! Task 3:N ns Brute For In mathematics, the notion of permutation relates to the act of arranging all the members of a set into some sequence or order, or if the set is already ordered, rearranging (reordering) its elements, a process called permuting. The number of permutations on a set of n elements is given by n! (Read as n factorial). For example, there are 2!2 x 1- 2 permutations of 11,2), 2,1) and 3!-3x2x16 permutations of (1,2,3),...