(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to get the factorial of a non-negative integer. Sample Solution: Python Code: def factorial(n): if n <= 1: return 1 else: return n * (factorial(n – 1)) print(factorial(5)) Sample Output: 120 Write a Python program to get the factorial …
Month: March 2021
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program of recursion list sum. Sample Solution: Python Code: def recursive_list_sum(data_list): total = 0 for element in data_list: if type(element) == type([]): total = total + recursive_list_sum(element) else: total = total + element return total print( recursive_list_sum([1, 2, [3,4],[5,6]])) Sample Output: …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to calculate the sum of a list of numbers. Sample Solution: Python Code: def list_sum(num_List): if len(num_List) == 1: return num_List[0] else: return num_List[0] + list_sum(num_List[1:]) print(list_sum([2, 4, 5, 6, 7])) Sample Output: 24 Write a Python program to …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to find the kth smallest element in a given a binary search tree. Sample Solution: Python Code: class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None def kth_smallest(root, k): stack = [] while root or stack: …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to convert a given array elements to a height balanced Binary Search Tree (BST). Note: Search for a node to remove. If the node is found, delete the node. Sample Solution: Python Code: class TreeNode(object): def __init__(self, x): self.val = …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to delete a node with the given key in a given Binary search tree (BST). Note: Search for a node to remove. If the node is found, delete the node. Sample Solution: Python Code: class TreeNode(object): def __init__(self, x): self.val …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to create a Balanced Binary Search Tree (BST) using an array (given) elements where array elements are sorted in ascending order. Sample Solution: Python Code: class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None def …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to search a specific item in a singly linked list and return true if the item is found otherwise return false. Sample Solution: Python Code: class Node: def __init__(self, data=None): self.data = data self.next = None class singly_linked_list: def __init__(self): …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to find the size of a singly linked list. Sample Solution: Python Code: class Node: def __init__(self, data=None): self.data = data self.next = None class singly_linked_list: def __init__(self): self.tail = None self.head = None self.count = 0 def append_item(self, data): …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to create a singly linked list, append some items and iterate through the list. Sample Solution: Python Code: class Node: def __init__(self, data=None): self.data = data self.next = None class singly_linked_list: def __init__(self): self.tail = None self.head = None self.count …