Posts

What is a design pattern?

Design patterns : Design patterns represent the best practices Design patterns are solutions to general problems that software developers faced during software development. Gang of Four (GOF)? : In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book titled Design Patterns - Elements of Reusable Object-Oriented Software which initiated the concept of Design Pattern in Software development. These authors are collectively known as Gang of Four (GOF) According to these authors design patterns are primarily based on the following principles of object orientated design: Program to an interface not an implementation Favor object composition over inheritance Usage of Design Pattern : Common platform for developers Best Practices Types of Design Patterns : Creational Patterns : These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects

Explain map, filter, reduce and zip functions?

map : It is a built-in higher-order function that operates on iterable. Functions that can accept other functions as arguments and return function to the caller is known as Higher-order functions. It takes a function and an iterable as arguments and returns a new iterable with the function applied to each item of iterable. filter : It filters an iterable by removing items that don’t match a predicate. A predicate is a function that returns a Boolean. reduce : It continuously applies the function to sequence and returns a single value. zip : It takes two equal-length collections and merges them together in pairs. Example : nums = [ 11,22,33,44,55 ] list1  = [ 1,2,3  ] list2 = [ ‘a’,’b’,’c’ ] >> zip( list1 , list2 ) # [(1, 'a'), (2, 'b'), (3, 'c')] >> res = list( filter ( lambda x : x % 2 == 0 , nums ) ) >> print(res) # [ 22, 44 ] >> print(map(lambda x : x + 5 , nums)) >&

How to sort Dictionaries over values?

It is not possible to sort a dict, only to get a representation of a dict that is sorted. Dicts are inherently orderless, but other types, such as lists and tuples, are not. So we need a sorted representation, which will be a list—probably a list of tuples. Soln 1 : To sort on values : import operator x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} sorted_x = sorted(x.items(), key=operator.itemgetter(1)) sorted_x will be a list of tuples sorted by the second element in each tuple.dict(sorted_x) == x To sort on keys : import operator x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} sorted_x = sorted(x.items(), key=operator.itemgetter(0)) Soln 2 : x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} sorted_x = sorted( x.items(), key=lambda a : a[1] )

What can be key of a dictionary?

Key of a dictionary can be : String Numbers tuples List can’t be a key of a dictionary and also tuple of list can’t be same. If we assign a tuple of list as key then it will generate a TypeError: unhashable type: 'list'.

What is GIL (Global Interpreter Lock) in python?

Mutex : A mutex is a program object that allows multiple program threads to share the same resource, such as file access, but not simultaneously. When a program is started, a mutex is created with a unique name. GIL ( Global Interpreter Lock ) : A global interpreter lock (GIL) is a mechanism used in computer language interpreters to synchronize the execution of threads so that only one native thread can execute at a time. An interpreter that uses GIL always allows exactly one thread to execute at a time, even if run on a multi-core processor. C Python has a gil. GIL is a mutex that prevents multiple native threads from executing Python byte-codes at once. This lock is necessary because C Python's memory management is not thread-safe. The GIL is controversial because it prevents multi-threaded C Python programs from taking full advantage of multiprocessor systems in certain situations.

Difference between range and xrange

range vs xrange : range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements. xrange is a sequence object that evaluates lazily. xrange is not exactly a generator but it evaluates lazily and acts like a generator. In Python 3, range will be implemented by the Python 2 xrange(). If you need to actually generate the list, you will need to do: list(range(1,100))