title-img


Collections.namedtuple() python

collections.namedtuple() Basically, namedtuples are easy to create, lightweight object types. They turn tuples into convenient containers for simple tasks. With namedtuples, you don’t have to use integer indices for accessing members of a tuple. Example Code 01 >>> from collections import namedtuple >>> Point = namedtuple('Point','x,y') >>> pt1 = Point(1,2) >>> pt2 = Point(3,4) >>> dot_product = ( pt1.x * pt2.x ) +( pt1.y * pt2.y ) >>> print dot_product 11 Code 02 >>> from

View Solution →

Collections.OrderedDict() python

collections.OrderedDict An OrderedDict is a dictionary that remembers the order of the keys that were inserted first. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Example Code >>> from collections import OrderedDict >>> >>> ordinary_dictionary = {} >>> ordinary_dictionary['a'] = 1 >>> ordinary_dictionary['b'] = 2 >>> ordinary_dictionary['c'] = 3 >>> ordinary_dictionary['d'] = 4 >>> ordinary_dictionary['e'] = 5 >>> >>> print

View Solution →

Word Order Python

You are given n words. Some words may repeat. For each word, output its number of occurrences. The output order should correspond with the input order of appearance of the word. See the sample input/output for clarification. Note: Each input line ends with a "\n" character. Constraints: 1<=n<=10^5 The sum of the lengths of all the words do not exceed 10^6 All the words are composed of lowercase English letters only. Input Format The first line contains the integer, n. The n

View Solution →

Collections.deque() python

collections.deque() A deque is a double-ended queue. It can be used to add or remove elements from both ends. Deques support thread safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction. Click on the link to learn more about deque() methods. Click on the link to learn more about various approaches to working with deques: Deque Recipes. Example Code >>> from collections import deque >>> d = deque()

View Solution →

Company Logo Python

A newly opened multinational brand has decided to base their company logo on the three most common characters in the company name. They are now trying out various combinations of company names and logos based on this condition. Given a string s, which is the company name in lowercase letters, your task is to find the top three most common characters in the string. Print the three most common characters along with their occurrence count. Sort in descending order of occurrence count. If the o

View Solution →