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 118 of 855
Program to check whether two string arrays are equivalent or not in Python
Suppose we have two string arrays word1 and word2, we need to check whether they represent the same string when their elements are concatenated in order. A string can be represented by an array if concatenating all elements in that array forms the original string. So, if the input is like word1 = ["ko", "lka", "ta"] and word2 = ["k", "olk", "at", "a"], then the output will be True as both arrays form "kolkata" when concatenated. Approach To solve this, we will follow these steps: Initialize two empty strings s1 and s2 ...
Read MoreProgram to decrypt code to defuse the bomb in Python
Suppose there is a bomb that you are going to defuse, and your time is running out! You have a circular array code of length n and have a key k. To decrypt the code, you must replace every number simultaneously based on these rules ? If k > 0 then replace ith number with the sum of next k numbers. If k < 0 then replace ith number with the sum of previous k numbers. If k = 0 then replace ith number with 0. ...
Read MoreProgram to find maximum in generated array in Python
Given a number n, we need to generate an array A of length n + 1 using specific rules and find the maximum value in it. Array Generation Rules The array is built following these rules ? A[0] = 0 A[1] = 1 A[2 * i] = A[i] if 2 ≤ 2 * i ≤ n A[2 * i + 1] = A[i] + A[i + 1] if 2 ≤ 2 * i + 1 ≤ n Example Walkthrough For n = 5, let's see how the array is constructed ? ...
Read MoreProgram to check we can form array from pieces or not in Python
Suppose we have an array nums where all elements are unique and have another array with different smaller arrays called pieces. We have to check whether we can get the main array nums by concatenating the arrays in pieces in any order or not. But we are not allowed to reorder the elements present in each array pieces[i]. So, if the input is like nums = [5, 1, 12, 36, 2, 47, 6] pieces = [[2, 47, 6], [12, 36], [1], [5]], then the output will be True because we can concatenate them in this order [[5], [1], [12, ...
Read MoreProgram to sort array by increasing frequency of elements in Python
Suppose we have an array with some elements where elements may appear multiple times. We have to sort the array such that elements are sorted according to their increasing frequency. Elements that appear fewer times will come first, and when frequencies are equal, larger elements come first. So, if the input is like nums = [1, 5, 3, 1, 3, 1, 2, 5], then the output will be [2, 5, 5, 3, 3, 1, 1, 1] Algorithm Steps To solve this, we will follow these steps ? Create a frequency map to count occurrences of ...
Read MoreProgram to find largest substring between two equal characters in Python
Suppose we have a string s, we have to find the length of the longest substring between two equal letters or elements, excluding the two characters. If we cannot find such substring, then return -1. So, if the input is like s = "level", then the output will be 3 as optimal substrings can be either "lev" or "vel". Algorithm To solve this, we will follow these steps − memo := a new map for i in range 0 to size of s - 1, do if s[i] is in memo, then insert ...
Read MoreProgram to find mean of array after removing some elements in Python
Suppose we have an array called nums, we have to find the mean of the remaining values after removing the smallest 5% and the largest 5% of the elements. So, if the input is like nums = [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8], then the output will be 4.0 because after removing smallest and largest values, all remaining elements are the same. Algorithm To solve this problem, we will follow these steps − Sort the array nums n := size of ...
Read MoreProgram to find X for special array with X elements greater than or equal X in Python
A special array is one where there exists a number x such that exactly x elements in the array are greater than or equal to x. We need to find this value x, or return -1 if no such value exists. For example, in the array [4, 6, 7, 7, 1, 0], there are 4 numbers (4, 6, 7, 7) that are greater than or equal to 4, making x = 4 the special value. Algorithm To solve this problem, we follow these steps ? Iterate through all possible values of ...
Read MoreProgram to design parking system in Python
Suppose you want to design a parking system. A parking lot has three different kinds of parking spaces − big, medium, and small. Each size has a fixed number of available slots. We'll create a class called ParkingSystem with two methods ? constructor(big, medium, small) − Takes the number of slots available for different spaces and initializes the ParkingSystem object. addCar(carType) − Checks whether there is a parking space of the given carType for the car that wants to park. The three slot types big, medium, and small are represented by 1, 2, and 3 respectively. ...
Read MoreProgram to find minimum jump needed to return from a folder to home in Python
Suppose we have logs where we have a path to enter into folders. There are different symbols that represent navigation operations − "../": Move to the parent folder from current one. (If we are at main folder, do not change location). "./": Remain in the current folder. "x/": Move to the child folder named x. From the logs we need to find the minimum number of operations needed to come back from the last folder where we stop to the main folder. Problem Understanding If the input is like logs = ["Dir1/", "Dir2/", "../", ...
Read More