Server Side Programming Articles - Page 1473 of 2650

Program to evaluate Boolean expression from a string in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:15:08

932 Views

Suppose we have a string s containing a boolean expression with operators "and" and "or", evaluate it and return the result. Here the expressions may have parentheses, which should be evaluated first.So, if the input is like s = "T and (F or T)", then the output will be TrueTo solve this, we will follow these steps:stack := a new listt = list of elements of s split by blank spacesfor each v in t, doif v[0] is same as "(", thenpush true when v[from index of "(" to end] is same as "T", into stackotherwise when ")" is found, ... Read More

Program to find minimum bus fare for travelling all days in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:10:37

980 Views

Suppose we have a list of sorted numbers called days, where we must take the bus for on each day. We have to find the lowest cost it takes to travel for all the days. There are 3 types of bus tickets. 1-day pass for 2 bucks 7-day pass for 7 bucks 30-day pass for 25 bucksSo, if the input is like days = [1, 3, 5, 6, 28], then the output will be 9, as the lowest cost can be achieved by purchasing a 7-day pass in the beginning and then a 1-day pass on the 29th day.To solve ... Read More

Program to count number of swaps required to change one list to another in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:07:36

435 Views

Suppose we have two lists of numbers L1 and L2, the length of each list is n and each value is unique to its list, and values are in range 1 to n, we have to find the minimum number of adjacent swaps required to transform L1 to L2.So, if the input is like L1 = [0, 1, 2, 3] L2 = [2, 0, 1, 3], then the output will be 2, as we can swap 1 and 2, L1 will be [0, 2, 1, 3], and then 0 and 2, L1 will be [2, 0, 1, 3], this is ... Read More

Program to find maximum profit we can get by buying and selling stocks with a fee in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:05:39

204 Views

Suppose we have a list of stock prices of a company in chronological sequence, and also have the transaction fee for one sell transaction. We have to find the maximum profit we could have made from buying and selling that stock any number of times. We must buy before we can sell it.So, if the input is like prices = [2, 10, 4, 8] fee = 3, then the output will be 6, as we can buy at 2 and sell at 10 and incur a fee of 3, so profit is 5. Then we buy at 4 and sell ... Read More

Program to find maximum number enemies will be killed to place a bomb in C++?

Arnab Chakraborty
Updated on 10-Nov-2020 08:03:05

470 Views

Suppose we have a 2D matrix of three different values, 2s, 1s, and 0s, where a 2 represents an enemy, 1 represents a wall and 0 represents an empty cell. We have to find the maximum enemies we can kill using one bomb. The bomb kills all the enemies in the same row and column from the planted point until it hits the wall. And we can put bombs only on blank spaces.So, if the input is likethen the output will be 3, as we can place the bomb at the green box to kill maximum 3 enemies.ret := 0n ... Read More

Program to find number of places are safe when bomb explodes in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 07:58:13

192 Views

Suppose we have a 2d binary matrix, where 1 represents a bomb and 0 represents an empty cell. When a bomb explodes, all the spaces along on the same row and column are damaged. We have to find the number of spaces we can stand in to not get damaged.So, if the input is like110000000then the output will be 2, as there are two spaces the bottom right cell and the middle right cell are safe.To solve this, we will follow these steps:r := a list of size same as row count of matrix and fill with falsec := a ... Read More

Program to find minimum possible sum by changing 0s to 1s k times from a list of numbers in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 07:55:14

138 Views

Suppose we have a list of numbers called nums and another value k. We have to following operation k times: Select any number on the list. In the binary representation of that number, select a bit that is 0 and make it 1. Finally, we have to return the minimum possible sum of all the numbers after performing k operations. If the answer is too high, return result mode 10^9+7.So, if the input is like nums = [4, 7, 3] k = 2, then the output will be 17, as the binary representation of 4 is 100, 3 is 011, ... Read More

Program to find starting index of the child who receives last balloon in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 07:50:58

105 Views

Suppose we have n children standing in a circle, and they are waiting to get a balloon. The distribution is carried out starting with the kth child (first at index 0), and giving them a balloon they left the circle. Now every kth child gets a balloon going clockwise until there is only one child left that gets a balloon. So if we have n and k, we have to find the starting index of the child that receives the last balloon.So, if the input is like n = 3 k = 2, then the output will be 1, in ... Read More

Program to convert binary search tree to a singly linked list in C++?

Farhan Muhamed
Updated on 19-Aug-2025 17:27:40

655 Views

A Binary Search Tree is a data structure that stores data in a sorted order such that for every node, the left subtree contains values less than the node's value, and the right subtree contains values greater than the node's value. In this article, we will discuss how to convert a Binary Search Tree (BST) into a singly linked list in C++. Flatten Binary Search Tree to Singly Linked List Given a Binary Search Tree, the goal is to convert it into a singly linked list where the linked list nodes are in the same order as the in-order ... Read More

Program to find higher number with same number of set bits as n in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 07:40:38

193 Views

Suppose we have a number n; we have to find the smallest next higher number with the same number of 1s as n in binary form.So, if the input is like n = 7, then the output will be 11, as 7 in binary is 0111 and next higher from 7 with three ones would be 11 which is 1011 in binary.To solve this, we will follow these steps:copy := n, zeros := 0, ones := 0while copy is not 0 and copy is even, dozeros := zeros + 1copy = copy / 2while copy is odd, doones := ones ... Read More

Advertisements