Server Side Programming Articles - Page 1758 of 2650

Find most frequent element in a list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:44:36

8K+ Views

In this article we will see how to find the element which is most common in a given list. In other words, the element with highest frequency.With max and countWe apply why the set function to get the unique elements of the list and then keep account of each of those elements in the list. Finally apply a max function to get the element with highest frequency.Example Live Demo# Given list listA = [45, 20, 11, 50, 17, 45, 50, 13, 45] print("Given List:", listA) res = max(set(listA), key = listA.count) print("Element with highest frequency:", res)OutputRunning the above code gives us ... Read More

Find most common element in a 2D list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:42:24

687 Views

A 2D list has list as its element. In other words it is a list of lists. In this article we are required to find the element which is most common among all the lists inside a list.With max and countWe design a follow with a in condition to check for the presence of an element in a given sublist. Then we apply the max function along with the count function to get the the element with maximum frequency.Example Live Demodef highest_freq(lst):    SimpleList = [el for sublist in lst for el in sublist]    return max( SimpleList, key= SimpleList.count) # ... Read More

Tiling a Rectangle with the Fewest Squares in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:46:08

328 Views

Suppose we have a rectangle of size n x m. We have to find the minimum number of integers sided square objects that can tile the rectangles.So, if the input is like n = 2 and m = 3,then the output will be 3, as we need three blocks.To solve this, we will follow these steps −Define one map mres := infDefine a function dfs(), this will take n, m, an array h, cnt,if cnt >= res, then −returnisFull := truepos := -1, minH := inffor initialize i := 1, when i

Find maximum value in each sublist in Python

Pradeep Elance
Updated on 04-Jun-2020 11:40:56

398 Views

We are given a list of lists. In the inner lists or sublists we are required to find the maximum value in each.With max and inWe design a for loop with in condition and apply the max function to get the maximum value in each of the sublist.Example Live DemoAlist = [[10, 13, 454, 66, 44], [10, 8, 7, 23]] # Given list print("The given list: ", Alist) # Use Max res = [max(elem) for elem in Alist] # Printing max print("Maximum values from each element in the list: ", res)OutputRunning the above code gives us the following result −The given ... Read More

Maximum Profit in Job Scheduling in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:41:34

1K+ Views

Suppose we have n different tasks, where every task is scheduled to be done from startTime[i] to endTime[i], for that task we algo get profit of profit[i]. We know the startTime , endTime and profit lists, we have to find the maximum profit we can take such that there are no 2 tasks in the subset with overlapping time range. If we choose a task that ends at time X we will be able to start another task that starts at time X.So, if the input is like startTime = [1, 2, 3, 3], endTime = [3, 4, 5, 6] ... Read More

Count tuples occurrence in list of tuples in Python

Pradeep Elance
Updated on 04-Jun-2020 11:39:00

586 Views

A list is made up of tuples as its element. In this article we will count the number of unique tuples present in the list.With defaultdictWe treat the given list as a defaultdict data container and count the elements in it using the in condition.Example Live Demoimport collections Alist = [[('Mon', 'Wed')], [('Mon')], [('Tue')], [('Mon', 'Wed')] ] # Given list print("Given list:", Alist) res = collections.defaultdict(int) for elem in Alist:    res[elem[0]] += 1 print("Count of tuples present in the list:", res)OutputRunning the above code gives us the following result −Given list: [[('Mon', 'Wed')], ['Mon'], ['Tue'], [('Mon', 'Wed')]] Count of tuples ... Read More

Count the sublists containing given element in a list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:37:17

413 Views

The elements in a given list can also be present as another string in another variable. In this article we'll see how many times a given stream is present in a given list.With range and lenWe use the range and len function to keep track of the length of the list. Then use the in condition to find the number of times the string is present as an element in a list. A count variable initialized to zero keeps incrementing whenever the condition is met.Example Live DemoAlist = ['Mon', 'Wed', 'Mon', 'Tue', 'Thu'] Bstring = 'Mon' # Given list print("Given ... Read More

Maximum Equal Frequency in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:37:43

280 Views

Suppose we have an array nums of positive integers, we have to return the longest possible length of an array prefix of given array nums, such that it is possible to delete exactly one element from this prefix so that every number that has appeared in it will have the same frequency. After removing one element if there are no remaining elements, it's still considered that every appeared number has the same frequency (0).So, if the input is like [3, 3, 2, 2, 6, 4, 4, 6], then the output will be 7, So if we remove element 6 from ... Read More

Count the Number of matching characters in a pair of string in Python

Pradeep Elance
Updated on 04-Jun-2020 11:34:46

983 Views

We are given two strings. We need to find the count it of the characters in the first string which are also present in a second string.With setThe set function gives us unique values all the elements in a string. We also use the the & operator which finds the common elements between the two given strings.Example Live DemostrA = 'Tutorials Point' uniq_strA = set(strA) # Given String print("Given String", strA) strB = 'aeio' uniq_strB = set(strB) # Given String print("Search character strings", strB) common_chars = uniq_strA & uniq_strB print("Count of matching characters are : ", len(common_chars))OutputRunning the above code gives ... Read More

Count Vowels Permutation in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:34:36

294 Views

Suppose we have one number n, we have to count how many strings of length n can be formed using these rules − Each character is a lower case vowel Each vowel 'a' may only be followed by an 'e'. Each vowel 'e' may only be followed by an 'a' or 'i'. Each vowel 'i' may not be followed by another 'i'. Each vowel 'o' may only be followed by an 'i' or 'u'. Each vowel 'u' may only be followed by an 'a'. The answer may be too large, so we will find the answer modulo 10^9 + 7.So, ... Read More

Advertisements