Found 26504 Articles for Server Side Programming

Hand of Straights in C++

Arnab Chakraborty
Updated on 05-May-2020 10:24:24

574 Views

Suppose Rima has a hand of cards, given as an array of integers. Now she wants to shuffle the cards into groups so that each group is size W, and consists of W consecutive cards. We have to check whether it is possible or not.So if the cards are [1, 2, 3, 6, 2, 3, 4, 7, 8], and W = 3, then the answer will be true, as she can rearrange them like [1, 2, 3], [2, 3, 4], [6, 7, 8]To solve this, we will follow these steps −Define a map m, and store frequency of each element ... Read More

Finding frequency in list of tuples in Python

Pradeep Elance
Updated on 05-May-2020 10:22:42

537 Views

Many different types of data container can get mixed up in python. A list can have elements each of which is a tuple. In this article we will take such a list and find the frequency of element in the tuples which are themselves elements of a list.Using count and mapWe apply a lambda function to count through each of the first element in the tuples present in the list. Then apply a map function to arrive at the total count of the element we are searching for.Example Live Demo# initializing list of tuples listA = [('Apple', 'Mon'), ('Banana', 'Tue'), ('Apple', ... Read More

Find sum of frequency of given elements in the list in Python

Pradeep Elance
Updated on 05-May-2020 10:21:16

501 Views

A given list has many repeated items. We are interested in finding out the sum of the frequency of some such items which are repeated in the list. Below are the approaches how we can achieve this.With sumWe have two lists. One has the list of values and other has the values whose frequency needs to be checked from the first list. So we create a for loop to count the number of occurrences of the elements from the second list in the first list and then apply the sum function to get the final sum of frequency.Example Live Demochk_list= ['Mon', ... Read More

Longest Mountain in Array in C++

Arnab Chakraborty
Updated on 05-May-2020 10:22:05

543 Views

Consider any (contiguous) subarray B (of A) a called mountain if the following properties hold −size of B >= 3There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1]Suppose we have an array A of integers; we have to find the length of the longest mountain. We have to return 0 if there is no mountain. So if the input is like [2, 1, 4, 7, 3, 2, 5], then the result will be 5. So the largest mountain will be [1, ... Read More

Find frequency of given character at every position in list of lists in Python

Pradeep Elance
Updated on 05-May-2020 10:19:58

264 Views

Lets consider a scenario where you have a list which is made of lists as its elements. We are interested in finding the frequency of one character at different positions of the inner lists. Below example will clarify the requirement.Consider a list of lists given below.listA = [['a', 'a', 'b'], ['a', 'c', 'b'], ['c', 'a', 'b'], ['c', 'a', 'a']]In the abobe list we have elements which are lists with 3 elements. If I consider the first inner list tit has a, a, b at positions 0, 1, 2. Similarly for the 3rd list it is c, a, b at 0, ... Read More

Find elements of a list by indices in Python

Pradeep Elance
Updated on 05-May-2020 10:17:19

6K+ Views

Consider two lists. The elements in the second list are numbers which needs to be considered as index position for elements of the first list. For this scenario we have the below python programs.With map and getitemWe can use the getitem magic method is used to access the list items. We can use it along with the map function, so that we get the result from first list which takes the elements from second list as its indics.Example Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] listB = [0, 1, 3] print("Given list A:", listA) print("Given list B:", listB) ... Read More

New 21 Game in C++

Arnab Chakraborty
Updated on 05-May-2020 10:19:03

385 Views

Suppose Rima plays the following game, that is loosely based on the card game "21". So Rima starts with 0 points, and draws numbers while she has less than K points. Now, during each draw, she gains an integer number of points randomly from the range [1, W], where W is given, and that is an integer. Now each draw is independent and the outcomes have equal probabilities. Rima stops drawing numbers when she gets K or more points. We have to find the probability that she has N or less points?So if N = 6, K is 1 and ... Read More

Extract only characters from given string in Python

Pradeep Elance
Updated on 05-May-2020 10:16:10

3K+ Views

A piece of data may contain letters, numbers as well as special characters. If we are interested in extracting only the letters form this string of data, then we can use various options available in python.With isalphaThe isalpha function will check if the given character is an alphabet or not. We will use this inside a for loop which will fetch each character from the given string and check if it is an alphabet. The join method will capture only the valid characters into the result.Example Live DemostringA = "Qwer34^&t%y" # Given string print("Given string : ", stringA) # ... Read More

Extract numbers from list of strings in Python

Pradeep Elance
Updated on 05-May-2020 10:14:34

1K+ Views

While using python for data manipulation, we may come across lists whose elements are a mix of letters and numbers with a fixed pattern. In this article we will see how to separate the numbers form letters which can be used for future calculations.With splitThe split functions splits a string by help of a character that is treated as a separator. In the program below the list elements have hyphen as their separator between letters and text. We will use that along with a for loop to capture eachExample Live DemolistA = ['Mon-2', 'Wed-8', 'Thu-2', 'Fri-7'] # Given list print("Given ... Read More

Find And Replace in String in C++

Arnab Chakraborty
Updated on 05-May-2020 10:15:24

2K+ Views

Suppose we have a string S, we will perform some replacement operations that replace groups of letters with new ones. In each replacement operation there are 3 parameters − a starting index i, a source word x and a target word y. Now the rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. otherwise, we do nothing.So as an example, consider, if we have S = "abcd" and we have some replacement operation i = 2, x = "cd", y = "ffff", then because "cd" ... Read More

Advertisements