Python Articles

Page 631 of 855

Program to find the nth row of Pascal's Triangle in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Pascal's Triangle is a triangular array of numbers where each row represents the binomial coefficients. We need to find the nth row (0-indexed) of this mathematical structure. Understanding Pascal's Triangle Pascal's Triangle follows these rules: The top row contains only [1] Each subsequent row starts and ends with 1 Each interior number is the sum of the two numbers above it 1 1 1 1 2 1 1 3 3 1 ...

Read More

Program to count number of valid pairs from a list of numbers, where pair sum is odd in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 414 Views

Suppose we have a list of positive numbers, we have to find the number of valid pairs of indices (i, j), where i < j, and nums[i] + nums[j] is an odd number. An important mathematical property: the sum of two numbers is odd only when one number is even and the other is odd. This means we need to count pairs where one element is even and another is odd. So, if the input is like [5, 4, 6], then the output will be 2, as two pairs are [5, 4] and [5, 6], whose sums are ...

Read More

Program to check old and new version numbering are correct or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 232 Views

Version comparison is a common task in software development. We need to compare two version strings in the format "major.minor.patch" to determine if the newer version is actually newer than the older one. So, if the input is like older = "7.2.2", newer = "7.3.1", then the output will be True because 7.3.1 is newer than 7.2.2. Algorithm To solve this problem, we will follow these steps − Split both version strings into lists of major, minor, patch numbers Compare each component (major, minor, patch) from left to right If newer component is greater, return ...

Read More

Program to count number of elements in a list that contains odd number of digits in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 608 Views

Given a list of positive numbers, we need to count how many elements have an odd number of digits. For example, the number 123 has 3 digits (odd), while 1234 has 4 digits (even). So, if the input is like [1, 300, 12, 10, 3, 51236, 1245], then the output will be 4 because numbers 1, 3, 300, and 1245 have odd digit counts (1, 1, 3, and 4 digits respectively). Algorithm To solve this, we will follow these steps: Initialize counter c = 0 For each ...

Read More

Program to find the sum of first n odd numbers in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 10K+ Views

In Python, finding the sum of the first n positive odd numbers is a common mathematical problem. The odd numbers sequence starts from 1, 3, 5, 7, 9, and so on. We can solve this using multiple approaches. So, if the input is like 7, then the output will be 49 as [1+3+5+7+9+11+13] = 49 Method 1: Using While Loop This approach iterates through the first n odd numbers and calculates their sum ? def sum_first_n_odds(n): if n == 0: return 0 ...

Read More

Program to find total number of strings, that contains one unique characters in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 261 Views

When working with strings, we often need to count substrings that contain only one unique character. This means finding all contiguous sequences where all characters are the same. So, if the input is like "xxyy", then the output will be 6 as the valid substrings are: ["x", "x", "xx", "y", "y", "yy"]. Algorithm To solve this problem, we follow these steps ? Initialize total count to 0 Track the previous character to detect character changes For each character in the string: If character differs from previous, start a new sequence Otherwise, extend the current ...

Read More

Program to find k-length paths on a binary tree in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 360 Views

Finding k-length paths in a binary tree is a classic dynamic programming problem on trees. We need to count all unique paths of exactly k nodes, where paths can traverse both upward (child to parent) and downward (parent to child). Problem Understanding Given a binary tree with unique values and a number k, we count paths of length k. Two paths are different if they contain different nodes or visit nodes in different order. 12 8 ...

Read More

Program to find number of ways to arrange n rooks so that they cannot attack each other in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 463 Views

Suppose we have a number n representing a chessboard of size n x n. We have to find the number of ways we can place n rooks, such that they cannot attack each other. Here two ways will be considered different if in one of the ways, some cell of the chessboard is occupied, and another way, the cell is not occupied. (We know that rooks can attack each other if they are either on the same row or on the same column). So, if the input is like 3, then the output will be 6. ...

Read More

Program to find number of nodes in a range in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 994 Views

Given a Binary Search Tree (BST) and two bounds l and r, we need to count all nodes whose values fall within the range [l, r] (inclusive). For example, if we have a BST with nodes and l = 7, r = 13, we count nodes with values 8, 10, and 12, giving us a result of 3. 12 8 15 3 ...

Read More

Program to check whether given number is Narcissistic number or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 899 Views

A Narcissistic number (also called an Armstrong number) is a number that equals the sum of its digits raised to the power of the number of digits. For example, 153 is narcissistic because 1³ + 5³ + 3³ = 153. So, if the input is like 9474, then the output will be True as 9⁴ + 4⁴ + 7⁴ + 4⁴ = 6561 + 256 + 2401 + 256 = 9474. Algorithm To solve this, we will follow these steps − Convert the number to string to get individual digits Calculate the sum of each ...

Read More
Showing 6301–6310 of 8,546 articles
« Prev 1 629 630 631 632 633 855 Next »
Advertisements