Skip to main content

Posts

Showing posts with the label Programming

Lambda, Map, and Filter in Python

Lambda: Lambda functions are known as the anonymous functions in Python. This kind of functions are declared without any name. Can accept any number of argument.  Restricted to only a single expression.  Used only one-time. Lambda function is used when you need a function for a short period of time.  Basic syntax:  lambda arguments : expression Example: Map: Map is a built-in function that takes a function object and iterable list (list/dictionary) as its parameter. Basic syntax: map (function_object, iterable_1, iterable_2, ... ) Few examples of Map in python: Filter: It works similar way like map. The difference is it makes a new list from the element in the list that satisfy some condition . In case of of parameters, it takes a function and one iterable. Basic syntax: filter (function, iterable) Normally used with lambda functions. Example is given below:

Different Types of Programming Paradigm

Programming paradigm mainly describes the way in which a particular computer program should be written. According to Wikipedia, it is fundamental style of computer programming. Basically, different programming languages are designed to follow a particular paradigm or may be multiple paradigms. Imperative(procedural) Programming Paradigm:   -- Imperative programming is a programming paradigm that uses statements that change a program's   state.   -- In much the same way that the imperative mood in natural languages expresses commands.   -- An imperative program consists of commands for the computer to perform.   -- Imperative programs describe the details of HOW the results are to be obtained. --  HOW means describing the Inputs and describing how the Outputs are produced.     Examples are: C, C++, Java, PHP, Python, Ruby etc. Popular programming languages are imperative more often than they are any other paradigm . Ther...

Exception Handling & Event Handling

  Q. Define exception , exception handler , and built-in exception.   Exception : An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. Any unusual event, erroneous or not, that is detectable by either hardware or software and that may require special processing is called exception. Exception Handler: An exception handler processes a raised exception. The exception can be either predefined or user-defined. Predefined exceptions are raised implicitly (automatically) by the run-time system.   Built-in Exception : Built-in exceptions are the exceptions which are available in libraries. These exceptions are suitable to explain certain error situations. Below is the list of important built-in exceptions in Java. Arithmetic Exception ArrayIndexOutOfBoundException ClassNotFoundException FileNotFoundException IOException. Q. What are the advantages of having support...

What is Code refactoring?

Refactoring is "the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure. If we are making a refactoring, or behavior-preserving code modification: all our unit tests should pass before and after the modification we should not need to modify any tests, or write any new ones we expect cleaner code when we are done we do not expect new behavior

The Way Recursion Works

Recursive function is the function that, as part of its execution, it invokes itself. For example, if we at a function fact(n) that does factorial of number n.    fact(1) = 1    fact(2) =2 * 1    fact(3) =3 * 2 * 1    fact(4) =4 * 3 * 2 * 1    fact(5) =5 * 4 * 3 * 2 * 1    ...... We can define the above lines like this:    fact(1) = 1    fact(2) =2 * fact(1)    fact(3) =3 * fact(2)    fact(4) =4 * fact(3)    fact(5) =5 * fact(4)     ........  So, we can say that,   fact(n) = n * fact(n-1)    is the formula to find the factorial of any number. Every recursive function has two parts: Base Case: It terminates the recursive process. Recursive Case: Where the recursion actually occurs. In other words, where it calls the function itself. From this case, the program keeps running, means it keeps calling the function. To te...

Asymptotic Analysis of Algorithms

  In Asymptotic analysis, We evaluate the performance of an algorithm by the input size, not by the actual running time. For example, if we want to search an item from an sorted array, we can do this by two ways: Linear Search - Takes linear time. Binary Search -Takes logarithmic time Now let's assume we are running the linear search in a fast computer and the binary search in a slow computer . For small inputs, the fast computer may take less time to find the item, but as the input size increases and for a larger input the slow computer with binary search will take less time to find the item. The reason is that binary search takes logarithmic time. So, the machine dependency can be ignored after a certain size of inputs. So, is it always gives perfect result? No. But that's the best way available to evaluate an algorithm. Let's say, two algorithms take 2000nLogn 5nLogn In asymptotic analysis, we always ignore constants. So, both of the above becomes: nL...