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 637 of 855
Program to find number of tasks can be finished with given conditions in Python
Suppose we have a list of tasks and another list of people. The tasks[i] determines the amount of strength required to perform the ith task. And the people[i] determines the amount of strength the ith person has. We need to find the maximum number of tasks that can be finished if one person can perform at most one task. So, if the input is like tasks = [4, 3, 9, 15], people = [10, 5, 3, 2], then the output will be 3. The first person (strength 10) can perform task requiring 9, second person (strength 5) can perform ...
Read MoreProgram to find minimum number of operations required to make one number to another in Python
Given two numbers start and end (where start < end), we need to find the minimum number of operations required to convert start to end using these operations: Increment by 1 Multiply by 2 For example, if start = 5 and end = 11, the output will be 2. We can multiply 5 by 2 to get 10, then add 1 to get 11. Algorithm The key insight is to work backwards from the end number. Instead of thinking "how to reach end from start", we think "how did we reach end from start" ...
Read MoreProgram to find any two numbers in a list that sums up to k in Python
Suppose we have a list of numbers called nums and we have another number k, we have to check whether any two numbers present in the list add up to k or not. Same elements must not be used twice, and numbers can be negative or 0. So, if the input is like nums = [45, 18, 9, 13, 12], k = 31, then the output will be True, as 18 + 13 = 31. Approach To solve this, we will follow these steps − temp_set := a new set ...
Read MoreProgram to find the sum of all digits of given number in Python
Finding the sum of digits of a number is a common programming problem. We need to extract each digit from the number and add them together without converting the number to a string. So, if the input is like num = 512, then the output will be 8, as 8 = 5 + 1 + 2. To solve this, we will follow these steps ? Initialize sum = 0 while num is not equal to 0, do ...
Read MoreProgram to find minimum number of operations required to make one string substring of other in Python
Suppose we have two strings s and t, we have to find the minimum number of operations required to make t a substring of s. In each operation, we can choose any position in s and change the character at that position to any other character. For example, if s = "abbpqr" and t = "bbxy", then the output will be 2. We can take the substring "bbpq" from s and change 'p' to 'x' and 'q' to 'y' to make "bbxy" a substring. Approach The strategy is to check all possible substrings of s that have ...
Read MoreProgram to find nth sequence after following the given string sequence rules in Python
Suppose we have two strings s, t and another positive number n is also given, we have to find the nth term of the sequence A where: A[0] = s A[1] = t A[n] = A[n - 1] + A[n - 2] when n is even, otherwise A[n] = A[n - 2] + A[n - 1] As an example, if s = "a" and t = "b", then the sequence A would be: ["a", "b", "ba" ("b" + "a"), "bba" ("b" + "ba"), "bbaba" ("bba" + "ba")] ...
Read MoreProgram to find the maximum width of a binary tree in Python
The maximum width of a binary tree is the number of nodes that can fit between the leftmost and rightmost nodes at any level. This includes null nodes that could exist in between. Approach We'll use a depth-first search (DFS) approach where each node is assigned a position index. For any node at position pos, its left child gets position 2*pos and right child gets 2*pos+1. At each level, we track the minimum and maximum positions to calculate the width. Algorithm Steps Create a dictionary to store minimum and maximum positions for each depth level ...
Read MoreProgram to check whether one string can be 1-to-1 mapped into another string in Python
Sometimes we need to check whether one string can be mapped to another string with a 1-to-1 character mapping. This means each character in the first string maps to exactly one character in the second string, and vice versa. For example, if we have strings "papa" and "lili", we can create a valid mapping: "p" → "l" and "a" → "i". The character order remains unchanged. Algorithm To solve this problem, we use two dictionaries to track mappings in both directions ? Create two dictionaries: one for mapping characters from string s to t, another ...
Read MoreProgram to check whether one value is present in BST or not in Python
A Binary Search Tree (BST) is a tree data structure where each node has at most two children, and for each node, all values in the left subtree are smaller and all values in the right subtree are larger. We can efficiently search for a value by comparing it with the current node and moving left or right accordingly. .node { fill: #e8f4fd; stroke: #2196f3; stroke-width: 2; } .text { font-family: Arial; font-size: 14px; text-anchor: middle; fill: #333; ...
Read MoreProgram to check one string can be converted to other by shifting characters clockwise in Python
When working with strings, we sometimes need to check if one string can be transformed into another by shifting characters clockwise in the alphabet. For example, "c" can be converted to "e" using 2 clockwise shifts (c → d → e). In this problem, we have two strings p and q, and a number r representing the maximum allowed shifts. We need to determine if p can be converted to q by shifting characters clockwise at most r times in total. Example If p = "abc", q = "ccc", and r = 3, the answer is True ...
Read More