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
Articles by Arnab Chakraborty
Page 16 of 377
Program to form smallest number where no two adjacent digits are same in Python
Suppose we have a string s with four possible characters "1", "2", "3" and "?". We can place any one of "1", "2" and "3" in the place of "?". We have to find the smallest possible number that we can make such that no two adjacent digits are same. So, if the input is like s = "2??3?", then the output will be 21231. Algorithm To solve this, we will follow these steps − Convert the string to a list for easy modification Handle the special ...
Read MoreProgram to find length of shortest sublist with maximum frequent element with same frequency in Python
Suppose we have a list of numbers called nums. If the frequency of a most frequent number in nums is k, we have to find the length of the shortest sublist such that the frequency of its most frequent item is also k. So, if the input is like nums = [10, 20, 30, 40, 30, 10], then the output will be 3, because here the most frequent numbers are 10 and 30, where k = 2. If we select the sublist [30, 40, 30], this is the shortest sublist where 30 appears with its maximum frequency of 2. ...
Read MoreProgram to check right rotation forms increasing or decreasing array with first n natural numbers or not in Python
Suppose we have a list of numbers called nums, where n elements are present. We have to check whether we can make a list with first n natural numbers either in increasing or decreasing fashion, like [1, 2, ..., n] or [n, n - 1, ..., 1] by shifting nums to the right any number of times or not. So, if the input is like nums = [5, 6, 1, 2, 3, 4], then the output will be True, because we can shift them four times to make the array [1, 2, 3, 4, 5, 6]. Algorithm ...
Read MoreProgram to define set data structure without using library set class in Python
A set is a collection of unique elements. We can implement a custom set data structure without using Python's built-in set class by creating a class with basic set operations. Set Operations to Implement Our custom set class will support the following methods ? Constructor to create a new set instance add(val) to insert an integer into the set exists(val) to check if a value exists in the set remove(val) to remove a value from the set Implementation Approach We'll use a dictionary where each key represents a value and maps to a ...
Read MoreProgram to check whether all can get a seat or not in Python
Suppose we have a number n, there are n number of people searching for a seat, we also have a list of bits where 1 represents an already occupied seat and 0 represents empty seat. No two people can sit next to each other, so we have to check whether all n people can find a seat or not. So, if the input is like n = 2 seats = [1, 0, 0, 0, 1, 0, 0], then the output will be True, because they can seat at index 2 and 6. Approach To solve this, we ...
Read MoreProgram to implement run length string decoding iterator class in Python
Run-length encoding compresses data by representing consecutive identical characters as a count followed by the character. This article implements an iterator class that decodes such strings character by character using next() and hasnext() methods. Problem Understanding Given a run-length encoded string like "2b1a", we need to create an iterator that can decode it step by step. The string "2b1a" represents "bba" (2 b's followed by 1 a). Iterator Methods next() − Returns the next character in the decoded sequence hasnext() − Checks if more characters are available Algorithm Steps The solution follows ...
Read MoreProgram to count operations to remove consecutive identical bits in Python
Suppose we have a binary string s, now let us consider an operation where we select a bit and flip its value from 0 to 1 or vice-versa. We have to find the minimum number of operations needed to get a string with no three identical consecutive bits. So, if the input is like s = "10011100", then the output will be 1, because we can flip 1 to 0 the bit at index 4 to make the string "10010100" where there are no three consecutive identical bits. Algorithm To solve this, we will follow these steps ...
Read MoreProgram to define data structure that supports rate limiting checking for user in Python
Rate limiting is a technique used to control the frequency of requests from users. We need to design a data structure that tracks user requests and determines if a new request should be allowed based on an expiration time window. The data structure should support checking whether a user's request fails based on their previous successful request. A request fails (returns True) only when the user had a successful request within the expire time window. Problem Understanding Given an expire time, we need to: Track the last successful request timestamp for each user Check if a ...
Read MoreProgram to define data structure that supports range sum in Python
Suppose we want to develop a data structure that can build up with a list of integers, and there is a function to find sum of elements from index i to index j-1 whenever we require in an efficient way. There are two functions: Constructor that constructs a new instance with the integer array. get_sum(i, j) returns the sum of integers of array elements from starting index i and ending index j-1. So, if the input is like array = [5, 2, 3, 6, 4, 7, 8, 9, ...
Read MorePython Pandas - Return a new Timedelta with seconds floored resolution
To return a new Timedelta floored to this resolution, use the timedelta.floor() method. For seconds floored resolution, set the freq parameter to 'S'. What is Timedelta Flooring? Flooring a Timedelta means rounding down to the nearest specified time unit. When you floor to seconds resolution, all sub-second components (milliseconds, microseconds, nanoseconds) are removed. Syntax timedelta.floor(freq) Parameters: freq − The frequency string. Use 'S' for seconds resolution. Example Let's create a Timedelta object with various time components and floor it to seconds resolution − import pandas as pd ...
Read More