Posts

Showing posts with the label Problem Solving

How to think like a programmer — Lessons in Problem Solving

Image
If you’re interested in programming, you may well have seen this quote before: “Everyone in this country should learn to program a computer, because it teaches you to think.” — Steve Jobs You probably also wondered what does it mean, exactly, to think like a programmer? And  how  do you do it?? Essentially,   it’s all about  a more effective way for problem solving . In this post, my goal is to teach you that way. By the end of it, you’ll know exactly what steps to take to be a better problem-solver. Why is this important? Problem solving is the meta-skill. We all have problems. Big and small. How we deal with them is sometimes, well…pretty random. Unless you have a system, this is probably how you “solve” problems (which is what I did when I started coding): Try a solution. If that doesn’t work, try another one. If that doesn’t work, repeat step 2 until you luck out. Look, sometimes you luck out. But that is the worst way to solve problems! And it’s a huge, huge was...

Why The Hell Would I Use Node.js?

Image
JavaScript’s rising popularity has brought with it a lot of changes, and the face of web development today is dramatically different. The things that we can do on the web nowadays with JavaScript running on the server, as well as in the browser, were hard to imagine just several years ago, or were encapsulated within sandboxed environments like Flash or Java Applets. Before digging into  Node.js solutions , you might want to read up on the benefits of using  JavaScript across the stack  which unifies the language and data format (JSON), allowing you to optimally reuse developer resources. As this is more a benefit of JavaScript than Node.js specifically, we won’t discuss it much here. But it’s a key advantage to incorporating Node in your stack. As Wikipedia states: “Node.js is a packaged compilation of Google’s V8 JavaScript engine, the libuv platform abstraction layer, and a core library, which is itself primarily written in JavaScript.” Beyond that, it’s worth noting t...

Matrix Chain Multiplication

Image
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications.  We have many options to multiply a chain of matrices because matrix multiplication is associative. In other words, no matter how we parenthesize the product, the result will be the same. For example, if we had four matrices A, B, C, and D, we would have:    (ABC)D = (AB)(CD) = A(BCD) = ....  However, the order in which we parenthesize the product affects the number of simple arithmetic operations needed to compute the product, or the efficiency. For example, suppose A is a 10 × 30 matrix, B is a 30 × 5 matrix, and C is a 5 × 60 matrix.  (AB)C = (10×30×5) + (10×5×60) = 1500 + 3000 = 4500 operations A(BC) = (30×5×60) + (10×30×60) = 9000 + 18000 = 27000 operations. Clearly the first parenthesization requires less number of operations. Given an a...

Tower of Hanoi

Image
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one rings is as depicted − Tower Of Hanoi These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits over the larger one. There are other variations of the puzzle where the number of disks increase, but the tower count remains the same. Rules The mission is to move all the disks to some another tower without violating the sequence of arrangement. A few rules to be followed for Tower of Hanoi are − Only one disk can be moved among the towers at any given time. Only the "top" disk can be removed. No large disk can sit over a small disk. Following is an animated representation of solving a Tower of Hanoi puzzle with three disks. Tower Of Hanoi Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps. This presentation shows that a puzzle with 3 disks has taken 23 - 1 = 7 steps. Algorithm To write an algorithm for Tower of H...

Breadth First Search (BFS)

Image
Breadth-first search assigns two values to each vertex v: A distance, giving the minimum number of edges in any path from the source vertex to vertex vvv. The predecessor vertex of vvv along some shortest path from the source vertex. The source vertex's predecessor is some special value, such as null, indicating that it has no predecessor. If there is no path from the source vertex to vertex vvv, then vvv's distance is infinite and its predecessor has the same special value as the source's predecessor. For example, here's an undirected graph with eight vertices, numbered 0 to 7, with vertex numbers appearing above or below the vertices. Inside each vertex are two numbers: its distance from the source, which is vertex 3, followed by its predecessor on a shortest path from vertex 3. A dash indicates null: In BFS, we initially set the distance and predecessor of each vertex to the special value (null). We start the search at the source and assign it a distance of 0. ...

Recursion

Image
What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc. A Mathematical Interpretation Let us consider a problem that a programmer have to determine the sum of first n natural numbers, there are several ways of doing that but the simplest approach is simply add the numbers starting from 1 to n. So the function simply looks like, approach(1) – Simply adding one by one f(n) = 1 + 2 + 3 +……..+ n but there is another mathematical approach of representing this, approach(2) – Recursive adding f(n) = 1 n=1 f(n) = n + f(n-1) n>1 There is a simple difference between the approach (1) and approach(2) and that is in approach(2) the function “ f( ) ” itself is being...

Understanding React and ReactDOM

Image
The first line — import React from 'react'; - brings in the React module from your React library. It also creates an Object called React which you can tap into pre-written methods. For example, createElement() is a method that belongs in the React object. createElement() is also how React renders things into HTML. When you're using JSX, JSX compiles and transforms your code into a React.createElement() call. Note: You can write React ‘code’ without JSX, but it means that you will need to format everything to fit with createElement() method requirements. The next line after import React from 'react'; is import ReactDOM from 'react-dom';. This line imports methods that are available from react-dom and makes it accessible through the Object named ReactDOM. When you are rendering your React component, you are doing it via ReactDOM. Note: the DOM is not something that is new or exclusive to React. It is part of HTML and lets you hook into different parts of ...

Java Programming Language

Image
Java is one of the most popular and widely used programming language. Java has been one of the most popular programming language for many years. Java is Object Oriented. However it is not considered as pure object oriented as it provides support for primitive data types (like int, char, etc) The Java codes are first compiled into byte code (machine independent code). Then the byte code runs on Java Virtual Machine (JVM) regardless of the underlying architecture. Java syntax is similar to C/C++. But Java does not provide low level programming functionalities like pointers. Also, Java codes are always written in the form of classes and objects. Java is used in all kind of applications like Mobile Applications (Android is Java based), desktop applications, web applications, client server applications, enterprise applications and many more. When compared with C++, Java codes are generally more maintainable because Java does not allow many things which may lead bad/inefficient program...