Programming Articles

Page 391 of 2547

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

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 267 Views

Finding the largest value in a binary tree using inorder traversal is a common tree operation. This approach visits nodes in left-root-right order while tracking the maximum value encountered during traversal. Below is a complete implementation using a binary tree class with methods for tree construction and largest value finding ? Complete Implementation class BinaryTree: def __init__(self, key=None): self.key = key self.left = None self.right = None ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 742 Views

When working with strings in Python, you might need to remove characters at odd index positions (1, 3, 5, etc.). This can be accomplished using various approaches including loops, string slicing, and list comprehensions. Understanding String Indexing In Python, string indexing starts from 0. So for a string "Hello": Index 0: 'H' (even) Index 1: 'e' (odd) Index 2: 'l' (even) Index 3: 'l' (odd) Index 4: 'o' (even) Method 1: Using While Loop This approach iterates through the string and skips characters at odd indices ? def remove_odd_index_characters(my_str): ...

Read More

Python Program for Depth First Binary Tree Search using Recursion

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 481 Views

Depth First Search (DFS) is a tree traversal algorithm that explores nodes by going as deep as possible before backtracking. In binary trees, DFS can be implemented recursively by visiting the current node, then traversing the left subtree, and finally the right subtree. Binary Tree Class Structure First, let's define a binary tree class with methods for insertion and DFS traversal ? class BinaryTree_struct: def __init__(self, key=None): self.key = key self.left = None ...

Read More

Python Program to Sort using a Binary Search Tree

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 866 Views

A Binary Search Tree (BST) is a tree data structure where the left subtree contains values less than the root, and the right subtree contains values greater than or equal to the root. The inorder traversal of a BST gives elements in sorted order, making it useful for sorting. How BST Sorting Works BST sorting involves two main steps: Insertion: Add elements to the BST maintaining the BST property Inorder Traversal: Visit nodes in left-root-right order to get sorted sequence Implementation We'll create two classes: BinSearchTreeNode for individual nodes and BinSearchTree for the ...

Read More

Vertical Concatenation in Matrix in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 497 Views

Vertical concatenation in Python matrix operations involves combining elements from corresponding positions across different rows. This creates new strings by joining elements column-wise rather than row-wise. Using zip_longest() for Vertical Concatenation The zip_longest() function from the itertools module handles matrices with unequal row lengths by filling missing values ? from itertools import zip_longest matrix = [["Hi", "Rob"], ["how", "are"], ["you"]] print("The matrix is:") print(matrix) result = ["".join(elem) for elem in zip_longest(*matrix, fillvalue="")] print("The matrix after vertical concatenation:") print(result) The matrix is: [['Hi', 'Rob'], ['how', 'are'], ['you']] The matrix after ...

Read More

Get Nth Column of Matrix in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 476 Views

When working with matrices in Python, you often need to extract a specific column. Python provides several methods to get the Nth column of a matrix, including list comprehension, the zip() function, and NumPy arrays. Using List Comprehension The most straightforward approach is using list comprehension to extract elements at a specific index ? matrix = [[34, 67, 89], [16, 27, 86], [48, 30, 0]] print("The matrix is:") print(matrix) N = 1 print(f"Getting column {N}:") # Extract Nth column using list comprehension nth_column = [row[N] for row in matrix] print(nth_column) ...

Read More

Matrix creation of n*n in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 733 Views

When it is required to create a matrix of dimension n * n, a list comprehension is used. This technique allows us to generate a square matrix with sequential numbers efficiently. Below is a demonstration of the same − Example N = 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 * N is:") print(my_result) Output ...

Read More

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

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

Converting height from centimeters to feet and inches is a common unit conversion task. Python provides built-in functions like round() to handle decimal precision in calculations. Basic Conversion Method Here's a simple approach to convert centimeters to both feet and inches separately ? height_cm = int(input("Enter the height in centimeters: ")) # Convert to inches and feet height_inches = 0.394 * height_cm height_feet = 0.0328 * height_cm print("The height in inches is:", round(height_inches, 2)) print("The height in feet is:", round(height_feet, 2)) Enter the height in centimeters: 178 The height in inches ...

Read More

Python Program to Compute Simple Interest Given all the Required Values

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 397 Views

When it is required to compute simple interest when the principal amount, rate, and time are given, we can use the standard formula: Simple Interest = (Principal × Time × Rate) / 100. Below is a demonstration of the same − Simple Interest Formula The mathematical formula for calculating simple interest is: Simple Interest = (Principal × Time × Rate) / 100 Where: Principal − The initial amount of money Time − Duration in years Rate − Annual interest rate (percentage) ...

Read More

Python Program to Check if a Date is Valid and Print the Incremented Date if it is

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 650 Views

When it is required to check if a date is valid or not, and print the incremented date if it is a valid date, the 'if' condition is used along with date validation logic. Below is a demonstration of the same − Example my_date = input("Enter a date : ") dd, mm, yy = my_date.split('/') dd = int(dd) mm = int(mm) yy = int(yy) if(mm == 1 or mm == 3 or mm == 5 or mm == 7 or mm == 8 or mm == 10 or mm == 12): ...

Read More
Showing 3901–3910 of 25,466 articles
« Prev 1 389 390 391 392 393 2547 Next »
Advertisements