Server Side Programming Articles

Page 507 of 2109

How to select subset of data with Index Labels in Python Pandas?

Kiran P
Kiran P
Updated on 25-Mar-2026 2K+ Views

Pandas provides powerful selection capabilities to extract subsets of data using either index positions or index labels. This article demonstrates how to select data using index labels with the .loc accessor. The .loc attribute works similar to Python dictionaries, selecting data by index labels rather than positions. This is different from .iloc which selects by integer position like Python lists. Setting Up the Dataset Let's start by importing a movies dataset with the title as the index ? import pandas as pd movies = pd.read_csv("https://raw.githubusercontent.com/sasankac/TestDataSet/master/movies_data.csv", ...

Read More

How to Find The Largest Or Smallest Items in Python?

Kiran P
Kiran P
Updated on 25-Mar-2026 478 Views

Finding the largest or smallest items in a collection is a common task in Python. This article explores different methods to find single or multiple largest/smallest values efficiently. Method 1: Using min() and max() for Single Items For finding a single smallest or largest item (N=1), min() and max() are the most efficient functions ? import random # Create a random list of integers random_list = random.sample(range(1, 10), 9) print("List:", random_list) # Find the smallest number smallest = min(random_list) print("Smallest:", smallest) # Find the largest number largest = max(random_list) print("Largest:", largest) ...

Read More

How to Identify Most Frequently Occurring Items in a Sequence with Python?

Kiran P
Kiran P
Updated on 25-Mar-2026 367 Views

When analyzing sequences of data, identifying the most frequently occurring items is a common task. Python's Counter from the collections module provides an elegant solution for counting and finding the most frequent elements in any sequence. What is a Counter? The Counter is a subclass of dictionary that stores elements as keys and their counts as values. Unlike regular dictionaries that raise a KeyError for missing keys, Counter returns zero for non-existent items. from collections import Counter # Regular dictionary raises KeyError regular_dict = {} try: print(regular_dict['missing_key']) except KeyError as e: ...

Read More

Program to check whether given list is in valid state or not in Python

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

Sometimes we need to check if a list can be completely partitioned into valid groups. This problem involves grouping numbers using specific rules to determine if the entire list is in a "valid state". Problem Definition Given a list of numbers, check if every number can be grouped using one of these rules: Contiguous pairs: (a, a) − two identical numbers Identical triplets: (a, a, a) − three identical numbers Consecutive triplets: (a, a+1, a+2) − three consecutive numbers Example For nums = [7, 7, 3, 4, 5], we can group [7, 7] ...

Read More

Program to find all upside down numbers of length n in Python

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

An upside down number (also called a strobogrammatic number) is a number that appears the same when rotated 180 degrees. The digits that remain valid when rotated are: 0, 1, 6, 8, and 9, where 6 becomes 9 and 9 becomes 6 when rotated. So, if the input is like n = 2, then the output will be ['11', '69', '88', '96']. Understanding Valid Digits When rotated 180 degrees ? 0 → 0 1 → 1 6 → 9 ...

Read More

Program to check all values in the tree are same or not in Python

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

Suppose we have a binary tree, we have to check whether all nodes in the tree have the same values or not. This is a common tree traversal problem that can be solved using recursive depth-first search. So, if the input is like ? 5 5 5 5 5 ...

Read More

Program to check occurrences of every value is unique or not in Python

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

Suppose we have a list of numbers nums (positive or negative), we have to check whether the number of occurrences of every value in the array is unique or not. So, if the input is like nums = [6, 4, 2, 9, 4, 2, 2, 9, 9, 9], then the output will be True, as there is 1 occurrence of 6, 2 occurrences of 4, 3 occurrences of 2, and 4 occurrences of 9. So all number of occurrences are unique. Approach To solve this, we will follow these steps − ...

Read More

Program to check whether we can pick up and drop every passenger in given list in Python

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

Suppose we have a matrix called requested_trips where each row contains [start_x, end_x, num_passengers], and we also have a capacity value. Each requested trip asks to pick up num_passengers passenger at start_x and drop them off at end_x. We have a car with the given capacity that starts at position x = 0. The car can only move to the right side, and we need to check whether we can pick up and drop off everyone without exceeding the capacity. So, if the input is like trips = [[1, 25, 2], [3, 4, 3], [5, 12, 3]] and capacity ...

Read More

Program to find the sum of the lengths of two nonoverlapping sublists whose sum is given in Python

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

Given a list of numbers and a target value k, we need to find two nonoverlapping sublists whose sum equals k and return the sum of their lengths. When multiple solutions exist, we choose the two shortest sublists. Problem Understanding For example, with nums = [7, 10, -2, -1, 4, 3] and k = 7, we can find sublists [7] (length 1) and [4, 3] (length 2), giving us a total length of 3. We don't choose [10, -2, -1] because it's longer than [7]. Algorithm Approach The solution uses a two-pass approach with prefix and ...

Read More

Program to check two trees are exactly same based on their structure and values in Python

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

Suppose we have two binary trees, we have to check whether they are exactly same in terms of their structures and values or not. We can say them as twin trees. So, if the input is like ? Tree 1 10 5 15 Tree 2 10 ...

Read More
Showing 5061–5070 of 21,090 articles
« Prev 1 505 506 507 508 509 2109 Next »
Advertisements