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 630 of 855
Program to find number of moves to win deleting repeated integer game in Python
Suppose two friends Amal and Bimal are playing a game with a sorted list of numbers called nums. In this game in a single turn, Amal chooses any three numbers. Bimal removes one of them, and then Amal removes one of them. The list starts out with an odd number of elements. Here Amal wishes to minimize the number of turns required to make the list contain no repeated elements, Bimal wishes to maximize the number of turns. If Amal and Bimal act optimally, we have to find how many turns are needed for this game. So, if the ...
Read MoreProgram to make target by removing from first or last and inserting again in Python
In this problem, we have two strings S and T that are permutations of each other. We can perform an operation where we remove either the first or last character from S and insert it anywhere in the string. The goal is to find the minimum number of operations needed to convert S into T. Problem Understanding Given strings s = "zyvxw" and t = "vwxyz", we need to transform s into t using the allowed operations. The key insight is to find the longest common subsequence that appears in the same order in both strings, then calculate ...
Read MoreProgram to delete all leaves with even values from a binary tree in Python
Suppose we have a binary tree, we will repeatedly delete all leaves that have even values. After deleting all even-valued leaves, if only the root remains and it has an even value, that will be deleted as well. So, if the input is like ? 13 12 14 16 22 ...
Read MoreProgram to find number of ways we can decode a message in Python
Suppose we have a mapping like a = 1, b = 2, ... z = 26, and we have an encoded message string. We need to count the number of ways it can be decoded using dynamic programming. For example, if the input is message = "222", then the output will be 3, as this can be decoded in 3 ways: "bbb" (2-2-2), "bv" (2-22), and "vb" (22-2). Algorithm To solve this problem, we will follow these steps − Create a memo array of size message length + 1 initialized with zeros Set memo[0] = ...
Read MoreProgram to check two parts of a string are palindrome or not in Python
Suppose we have two strings S and T of same length, we have to check whether it is possible to cut both strings at a common point so that the first part of S and the second part of T form a palindrome. So, if the input is like S = "cat" T = "dac", then the output will be True, as if we cut the strings into "c" + "at" and "d" + "ac", then "c" + "ac" is a palindrome. Algorithm To solve this, we will follow these steps − ...
Read MoreProgram to check whether we can take all courses or not in Python
Suppose we have a 2D matrix where matrix[i] represents the list of prerequisite courses needed to enroll in course i. We need to check whether it is possible to take all courses without encountering circular dependencies. So, if the input is like matrix = [[1], [2], []], then the output will be True, as we can take course 2, then course 1, and then course 0. Algorithm Overview This problem is essentially detecting cycles in a directed graph. We use Depth-First Search (DFS) with two tracking arrays: vis − tracks nodes currently in the DFS ...
Read MoreProgram to find a sublist where first and last values are same in Python
Suppose we have a list of numbers called nums, we have to find the number of sublists where the first element and the last element are same. So, if the input is like nums = [10, 15, 13, 10], then the output will be 5, as the sublists with same first and last element are: [10], [15], [13], [10], [10, 15, 13, 10]. Algorithm To solve this, we will follow these steps − num_sublists := size of nums d := an empty map for each n ...
Read MoreProgram to find duplicate element from n+1 numbers ranging from 1 to n in Python
Suppose we have a list of numbers called nums of length n + 1. These numbers are picked from range 1, 2, ..., n. As we know, using the pigeonhole principle, there must be a duplicate. We have to find that and return it. So, if the input is like [2, 1, 4, 3, 3], then the output will be 3. Approach To solve this, we will follow these steps − l := size of nums temp := l*(l-1) /2 temp_sum := sum ...
Read MoreProgram to check programmers convention arrangements are correct or not in Python
Suppose we have a number n representing programmers looking to enter a convention, and we also have a list of numbers where 1 represents a programmer and 0 represents empty space. The condition is that no two programmers can sit next to each other. We need to check whether all n programmers can enter the convention or not. So, if the input is like n = 2, convention = [0, 0, 1, 0, 0, 0, 1], then the output will be True because we can place 2 more programmers without violating the adjacency rule. Algorithm To solve ...
Read MoreProgram to find the formatted amount of cents of given amount in Python
When dealing with financial calculations in Python, we often need to format amounts from cents to currency format. This involves converting a numeric value representing cents into a properly formatted string with dollars and cents separated by a decimal point, and thousands separated by commas. So, if the input is like n = 123456, then the output will be "1, 234.56". Algorithm To solve this, we will follow these steps − Convert the number to a string to work with digits Handle special cases where the amount is less than $1.00 Separate the dollars and ...
Read More