Server Side Programming Articles - Page 1540 of 2650

Program to find number of moves to win deleting repeated integer game in Python

Arnab Chakraborty
Updated on 07-Oct-2020 12:29:47

188 Views

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 Amla 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 input is ... Read More

Program to make target by removing from first or last and inserting again in Python

Arnab Chakraborty
Updated on 07-Oct-2020 12:27:15

142 Views

Suppose we have two strings S and T and they are permutations of each other. Suppose there is an operation where we remove either the first or the last character in S and insert it anywhere in the string. Then find the minimum number of operations needed to convert S into T.So, if the input is like s = "zyvxw" t = "vwxyz", then the output will be 3, as these operations are: Remove "w" and insert it after "v" to get "zyvwx" Remove "z" and insert it after "x" to get "yvwxz" Remove "y" and insert it after "x" ... Read More

Program to delete all leaves with even values from a binary tree in Python

Arnab Chakraborty
Updated on 07-Oct-2020 11:55:18

398 Views

Suppose we have we have a binary tree, we will repeatedly delete all leaves that have even values. After deleting all, if it has only root with even values, that will be deleted also.So, if the input is likethen the output will beTo solve this, we will follow these steps −Define a function solve() . This will take rootif root is null, thenreturn nullleft of root := solve(left of root)right of root := solve(right of root)if root is leaf and data of root is even, thenreturn nullreturn rootLet us see the following implementation to get better understanding −Exampleclass TreeNode:   ... Read More

Program to find number of ways we can decode a message in Python

Arnab Chakraborty
Updated on 07-Oct-2020 11:45:07

365 Views

Suppose we have mapping like 'a' = 1, 'b' = 2, ... 'z' = 26, and we have an encoded message message string, we have to count the number of ways it can be decoded.So, if the input is like message = "222", then the output will be 3, as This can be decoded 3 ways: bbb, bv, and vb.To solve this, we will follow these steps −memo := a list of 0s of size same as message size + 1memo[0] := 1memo[1] := 1 when message[0] is not same as "0" otherwise 0for i in range 2 to size ... Read More

Program to check two parts of a string are palindrome or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 11:07:03

338 Views

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 = "pac", then the output will be True, as If we cut the strings into "c" + "at" and "d" + "ac", then "c" + "ac" is a palindrome.To solve this, we will follow these steps −n := size of ai := 0while i < n and a[i] ... Read More

Program to check whether we can take all courses or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 11:03:47

190 Views

Suppose we have a 2D matrix where matrix[i] represents the list of prerequisite courses needed to enroll course i. Now, we have to check whether it is possible to take all courses or not.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.To solve this, we will follow these steps −Define a function dfs(). This will take iif vis[i] is true, thenreturn falseif chk[i] is true, thenreturn Truevis[i]:= Truefor each j in matrix[i], doif dfs(j) is false, thenreturn Falsevis[i]:= Falsechk[i]:= ... Read More

Program to find a sublist where first and last values are same in Python

Arnab Chakraborty
Updated on 07-Oct-2020 10:55:10

235 Views

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].To solve this, we will follow these steps −num_sublists := size of numsd := an empty mapfor each n in nums, dod[n] := d[n] + 1for each number k and corresponding frequency v of elements in d, doif v ... Read More

Program to find duplicate element from n+1 numbers ranging from 1 to n in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:31:30

293 Views

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 3To solve this, we will follow these steps −l := size of numstemp := l*(l-1) /2temp_sum := sum of all elements in numsreturn (temp_sum - temp)Let us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, ... Read More

Program to find all words which share same first letters in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:29:41

432 Views

Suppose we have a list of words in lowercase letters, we have to find the length of the longest contiguous sublist where all words have the same first letter.So, if the input is like ["she", "sells", "seashells", "on", "the", "seashore"], then the output will be 3 as three contiguous words are "she", "sells", "seashells", all have same first letter 's'.To solve this, we will follow these steps −maxlength := 0curr_letter := Null, curr_length := 0for each word in words, doif curr_letter is null or curr_letter is not same as word[0], thenmaxlength := maximum of maxlength, curr_lengthcurr_letter := word[0], curr_length := ... Read More

Program to check programmers convention arrangements are correct or not in Python

Arnab Chakraborty
Updated on 06-Oct-2020 07:28:09

127 Views

Suppose we have a number n, this is representing programmers looking to enter a convention, and we also have a list of number, convention 1 represents a programmer and 0 represents empty space. Now the condition is no two programmers can sit next to each other, we have 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 TrueTo solve this, we will follow these steps −for i in range 0 to size of conv, doa:= ... Read More

Advertisements