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
Python Articles
Page 55 of 852
Program to Find Out the Smallest Substring Containing a Specific String in Python
Suppose we have two strings s and t. We have to find the smallest substring in s, where t is also a subsequence of the substring. If that type of substring does not exist, we will return a blank string, and if there are multiple smallest substrings, we will take the leftmost one.So, if the input is like s = "abcbfbghfb", t = "fg", then the output will be fbgTo solve this, we will follow these steps −N := size of Sdp := a new list of size N initialized with infinityfor i in range 0 to N − 1, ...
Read MoreCheck if a point lies on or inside a rectangle in Python
Suppose we have a rectangle represented by two points bottom-left and top-right corner points. We have to check whether a given point (x, y) is present inside this rectangle or not.So, if the input is like bottom_left = (1, 1), top_right = (8, 5), point = (5, 4), then the output will be TrueTo solve this, we will follow these steps −Define a function solve() . This will take bl, tr, pif x of p > x of bl and x of p < x of tr and y of p > y of bl and y of p < ...
Read MoreCheck if a prime number can be expressed as sum of two Prime Numbers in Python
Suppose we have a prime number n. we have to check whether we can express n as x + y where x and y are also two prime numbers.So, if the input is like n = 19, then the output will be True as we can express it like 19 = 17 + 2To solve this, we will follow these steps −Define a function isPrime() . This will take numberif number
Read MoreCheck if a Queen can attack a given cell on chessboard in Python
Suppose we have two coordinates on a chessboard for queen and opponent. These points are Q and O respectively. We have to check whether the queen can attack the opponent or not. As we know that the queen can attack in the same row, same column and diagonally.So, if the input is like Q = (1, 1) O = (4, 4), then the output will be True as Q can go (4, 4) diagonally.To solve this, we will follow these steps −if x of Q is same as x of O, thenreturn Trueif y of Q is same as y ...
Read MoreCheck if a sorted array can be divided in pairs whose sum is k in Python
Suppose we have an array of numbers and have another number k, we have to check whether given array can be divided into pairs such that the sum of every pair is k or not.So, if the input is like arr = [1, 2, 3, 4, 5, 6], k = 7, then the output will be True as we can take pairs like (2, 5), (1, 6) and (3, 4).To solve this, we will follow these steps −n := size of arrif n is odd, thenreturn Falselow := 0, high := n - 1while low < high, doif arr[low] + ...
Read MoreCheck if a queue can be sorted into another queue using a stack in Python
Suppose we have a Queue with first n natural numbers (unsorted). We have to check whether the given Queue elements can be sorted in non-decreasing sequence in another Queue by using a stack. We can use following operations to solve this problem −Push or pop elements from stackDelete element from given Queue.Insert element in the other Queue.So, if the input is like Que = [6, 1, 2, 3, 4, 5], then the output will be True as we can pop 6 from Que, then push it into stack. Now pop all remaining elements from Que to another queue, then pop ...
Read MoreCheck if a string can be converted to another string by replacing vowels and consonants in Python
Suppose we have two strings s and t. We can only change a character at any position to any vowel if it is already a vowel or to a consonant if it is already a consonant. We have to check whether s can be represented to t or vice-versa.So, if the input is like s = "udpmva", t = "itmmve", then the output will be True as we can transform u -> i, d -> t, p -> m, a -> eTo solve this, we will follow these steps −s_size := size of sif s_size is not same as size ...
Read MoreCheck if a string can be obtained by rotating another string 2 places in Python
Suppose we have two strings s and t. We have to check whether we can get s by rotating t two place at any direction left or right.So, if the input is like s = "kolkata" t = "takolka", then the output will be True as we can rotate "takolka" to the left side two times to get "kolkata".To solve this, we will follow these steps −if size of s is not same as size of t, thenreturn Falseright_rot := blank stringleft_rot := blank stringl := size of tleft_rot := left_rot concatenate t[from index l - 2 to end] concatenate ...
Read MoreCheck if a string can be repeated to make another string in Python
Suppose we have two strings s and t, we have to find how many times the string s can be concatenated to generate t. If we cannot generate t using s, then return -1.So, if the input is like s = "tom" t = "tomtomtom", then the output will be 3 as we can concatenate "tom" 3 times to get "tomtomtom".To solve this, we will follow these steps −if size of t is not divisible by size of s, thenreturn -1cnt := quotient of (size of t / size of s)s := concatenate s cnt number of timesif s is ...
Read MoreCheck if a string can become empty by recursively deleting a given sub-string in Python
Suppose we have two strings s and t. We can delete t from s any number of times. And t appears only once at a time. We have to check whether s can become empty by removing t as many times as required.So, if the input is like s = "pipipinnn" t = "pin", then the output will be True as we can remove "pin" from "pipipinnn", then we will get "pipinn", again remove "pin" to get string "pin", then remove it to make it empty.To solve this, we will follow these steps −while size of s > 0, doposition ...
Read More