Found 33676 Articles for Programming

Python Program to Implement Depth First Search Traversal using Post Order

AmitDiwan
Updated on 16-Apr-2021 12:31:03

488 Views

When it is required to implement depth first search using post order traversal, a tree class is created with methods to add element, search for a specific element, and perform post order traversal 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 Tree_Struct:    def __init__(self, key=None):       self.key = key       self.children = []    def add_elem(self, node):       self.children.append(node)      def search_elem(self, key):       if self.key == key:     ... Read More

Python Program to Find the Largest value in a Tree using Inorder Traversal

AmitDiwan
Updated on 16-Apr-2021 12:28:03

196 Views

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

Python Program to Calculate the Number of Words and the Number of Characters Present in a String

AmitDiwan
Updated on 16-Apr-2021 12:24:38

1K+ Views

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

Python Program to Remove the Characters of Odd Index Values in a String

AmitDiwan
Updated on 16-Apr-2021 12:22:23

669 Views

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

Python Program for Depth First Binary Tree Search using Recursion

AmitDiwan
Updated on 16-Apr-2021 12:21:22

408 Views

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

Python Program to Sort using a Binary Search Tree

AmitDiwan
Updated on 16-Apr-2021 12:19:41

788 Views

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

Vertical Concatenation in Matrix in Python

AmitDiwan
Updated on 16-Apr-2021 12:15:42

433 Views

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

Get Nth Column of Matrix in Python

AmitDiwan
Updated on 16-Apr-2021 12:14:57

414 Views

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

Matrix creation of n*n in Python

AmitDiwan
Updated on 16-Apr-2021 12:12:23

666 Views

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

Python Program to Read Height in Centimeters and convert the Height to Feet and Inches

AmitDiwan
Updated on 16-Apr-2021 12:11:44

3K+ Views

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

Advertisements