Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 627 of 855
Program to interleave list elements from two linked lists in Python
Suppose we have two linked lists l1 and l2, we have to return one linked list by interleaving elements of these two lists starting with l1. If there are any leftover nodes in a linked list, they should be appended to the result. So, if the input is like l1 = [5, 4, 6, 3, 4, 7] l2 = [8, 6, 9], then the output will be [5, 8, 4, 6, 6, 9, 3, 4, 7] Algorithm To solve this, we will follow these steps − ans := l1 ...
Read MoreProgram to count n digit integers where digits are strictly increasing in Python
Suppose we have a number n, we have to find the number of n-digit positive integers such that the digits are in strictly increasing order. So, if the input is like n = 3, then the output will be 84, as numbers are 123, 124, 125, ..., 678, 789. Understanding the Problem For n-digit numbers with strictly increasing digits, we need to choose n different digits from {1, 2, 3, ..., 9}. We cannot use 0 as the first digit in an n-digit number. This becomes a combination problem: C(9, n). Algorithm To solve this, ...
Read MoreProgram to find minimum number of heights to be increased to reach destination in Python
Sometimes we need to find the minimum height increase required to reach from the top-left corner to the bottom-right corner of a matrix. We can only move to adjacent cells if their height is less than or equal to our current cell's height, but we can increase any cell's height before moving. Problem Understanding Given a matrix M where M[r][c] represents the height of that cell, we need to find the minimum total height increase to create a path from (0, 0) to (R-1, C-1). Movement is allowed to adjacent cells (up, down, left, right) only if the ...
Read MoreHow to use for loop to print all the elements of a list in R?
**Issue:** This article is about R programming, not Python. Since you asked me to improve a Python article, I'll convert this to Python while maintaining the same teaching approach. In Python, you can use a for loop to iterate through all elements of a list. The syntax is straightforward: for item in list_name: where item represents each element in the list. Basic Syntax The basic syntax for iterating through a list in Python is ? for item in list_name: print(item) Example with Simple List Let's start with a ...
Read MoreProgram to group a set of points into k different groups in Python
Suppose we have a list of points and a number k. The points are in the form (x, y) representing Cartesian coordinates. We can group any two points p1 and p2 if the Euclidean distance between them is ≤ k. We have to find the total number of disjoint groups. So, if the input is like points = [[2, 2], [3, 3], [4, 4], [11, 11], [12, 12]], k = 2, then the output will be 2, as it can make two groups: ([2, 2], [3, 3], [4, 4]) and ([11, 11], [12, 12]). Algorithm To solve ...
Read MoreHow to find the statistical summary of an R data frame with all the descriptive statistics?
When analyzing data in R, the default summary() function provides basic statistics like minimum, quartiles, mean, and maximum. However, for comprehensive statistical analysis, we need additional descriptive measures such as variance, standard deviation, skewness, and kurtosis. The basicStats() function from the fBasics package provides all these descriptive statistics in one output. Loading Required Package First, install and load the fBasics package ? library(fBasics) Example 1: mtcars Dataset Let's examine the built-in mtcars dataset ? data(mtcars) head(mtcars, 10) ...
Read MoreProgram to count number of strings we can make using grammar rules in Python
Suppose we have a number n, we have to find the number of strings of length n can be generated using the following grammar rules ? Each character is a lower case vowel [a, e, i, o, u] "a" may only be followed by one "e" "e" may only be followed by any of "a" and "i" "i" may not be followed by another "i" "o" may only be followed by any of "i" and "u" "u" may only be followed by one "a" If the result is very large, mod the result by 10^9 + ...
Read MoreProgram to implement the fractional knapsack problem in Python
The fractional knapsack problem is a greedy optimization algorithm where we can take fractions of items to maximize value within a weight capacity. Unlike the 0/1 knapsack, we can break items and take partial amounts. The strategy is to sort items by their value-to-weight ratio in descending order, then greedily select items starting with the highest ratio. Algorithm Steps To solve this problem, we follow these steps − Calculate value-to-weight ratio for each item Sort items by ratio in descending order For each ...
Read MoreProgram to find correct order of visited cities in C++
Given a list of airline tickets represented by pairs of departure and arrival airports, we need to reconstruct the correct travel itinerary. All tickets belong to a traveler who starts from KLK airport, so the itinerary must begin there. For example, if the input is [["MUC", "LHR"], ["KLK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]], the output should be ["KLK", "MUC", "LHR", "SFO", "SJC"]. Algorithm We'll use a depth-first search (DFS) approach with the following steps − Build a graph where each airport maps to its destinations Use DFS ...
Read MoreProgram to find first positive missing integer in range in Python
Suppose we have a list of sorted distinct integers of size n, we have to find the first positive number in range [1 to n+1] that is not present in the array. So, if the input is like nums = [0, 5, 1], then the output will be 2, as 2 is the first missing number in range 1 to 4. Algorithm To solve this, we will follow these steps − target := 1 for each i in arr, do if i is same as target, then target := target + 1 ...
Read More