title-img


Compress the String! python

In this task, we would like for you to appreciate the usefulness of the groupby() function of itertools . To read more about this function, Check this out . You are given a string S. Suppose a character 'c' occurs consecutively X times in the string. Replace these consecutive occurrences of the character 'c' with (X,c) in the string. For a better understanding of the problem, check the explanation. Input Format A single line of input consisting of the string S. Output Format

View Solution →

Iterables and Iterators python

The itertools module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an iterator algebra making it possible to construct specialized tools succinctly and efficiently in pure Python. To read more about the functions in this module, check out their documentation here. You are given a list of N lowercase English letters. For a given integer k, you can select any k indices (assume 1-based indexing) with a uniform prob

View Solution →

Maximize It! python

You are given a function f(X)=X^2. You are also given K lists. The ith list consists of Ni elements. You have to pick one element from each list so that the value from the equation below is maximized: S=(f(X1) + f(X2) +....+ f(Xk)%M Xi denotes the element picked from the ith list . Find the maximized value Smax obtained. % denotes the modulo operator. Note that you need to take exactly one element from each list, not necessarily the largest element. You add the squares of the chosen

View Solution →

collections.Counter() python

collections.Counter() A counter is a container that stores elements as dictionary keys, and their counts are stored as dictionary values. Sample Code >>> from collections import Counter >>> >>> myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3] >>> print Counter(myList) Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1}) >>> >>> print Counter(myList).items() [(1, 3), (2, 4), (3, 4), (4, 2), (5, 1)] >>> >>> print Counter(myList).keys() [1, 2, 3, 4, 5] >>> >>> print Counter(myList).values() [3, 4,

View Solution →

DefaultDict Tutorial python

The defaultdict tool is a container in the collections class of Python. It's similar to the usual dictionary (dict) container, but the only difference is that a defaultdict will have a default value if that key has not been set yet. If you didn't use a defaultdict you'd have to check to see if that key exists, and if it doesn't, set it to what you want. For example: from collections import defaultdict d = defaultdict(list) d['python'].append("awesome") d['something-else'].append("not

View Solution →