Found 10476 Articles for Python

Python Program to Print only Nodes in Left SubTree

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

277 Views

When it is required to print the nodes in the left subtree, a class can be created that consists of methods can be defined to set the root node, perform in order traversal, insert elements to the right of the root node, to the left of the root node, and so on. An instance of the class is created, and the methods can be used to perform the required operations.Below is a demonstration of the same −Example Live Democlass BinaryTree_struct:    def __init__(self, data=None):       self.key = data       self.left = None       self.right = ... Read More

Python Program to Print All Permutations of a String in Lexicographic Order without Recursion

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

459 Views

When it is required to print all the permutations of a string in the lexicographic order without using recursion, a method is defined, that takes the string as the parameter. It uses a simple ‘for’ loop to iterate over the string elements and uses ‘while’ condition to check for certain constraints.Below is the demonstration of the same −Example Live Demofrom math import factorial def lex_permutation(my_string): for i in range(factorial(len(my_string))):    print(''.join(my_string))    i = len(my_string) - 1    while i > 0 and my_string[i-1] > my_string[i]:       i -= 1    my_string[i:] = reversed(my_string[i:])    if i > 0: ... Read More

Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String

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

722 Views

When it is required to form a new string that is made from the first two and last two characters of a given string, a counter can be defined, and indexing can be used to access specific range of elements.Below is the demonstration of the same −Example Live Demomy_string = "Hi there how are you" my_counter = 0 for i in my_string:    my_counter = my_counter + 1 new_string = my_string[0:2] + my_string [my_counter - 2: my_counter ] print("The string is ") print(my_string) print("The new string is ") print(new_string)OutputThe string is Hi there how are you The new ... Read More

Python Program to Create a Mirror Copy of a Tree and Display using BFS Traversal

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

664 Views

When it is required to create a mirror copy of a tree, and display it using breadth first search, a binary tree class is created with methods set the root element, insert element to left, insert element to right, 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 BinaryTree_struct:    def __init__(self, key=None):       self.key = key       self.left = None       self.right = None ... Read More

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

Advertisements