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
Programming Articles
Page 263 of 2547
Program to find array by swapping consecutive index pairs in Python
Given a list of numbers, we need to swap consecutive pairs at even indexes with each other, and consecutive pairs at odd indexes with each other. This creates a specific pattern where elements at positions (0, 2), (1, 3), (4, 6), (5, 7), etc. are swapped. Problem Understanding The swapping pattern works as follows ? Swap elements at indexes 0 and 2 (first even pair) Swap elements at indexes 1 and 3 (first odd pair) Swap elements at indexes 4 and 6 (second even pair) Swap elements at indexes 5 and 7 (second odd pair) Continue ...
Read MorePython Pandas - How to create a RangeIndex
A RangeIndex is a memory-efficient special case of Int64Index in Pandas that represents monotonic ranges. It's particularly useful for creating sequential indices without storing every individual value in memory. What is RangeIndex? RangeIndex is similar to Python's range() function but designed for Pandas indexing. It stores only the start, stop, and step values, making it memory-efficient for large sequential indices. Basic Syntax pd.RangeIndex(start=0, stop=None, step=1, name=None) Creating a Simple RangeIndex Here's how to create a basic RangeIndex with default parameters ? import pandas as pd # Create a simple ...
Read MorePython Pandas - Compute slice locations for input labels
The slice_locs() method in Pandas allows you to compute slice locations for input labels in an Index. It returns a tuple containing the start and end positions that can be used for slicing operations. Syntax Index.slice_locs(start=None, end=None, step=None, kind=None) Parameters The method accepts the following parameters: start − Label to start the slice from (optional) end − Label to end the slice at (optional) step − Step size for slicing (optional) kind − Type of slicing behavior (optional) Basic Example Let's create an Index and find slice locations between ...
Read MoreProgram to check sum of two numbers is up to k from sorted List or not in Python
Suppose we have a list of numbers called nums and the elements in nums are sorted in ascending order. We also have another value k, we have to check whether any two elements taken from the list add up to k or not. The numbers can also be negative or 0. We have to solve this problem in constant amount of space usage. So, if the input is like nums = [-8, -3, 2, 7, 9] k = 4, then the output will be True, because if we take 7 and -3, then the sum is 7 + (-3) ...
Read MoreProgram to find sum of two numbers which are less than the target in Python
Suppose we have a list of numbers called nums and also have a target value, we have to find the sum of the largest pair of numbers in nums whose sum is at most (target-1). So, if the input is like nums = [8, 3, 4, 9, 2] target = 8, then the output will be 7, because the sum of the largest pair of numbers less than 8 is 4 + 3 = 7. Algorithm To solve this, we will follow these steps ? Sort the list nums Initialize p1 := 0 (left pointer) ...
Read MoreProgram to create data structure to check pair sum is same as value in Python
Sometimes we need to build a data structure that efficiently checks if any two elements sum to a target value. This problem requires designing a class with two methods: add() to insert values and find() to check pair sums. Problem Statement We need to create a data structure with two operations ? add(val) − adds the value to the data structure find(val) − checks whether there are two elements whose sum equals val The key requirement is efficiency − we should be able to answer queries quickly without searching through all numbers every time. ...
Read MoreProgram to check typed string is for writing target string in stuck keyboard keys or not in Python
Sometimes keyboard keys get stuck and produce repeated characters when typing. This program checks if a typed string could have been intended to write a target string, accounting for stuck keys that repeat characters. For example, if we want to type "apple" but the 'p' and 'e' keys are stuck, we might end up typing "appppleee". This program verifies if such typing errors are possible. Problem Statement Given two strings s (typed string) and t (target string), determine if s could be the result of typing t with some stuck keyboard keys. Example If s ...
Read MoreProgram to check whether list is alternating increase and decrease or not in Python
Suppose we have a list of numbers called nums. We have to check whether the list alternates starting from strictly increasing then strictly decreasing and then strictly increasing and so on. If the list is only strictly increasing, it will also be considered valid. So, if the input is like nums = [2, 4, 8, 7, 5, 1, 5, 7, 2, 1], then the output will be True, because [2,4,8] are increasing, then [7,5,1] is decreasing, then again [5,7] is increasing and [2,1] is decreasing. Algorithm To solve this, we will follow these steps ? If nums[1]
Read MoreProgram to find squared elements list in sorted order in Python
When we have a sorted array with negative and positive numbers, squaring them can disrupt the order. We need to find squared elements and return them in sorted order efficiently. So, if the input is like nums = [-8, -3, 0, 5, 6], then the output will be [0, 9, 25, 36, 64] Algorithm To solve this efficiently in O(n) time, we use a two-pointer approach ? Initialize left pointer at start (0) and right pointer at end (n-1) Create result array and fill from the end (largest squared values first) Compare absolute values at ...
Read MoreProgram to sort numbers based on 1 count in their binary representation in Python
Sorting numbers based on the count of 1s in their binary representation is a common programming problem. When two numbers have the same count of 1s, we sort them by their actual values in ascending order. So, if the input is like nums = [4, 1, 12, 7, 6], then the output will be [1, 4, 6, 12, 7], because − Binary form of 4 is 100 (one 1) Binary form of 1 is 1 (one 1) Binary form of 6 is 110 (two 1s) Binary form of 12 is 1100 (two 1s) Binary form of 7 ...
Read More