Print All Permutations of a String in Lexicographic Order Without Recursion

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

467 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

Form New String from First and Last 2 Characters in Python

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

727 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

Create a Mirror Copy of a Tree and Display Using BFS Traversal in Python

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

671 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

Depth First Search Traversal Using Post Order in Python

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

494 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

Find Largest Value in Tree Using Inorder Traversal

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

201 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

Calculate Number of Words and Characters in a String using Python

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

Remove Characters of Odd Index Values in a String using Python

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

675 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

Depth First Binary Tree Search Using Recursion in Python

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

416 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

Sort Using a Binary Search Tree in Python

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

800 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

444 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

Advertisements