(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to compute maximum product of three numbers of a given array of integers using Heap queue algorithm. Sample Solution: Python Code: def maximumProduct(nums): import heapq a, b = heapq.nlargest(3, nums), heapq.nsmallest(2, nums) return max(a[0] * a[1] * a[2], a[0] * …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to sort a given list of elements in ascending order using Heap queue algorithm. Sample Solution: Python Code: import heapq as hq nums_list = [18, 14, 10, 9, 8, 7, 9, 3, 2, 4, 1] print(“Original list:”) print(nums_list) hq.heapify(nums_list) s_result …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to delete the smallest element from the given Heap and then inserts a new item. Sample Solution: Python Code: import heapq as hq heap = [25, 44, 68, 21, 39, 23, 89] hq.heapify(heap) print(“heap: “, heap) hq.heapreplace(heap, 21) print(“heapreplace(heap, 21): …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python function which accepts an arbitrary list and converts it to a heap using Heap queue algorithm. Sample Solution: Python Code: import heapq as hq raw_heap = [25, 44, 68, 21, 39, 23, 89] print(“Raw Heap: “, raw_heap) hq.heapify(raw_heap) print(“nHeapify(heap): “, raw_heap) …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to implement a heapsort by pushing all values onto a heap and then popping off the smallest values one at a time. Sample Solution: Python Code: import heapq as hq def heapsort(iterable): h = [] for value in iterable: hq.heappush(h, …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to find the three smallest integers from a given list of numbers using Heap queue algorithm. Sample Solution: Python Code: import heapq as hq nums_list = [25, 35, 22, 85, 14, 65, 75, 22, 58] print(“Original list:”) print(nums_list) largest_nums = …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to find the three largest integers from a given list of numbers using Heap queue algorithm. Sample Solution: Python Code: import heapq as hq nums_list = [25, 35, 22, 85, 14, 65, 75, 22, 58] print(“Original list:”) print(nums_list) largest_nums = …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to perform Counter arithmetic and set operations for aggregating results. Sample Solution: Python Code: import collections c1 = collections.Counter([1, 2, 3, 4, 5]) c2 = collections.Counter([4, 5, 6, 7, 8]) print(‘C1:’, c1) print(‘C2:’, c2) print(‘nCombined counts:’) print(c1 + c2) print(‘nSubtraction:’) …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to find the most common element of a given list. Sample Solution: Python Code: from collections import Counter language = [‘PHP’, ‘PHP’, ‘Python’, ‘PHP’, ‘Python’, ‘JS’, ‘Python’, ‘Python’,’PHP’, ‘Python’] print(“Original list:”) print(language) cnt = Counter(language) print(“nMost common element of the …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to count the number of times a specific element presents in a deque object. Sample Solution: Python Code: import collections nums = (2,9,0,8,2,4,0,9,2,4,8,2,0,4,2,3,4,0) nums_dq = collections.deque(nums) print(“Number of 2 in the sequence”) print(nums_dq.count(2)) print(“Number of 4 in the sequence”) print(nums_dq.count(4)) …