When it is required to find the largest value in a tree using in order traversal, a binary tree class is created with methods to set the root element, perform in order traversal using recursion and so on.An instance of the class is created, and it can be used to access the methods.Below is the demonstration of the same −Example Live Democlass BinaryTree_Struct: def __init__(self, key=None): self.key = key self.left = None self.right = None def set_root(self, key): self.key = key def inorder_traversal_largest(self): ... Read More
When it is required to calculate the number of words and characters present in a string, Below is the demonstration of the sameExample Live Demomy_string = "Hi there, how are you Will ? " print("The string is :") print(my_string) my_chars=0 my_words=1 for i in my_string: my_chars=my_chars+1 if(i==' '): my_words=my_words+1 print("The number of words in the string are :") print(my_words) print("The number of characters in the string are :") print(my_chars)OutputThe string is : Hi there, how are you Will ? The number of words in the string are : 8 The number of characters in the string ... Read More
When it is required to remove characters from odd indices of a string, a method is defined that takes the string as parameter.Below is the demonstration of the same −Example Live Demodef remove_odd_index_characters(my_str): new_string = "" i = 0 while i < len(my_str): if (i % 2 == 1): i+= 1 continue new_string += my_str[i] i+= 1 return new_string if __name__ == '__main__': my_string = "Hi there Will" my_string = remove_odd_index_characters(my_string) print("Characters from odd ... Read More
When it is required to perform depth first search on a tree using recursion, a class is defined, and methods are defined on it that help perform breadth first search.Below is a demonstration for the same −Example Live Democlass BinaryTree_struct: def __init__(self, key=None): self.key = key self.left = None self.right = None def set_root(self, key): self.key = key def insert_at_left(self, new_node): self.left = new_node def insert_at_right(self, new_node): self.right = new_node def search_elem(self, key): ... Read More
When it is required to sort a binary search tree, a class is created, and methods are defined inside it that perform operations like inserting an element, and performing inorder traversal.Below is a demonstration of the same −Exampleclass BinSearchTreeNode: def __init__(self, key): self.key = key self.left = None self.right = None self.parent = None def insert_elem(self, node): if self.key > node.key: if self.left is None: self.left = node node.parent = self else: self.left.insert_elem(node) elif self.key
When it is required to concatenate a matrix vertically, the list comprehension can be used.Below is a demonstration of the same −Example Live Demofrom itertools import zip_longest my_list = [["Hi", "Rob"], ["how", "are"], ["you"]] print("The list is : ") print(my_list) my_result = ["".join(elem) for elem in zip_longest(*my_list, fillvalue ="")] print("The list after concatenating the column is : ") print(my_result)OutputThe list is : [['Hi', 'Rob'], ['how', 'are'], ['you']] The list after concatenating the column is : ['Hihowyou', 'Robare']ExplanationThe required packages are imported.A list of list is defined, and is displayed on the console.The list comprehension is used to ... Read More
When it is required to get the ‘n’th column of a matrix, the ‘any’ method can be used.Below is a demonstration of the same −Example Live Demomy_list = [[34, 67, 89], [16, 27, 86], [48, 30, 0]] print("The list is : ") print(my_list) N = 1 print("The value of N has been initialized to -") print(N) elem = 30 my_result = any(sub[N] == elem for sub in my_list) print("Does the element exist in a particular column ? ") print(my_result)OutputThe list is : [[34, 67, 89], [16, 27, 86], [48, 30, 0]] The value of N has been ... Read More
When it is required to create a matrix of dimension n * n, a list comprehension is used.Below is a demonstration of the same −Example Live DemoN = 4 print("The value of N is ") print(N) my_result = [list(range(1 + N * i, 1 + N * (i + 1))) for i in range(N)] print("The matrix of dimension N * 0 is :") print(my_result)OutputThe value of N is 4 The matrix of dimension N * 0 is : [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]ExplanationThe value of N is ... Read More
When it is required to read the height in ‘cm’ and convert it into ‘feet’ and ‘inches’, the ‘round’ method can be used.Below is a demonstration of the same −Example Live Demoin_cm=int(input("Enter the height in centimeters...")) in_inches=0.394*in_cm in_feet=0.0328*in_cm print("The length in inches is ") print(round(in_inches, 2)) print("The length in feet is") print(round(in_feet, 2))OutputEnter the height in centimeters...178 The length in inches is 70.13 The length in feet is 5.84ExplanationThe input is taken by user, as ‘cm’.It can be converted into inches by multiplying it with 0.394.This is assigned to a variable.It can be converted into feet by multiplying it with 0.0328.This ... Read More
When it is required to compute a simple interest when the amount, rate and interest are given, a simple formula can be defined, and the elements can be plugged into the formula.Below is a demonstration of the same −Example Live Demoprinciple_amt = float(input("Enter the principle amount...")) my_time = int(input("Enter the time in years...")) my_rate = float(input("Enter the rate...")) my_simple_interest=(principle_amt*my_time*my_rate)/100 print("The computed simple interest is :") print(my_simple_interest)OutputEnter the principle amount...45000 Enter the time in years...3 Enter the rate...6 The computed simple interest is : 8100.0ExplanationThe principal amount, the rate of interest, and the time are taken as user inputs.Another formula is defined ... Read More