AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 129 of 840

Python Program to Construct a Tree & Perform Insertion, Deletion, Display

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 725 Views

When building a tree data structure in Python, we need to construct a tree and perform operations such as inserting an element, deleting an element and displaying elements of the tree. This can be achieved by defining a class with methods for these operations. Below is a demonstration of the same − Tree Structure Implementation class Tree_struct: def __init__(self, data=None, parent=None): self.key = data self.children = [] self.parent ...

Read More

Check whether a number has consecutive 0's in the given base or not using Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 276 Views

When working with different number bases, we sometimes need to check if a number contains consecutive zeros in its representation. Python provides an efficient way to convert numbers between bases and detect patterns like consecutive zeros. Understanding the Problem For example, the number 8 in base 2 (binary) is represented as "1000", which contains consecutive zeros. We need to convert the number to the specified base and then check for consecutive zero digits. Complete Solution def check_consecutive_zero(N, K): my_result = convert_to_base(N, K) if (check_consecutive_zeros(my_result)): ...

Read More

Python Program to Implement a Stack

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 4K+ Views

A stack is a Last-In-First-Out (LIFO) data structure where elements are added and removed from the same end (the top). In Python, we can implement a stack using a class with methods for push, pop, and checking if the stack is empty. Stack Implementation Here's a complete stack implementation with push, pop, and empty check operations ? class Stack: def __init__(self): self.items = [] def is_empty(self): return ...

Read More

Python Program to Find Nth Node in the Inorder Traversal of a Tree

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 300 Views

Finding the nth node in inorder traversal of a binary tree is a common problem in tree algorithms. Inorder traversal visits nodes in the order: left subtree → root → right subtree. This article demonstrates how to find the nth node efficiently using a binary tree class with inorder traversal methods. Understanding Inorder Traversal In inorder traversal, nodes are visited in this sequence: Traverse the left subtree Visit the root node Traverse the right subtree For a binary search tree, inorder traversal gives nodes in sorted order. Binary Tree Implementation Here's a complete ...

Read More

Python Program to Convert a given Singly Linked List to a Circular List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 440 Views

A singly linked list can be converted to a circular linked list by making the last node point to the head node instead of None. This creates a circular structure where traversal can continue indefinitely. Node and LinkedList Structure First, we define the basic node and linked list classes ? class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_struct: def __init__(self): ...

Read More

Python program to find Tuples with positive elements in List of tuples

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 667 Views

When it is required to find tuples that have positive elements from a list of tuples, list comprehension along with the all() function can be used. This approach checks if all elements in each tuple are non-negative. Syntax result = [tuple for tuple in list_of_tuples if all(element >= 0 for element in tuple)] Example The following example demonstrates how to filter tuples containing only positive elements ? my_list = [(56, 43), (-31, 21, 23), (51, -65, 26), (24, 56)] print("The list is:") print(my_list) my_result = [sub for sub in my_list ...

Read More

Python program to find tuples which have all elements divisible by K from a list of tuples

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 973 Views

When it is required to find tuples that have all elements divisible by a specific value 'K', list comprehension along with the all() function can be used. The all() function returns True only when all elements in the tuple satisfy the divisibility condition. Example Let's find tuples where all elements are divisible by K ? my_list = [(45, 90, 135), (71, 92, 26), (2, 67, 98)] print("The list is:") print(my_list) K = 45 print("The value of K has been initialized to") print(K) my_result = [sub for sub in my_list if all(ele % K ...

Read More

Python program to Sort Tuples by their Maximum element

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 371 Views

When it is required to sort tuples based on their maximum element, we can define a function that uses the max() method to find the highest element in each tuple. The sort() method can then use this function as a key to sort the list of tuples. Method 1: Using Custom Function with sort() We can create a helper function to extract the maximum value from each tuple − def get_max_value(my_val): return max(my_val) my_list = [(4, 6, 8, 1), (13, 21, 42, 56), (7, 1, 9, 0), (1, 2)] print("The ...

Read More

Consecutive Nth column Difference in Tuple List using Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 254 Views

When working with lists of tuples, you may need to find the consecutive differences between elements in a specific column. This can be achieved by iterating through the list and using the abs() method to calculate absolute differences. The abs() method returns the absolute (positive) value of a number, while append() adds elements to a list. Example Let's find the consecutive differences in the second column (index 1) of a tuple list ? my_list = [(67, 89, 32), (11, 23, 44), (65, 75, 88)] print("The list is:") print(my_list) print("The value of k has ...

Read More

Remove Tuples from the List having every element as None in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 476 Views

When working with lists of tuples, you may need to remove tuples where every element is None. This is different from removing tuples that contain any None values. Python provides several approaches to filter out tuples containing only None elements. Using List Comprehension with all() The most efficient approach uses list comprehension with the all() function to check if every element in a tuple is None − my_list = [(2, None, 12), (None, None, None), (23, 64), (121, 13), (None, ), (None, 45, 6)] print("The list is :") print(my_list) my_result = [sub for sub ...

Read More
Showing 1281–1290 of 8,392 articles
« Prev 1 127 128 129 130 131 840 Next »
Advertisements