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 322 of 855
Program to insert new element into a linked list before the given position in Python
Inserting a new element into a linked list at a specific position is a common operation in data structures. This involves creating a new node and adjusting the links between existing nodes to maintain the list structure. So, if the input is like nums = [1, 5, 3, 6, 8] pos = 3 val = 7, then the output will be [1, 5, 3, 7, 6, 8]. Algorithm To solve this, we will follow these steps − new := create a linked list node with value same as val if pos is same as 0, ...
Read MoreProgram to find size of common special substrings of two given strings in Python
Suppose we have two strings s1 and s2. We have to find the size of longest string s3 which is a special substring of both s1 and s2. We can say a string x is a special substring of another string y if x can be generated by removing 0 or more characters from y. This is also known as the Longest Common Subsequence (LCS) problem. So, if the input is like s1 = 'pineapple' and s2 = 'people', then the output will be 5 as the special substring is 'peple', of size 5. Algorithm To ...
Read MoreProgram to find index whose left and right elements sums are equal in Python
Suppose we have a list of numbers called nums, we have to find the smallest index i such that the sum of the numbers which are present at the left of i is equal to the sum of numbers present at right of i. If we cannot find any such solution, return -1. So, if the input is like nums = [8, 2, 3, 6, 5, 2, 5, 9, 1, 2], then the output will be 4, because sum of elements that are left of index 4 is [8, 2, 3, 6] = 19, and sum of elements that ...
Read MoreProgram to check n can be represented as sum of k primes or not in Python
Suppose we have two inputs n and k. We have to check whether n can be represented as a sum of k number of prime values or not. So, if the input is like n = 30 k = 3, then the output will be True because 30 can be represented like 2 + 11 + 17. Algorithm To solve this, we will follow these steps − if n < k*2, then return False if k > 2, then return True if k is same as ...
Read MoreProgram to find all substrings whose anagrams are present in a string in Python
In this problem, we need to find all substrings in a string where there exists another substring at a different location that is an anagram of the first substring. An anagram contains the same characters but in different order. For example, if the input is s = "abcba", we need to find substrings like "a" (appears at positions 0 and 4), "ab" and "ba" (anagrams of each other), etc. Algorithm To solve this problem, we follow these steps − Generate all possible substrings of each length Group substrings by their sorted character representation (anagram signature) ...
Read MoreProgram to find smallest index for which array element is also same as index in Python
Suppose we have a list of elements called nums where all items are unique, and they are sorted in ascending order, we have to find the minimum i such that nums[i] = i. If we cannot find any solution, then return -1. We have to solve this problem in O(log(n)) time. So, if the input is like nums = [-4, -1, 2, 3, 8], then the output will be 2, because both nums[2] = 2 and nums[3] = 3 but 2 is smaller. Algorithm To solve this, we will follow these steps − ret := ...
Read MoreProgram to find first fit room from a list of rooms in Python
Suppose we have a list of numbers called rooms and another target value t. We have to find the first value in rooms whose value is at least t. If there is no such room, return -1. So, if the input is like rooms = [20, 15, 35, 55, 30] t = 30, then the output will be 35. Because 35 is the first room that can accommodate the target size of 30. Algorithm To solve this, we will follow these steps − for each ...
Read MoreProgram to check whether elements frequencies are even or not in Python
Suppose we have a list of elements called nums, we have to check whether all numbers appear even times or not. We have to solve it using constant space. So, if the input is like nums = [8, 9, 9, 8, 5, 5], then the output will be True, because all numbers have occurred twice. Algorithm To solve this, we will follow these steps ? If size of nums is odd, then return False Sort the list nums For i in range 1 ...
Read MoreProgram to find multiple of n with only two digits in Python
Suppose we have a number n. We have to find the least positive value x such that x is made up of only two digits 9's and 0's, and x is multiple of n. So, if the input is like n = 26, then the output will be 90090. Algorithm To solve this, we will follow these steps − m := 9 x := 1 while m is not divisible by n, do x := x + 1 m := replace all 1s with 9s in the binary form of x return ...
Read MoreProgram to sort given set of Cartesian points based on polar angles in Python
Suppose we have a set of Cartesian points in a list. We need to sort them based on their polar angles, which vary between 0 and 2π. If points have the same polar angles, we arrange them by their distance from the origin. For example, if we have points = [(1, 1), (1, -2), (-2, 2), (5, 4), (4, 5), (2, 3), (-3, 4)], the output will be [(5, 4), (1, 1), (4, 5), (2, 3), (-3, 4), (-2, 2), (1, -2)]. ...
Read More