Find Expected Sum of Subarrays in Python

Arnab Chakraborty
Updated on 11-Oct-2021 09:30:51

372 Views

Program to find expected sum of subarrays of a given array by performing some operationsSuppose we have an array A whose size is n and two values p and q. We can perform these operations on A.Randomly select two indexes (l, r) where l < r, then exchange A[l] and A[r]Randomly select two indexes (l, r) where l < r, then reverse subarray of A form index l to r.After performing first operation p number of times and the second operation q times, we randomly select two indices l & r where l < r and calculate the S = ... Read More

Display All Second Lowest Grade Students from Nested List in Python

Arnab Chakraborty
Updated on 11-Oct-2021 09:28:02

2K+ Views

Suppose we have the names and grades for each student in a nested list we have to display the names of any students having the second lowest grade. If there are more than one students with the second lowest grade, reorder these in an alphabetical order and print each name on a new line.So, if the input is like students = [['Amal', 37], ['Bimal', 37], ['Tarun', 36], ['Akash', 41], ['Himadri', 39]], then the output will be Amal, Bimal both have second least score 37, they are placed in alphabetic order.To solve this, we will follow these steps −min_mark := minimum ... Read More

Python Program to Find Runner-Up Score

Arnab Chakraborty
Updated on 11-Oct-2021 09:21:47

2K+ Views

Suppose we have a list of scores for different number of participants. We have to find the runner-up score.So, if the input is like scores = [5, 8, 2, 6, 8, 5, 8, 7], then the output will be 7 because the winner score is 8 and second largest score is 7.To solve this, we will follow these steps −winner := -99999runner_up := -99999for each i in scores, doif i > winner, thenwinner := irunner_up := winnerotherwise when i < winner and i > runner_up, thenrunner_up := ireturn runner_upExampleLet us see the following implementation to get better understandingdef solve(scores): ... Read More

Find Expected Value of Maximum Occurred Frequency Values in Python

Arnab Chakraborty
Updated on 11-Oct-2021 09:20:44

157 Views

Suppose we have M different expressions, and the answers of these expressions are in range 1 to N (both inclusive) So consider x = max(f(i)) for each i in range 1 through N, we have to find the expected value of x.So, if the input is like M = 3, N = 3, then the output will be 2.2, becauseSequenceMaximum frequency1113112211321222123113312223223223323333$$E(x) = \sum P(x) * x = P(1) + 2P(2) + 3P(3) = \frac{1}{10} + 2 * \frac{6}{10} + 3 * \frac{3}{10} = \frac{22}{10}$$To solve this, we will follow these steps −combination := a new mapDefine a function nCr() . ... Read More

Find Palindromic Borders in a String Using Python

Arnab Chakraborty
Updated on 11-Oct-2021 09:18:52

299 Views

Suppose we are provided with a string str. A border of a string is a substring that is a proper prefix and a suffix of that string. For example, 'ab' is a border of the string 'ababab'. A border is called a palindrome border if the border string is a palindrome. Now suppose there is f(str) number of palindrome borders in the given string str. We have to find out the sum of f(str_k) for all non-empty substrings str_k of str. The sum can be large, so a modulo operation can be performed by 10^9 + 7.So, if the input ... Read More

Find Substrings at Given Positions in Python

Arnab Chakraborty
Updated on 11-Oct-2021 09:08:44

189 Views

Suppose we are provided n number of strings; str1, str2, str3, ....., strn. Now, let's suppose that substri is a set that contains all the substrings of stri. The union of all the substr sets is substr_union. We now are given q number of queries, and we have to find the q-th element of the set substr_union. The set substr_union is lexicographically sorted and the indexes start from 1.So, if the input is like list of strings are = ['pqr', 'pqt'], queries are = [4, 7, 9], then the output will be ['pqt', 'qt', 't']The substrings from the first string ... Read More

Count Isosceles Triangles from Colored Vertex Regular Polygon in Python

Arnab Chakraborty
Updated on 11-Oct-2021 09:05:19

329 Views

Suppose we have one regular polygon with n sides represented as a binary string of size n. The vertices can be colored either in blue (0) or in red (1). They are colored in clockwise direction We have to count number of isosceles triangles whose vertices are vertices of the regular polygon, and their colors are same.So, if the input is like polygon = "111010", then the output will be 2 becausethere are two triangles ACE and AFE.To solve this, we will follow these steps −Define a function all() . This will take nif n mod 2 is same as ... Read More

Find Indexes of Substring Matches in Python

Arnab Chakraborty
Updated on 11-Oct-2021 08:59:46

243 Views

Suppose, we are provided with two strings. The first one has a length greater than the second one, and we have to check if the substrings from the first string match exactly with the second string or differ in one position. We return the indexes of the first string, where the substrings that can match with the second string start.So, if the input is like string1 = 'tpoint', string2 = 'pi', then the output will be 1 2.The substring from the first string that matches with the second string or differs in one position at index 1 and 2 are ... Read More

Find Maximum Possible Value of an Expression in Python

Arnab Chakraborty
Updated on 11-Oct-2021 08:50:15

369 Views

Suppose we have two arrays called nums1 and nums2, they have same number of elements N. Now consider a set S with N elements from 1 through N. We have to find the value of (nums1[i1] + nums1[i2] + ... nums1[ik])^2 + (nums2[i1] + nums2[i2] + ... nums2[ik])^2 where {i1, i2, ... ik} is non empty subset of the set S.So, if the input is like nums1 = [-1, 6] nums2 = [5, 4], then the output will be 106 because(-1)^2 + (5)^2 = 26(6)^2 + (4)^2 = 50(-1 + 6)^2 + (5 + 4)^2 = 106To solve this, we ... Read More

Find Similarity Between a String and Its Suffixes in Python

Arnab Chakraborty
Updated on 11-Oct-2021 08:47:00

189 Views

Suppose, we are given a string 'input_str'. If we determine all the suffixes from input_str; for example if the string is 'abcd', the suffixes are 'abc', 'bcd', 'cd', 'd'. Now, we check the similarity between input_str and all the suffixes by the length of the longest common prefix in input_str and a suffix. The sum of the similarities between input_str and all the suffixes has to be returned.So, if the input is like input_str = 'tpotp', then the output will be 7All the suffixes from the string 'tpotp' are 'tpotp', 'potp', 'otp', 'tp', and 'p'.If we check the similarity of ... Read More

Advertisements