Python Articles

Page 32 of 852

Python program to convert a given binary tree to doubly linked list

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 251 Views

When it is required to convert a given binary tree to a doubly linked list, a ‘Node’ class needs to be created. In this class, there are two attributes, the data that is present in the node, and the access to the next node of the linked list.Another ‘linked_list’ class needs to be created that would have an initialization function, and the head of the node would be initialized to ‘None’.In a doubly linked list, the nodes have pointers. The current node would have a pointer to the next node as well as the previous node. The last value in ...

Read More

Python program to create and display a doubly linked list

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 861 Views

When it is required to create and display the elements in a doubly linked list, a ‘Node’ class needs to be created. In this class, there are three attributes, the data that is present in the node, the access to the next node of the linked list, and the access to the previous node of the linked list.Below is a demonstration for the same −Exampleclass Node:    def __init__(self, my_data):       self.prev = None       self.data = my_data       self.next = None class double_list:    def __init__(self):       self.head = None   ...

Read More

Python program to create a doubly linked list of n nodes and count the number of nodes

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 198 Views

When it is required to count the number of nodes in a doubly linked list, a ‘Node’ class needs to be created. In this class, there are three attributes, the data that is present in the node, the access to the next node of the linked list, and the access to the previous node of the linked list.In a doubly linked list, the nodes have pointers. The current node would have a pointer to the next node as well as the previous node. The last value in the list will have ‘NULL’ value in the next pointer. It can be ...

Read More

Python Program to Print an Identity Matrix

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 871 Views

When it is required to print an identity matrix, nested loops can be used.Below is a demonstration for the same −Examplen = 4 print("The value of n has been initialized to " +str(n)) for i in range(0, n):    for j in range(0, n):       if(i==j):          print("1", sep=" ", end=" ")       else:          print("0", sep=" ", end=" ")    print()OutputThe value of n has been initialized to 4 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1ExplanationThe value of ‘n’ is ...

Read More

Python program to create a doubly linked list of n nodes and display it in reverse order

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 291 Views

When it is required to create a doubly linked list and display the elements in a reversed order, a ‘Node’ class needs to be created. In this class, there are three attributes, the data that is present in the node, the access to the next node of the linked list, and the access to the previous node of the linked list.Another class needs to be created that would have an initialization function, and the head of the node would be initialized to ‘None’ inside this.Multiple methods are defined by the user to add node to the linked list, to reverse ...

Read More

Python program to sort a list of tuples by second Item

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 686 Views

When it is required to sort a list of tuples based on the second item, the lambda function and ‘sorted’ method can be used.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). A list of tuple basically contains tuples enclosed in a list.Anonymous function is a function which is defined without a name.In general, functions in Python are defined using ‘def’ keyword, but anonymous function is defined with the help of ‘lambda’ keyword. It takes a single expression, but can take any number of arguments. It ...

Read More

Python program to delete a new node from the beginning of the doubly linked list

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 383 Views

When it is required to delete a node from the beginning of a doubly linked list, a ‘Node’ class needs to be created. In this class, there are three attributes, the data that is present in the node, the access to the next node of the linked list, and the access to the previous node of the linked list.Below is a demonstration for the same −Exampleclass Node:    def __init__(self, my_data):       self.prev = None       self.data = my_data       self.next = None class double_list:    def __init__(self):       self.head = None ...

Read More

Python - Assign a specific value to Non Max-Min elements in Tuple

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 157 Views

When it is required to assign a specific value to the non max-min elements in a tuple, the ‘max’ method, the ‘min’ method, the ‘tuple’ method and a loop can be used.The ‘max’ method returns the maximum value among all of the elements in an iterable. The ‘min’ method returns the minimum value among all of the elements in an iterable.The ‘tuple’ method converts a given value/iterable into a tuple type.Below is a demonstration for the same −Examplemy_tuple = (25, 56, 78, 91, 23, 11, 0, 99, 32, 10) print("The tuple is : ") print(my_tuple) K = 5 print("K has ...

Read More

Python program to delete a new node from the end of the doubly linked list

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 562 Views

When it is required to delete a node from the end of a doubly linked list, a ‘Node’ class needs to be created. In this class, there are three attributes, the data that is present in the node, the access to the next node of the linked list, and the access to the previous node of the linked list.Below is a demonstration for the same −Exampleclass Node:    def __init__(self, my_data):       self.prev = None       self.data = my_data       self.next = None class double_list:    def __init__(self):       self.head = None ...

Read More

Python Program to Find Longest Common Substring using Dynamic Programming with Bottom-Up Approach

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 349 Views

When it is required to find longest common substring using dynamic programming with bottom-up approach, a method can be defined, that computes the solution to smaller problems. These smaller problem results don’t need to be computed again and again. Instead, they can just be accessed when required. This would lead to developing a solution to the bigger problem in hand.Below is a demonstration for the same −Exampledef compute_lcw(string_1, string_2):    val = [[-1]*(len(string_2) + 1) for _ in range(len(string_1) + 1)]    for i in range(len(string_1) + 1):       val[i][len(string_2)] = 0    for j in range(len(string_2)):   ...

Read More
Showing 311–320 of 8,519 articles
« Prev 1 30 31 32 33 34 852 Next »
Advertisements