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 57 of 377
Program to find reformatted phone number in Python
Phone number formatting is a common task in data processing. Given a phone number string containing digits, spaces, and dashes, we need to reformat it according to specific grouping rules. Problem Requirements The reformatting follows these rules: Remove all spaces and dashes first Group digits from left to right into blocks of 3 until 4 or fewer digits remain Handle the final digits based on count: 2 digits: Single block of length 2 3 digits: Single block of length 3 4 digits: Two blocks of length 2 each Join all blocks with dashes ...
Read MoreProgram to count number of matches played in tournament in Python
Suppose we have a number n representing the number of teams in a tournament. The tournament follows specific rules to determine matches and winners ? If the number of teams is even, each team gets paired with another team. A total of (n/2) matches are played, and (n/2) winner teams advance to the next round. If the number of teams is odd, one team randomly advances to the next round without playing. The remaining teams get paired, so (n-1)/2 matches are played, and (n-1)/2 + 1 teams advance as winners. ...
Read MoreProgram to count the number of consistent strings in Python
Suppose we have a string s consisting of distinct characters and also have an array of strings called words. A string is consistent when all characters in the string appear in the string s. We have to find the number of consistent strings present in the array words. So, if the input is like s = "px", words = ["ad", "xp", "pppx", "xpp", "apxpa"], then the output will be 3 because there are three strings with only 'p' and 'x': ["xp", "pppx", "xpp"]. Algorithm To solve this, we will follow these steps − ...
Read MoreProgram to find goal parser interpretation command in Python
Suppose we have a Goal Parser that can interpret a given string command. A command consists of: An alphabet "G" Opening and closing parenthesis "()" and/or "(al)" in some order Our Goal Parser will interpret "G" as the string "G", "()" as "o", and "(al)" as the string "al". Finally interpreted strings are then concatenated in the original order. Problem Statement Given a string command, we need to find the Goal Parser's interpretation of the command. For example, if the input is command = "G()()()(al)(al)", then the output will be "Goooalal". Approach ...
Read MorePython Program to find out the number of sets greater than a given value
Suppose we have an array containing several integer numbers. We need to find all contiguous subarrays, replace each subarray with its maximum element, and count how many of these maximum values are greater than a given number k. So, if the input is like input_array = [5, 6, 7, 8], k = 7, then the output will be 4. Understanding the Problem The contiguous subarrays from the given input array are: {5}, {6}, {7}, {8}, {5, 6}, {6, 7}, {7, 8}, {5, 6, 7}, {6, 7, 8}, {5, 6, 7, 8} If we replace each subarray ...
Read MorePython Program to find out the determinant of a given special matrix
Determinants of special matrices formed from tree structures have important applications in graph theory and computational mathematics. In this problem, we construct an n×n matrix A where each element A(x, y) equals the weight of the least common ancestor (LCA) of vertices x and y in a tree. Problem Understanding Given a tree with n vertices labeled 1 to n, where vertex 1 is the root and each vertex has weight wi, we need to find the determinant of matrix A where A(x, y) = weight_of_LCA(x, y). Example Tree Structure For input edges [[1, 2], [1, ...
Read MorePython Program to find out the number of rooms in which a prize can be hidden
Suppose in a game show there are 2n rooms arranged in a circle. One room contains a prize that participants must find. The rooms are numbered 1, 2, 3, ..., n, -n, -(n-1), ..., -1 in clockwise order. Each room has a door with a marking x that indicates the distance to the next room. If x is positive, the door opens to the xth room clockwise; if negative, it opens to the xth room counter-clockwise. We need to find how many rooms can hide the prize such that participants cannot find it by following the door sequence. ...
Read MorePython Program to find out how many cubes are cut
Suppose we have several cubes of dimensions a, b, and c, and using them we create a new box of dimension a×b×c. The values a, b, and c are pairwise co-prime, meaning gcd(a, b) = gcd(b, c) = gcd(a, c) = 1. We need to cut the box into two pieces with a single slice and determine how many individual cubes are cut into two pieces. The cut is made through a plane that passes through specific vertices, creating a diagonal slice through the 3D box structure. Problem Statement Given an array containing multiple sets ...
Read MorePython Program to find out how many times the balls will collide in a circular tube
Suppose there are n balls in a circular tube that is 100 meters long. Initially, each ball is positioned at a specific distance from a starting point. The balls travel in different directions at a speed of 0.1 meters per second. When two balls meet, they collide and change their direction of travel. We need to find the total number of collisions that occur over 10^9 + 6 seconds. Problem Analysis The key insight is that balls moving in opposite directions will collide multiple times as they circle the tube. The collision count depends on: Number ...
Read MorePython Program to find the number of operations to pack several metal bars in a container
Suppose we are given the task to transport several metal bars of different sizes. The transportation container has limited length and can only contain bars of length 1. We are provided n number of bars with their lengths given in a list. To fit all bars in the container, we must cut and divide them into unit sizes, then pack them (which costs one operation per bar). The key insight is that cutting a bar optimally requires finding its prime factorization and cutting along those factors to minimize operations. Problem Analysis For input input_arr = [6, 3, ...
Read More