Algorithm for solving problems

Everyday we encounter or face different kinds of problems. Each problem has got it’s own scope and field. Some of them you might solve quite easily but some of them might be difficult to solve at that moment. It might take a while before it gets solved. But every problem has got a solution.

There might be times where you get stuck in a problem for hours and suddenly you lose the confidence of solving it. It’s like you fall into a hole and cannot come out of it. You lose the sense of direction and approach in terms of problem solving. Fortunately there is a way to prevent or at least reduce the chances of this situation occurring.

Computer Science has an approach for solving computational problems. But this approach can be applied across different fields and to your life as well. It is basically like this, if the problem is big and appears difficult to solve, then divide that problem into smaller sub parts/ problems and see whether it is solvable. If you don’t think you can solve it, continue to divide it until you feel that you can solve the problem.

Then in terms of solving the problem one follows a reverse approach. Solve each sub part and then combine the solutions which would solve a bigger problem. Continue this approach until you finally get the solution to the actual “main” problem.

This approach even though is quite prevalent and used in different areas of computer science, is quite extensively used in functional programming. Here the whole code is written with different functions, each function solving a sub part of the problem. Some of the functional programming languages are Lisp, Python and Perl.

Advertisements

Python Intro

Python is quite a robust and dynamic language. It is very useful in writing scripts. Like any other scripting language, it can be used to automate certain tasks.

In most languages the file extension s start with the first letter of the word or sometimes the whole word. For example, the file extension for the C language is .c , for c++ it is .cpp and for java it is .java.  Python uses a similar naming convention for it’s extension. It uses the first two letters of the word and hence it is named as .py .

Python unlike other programming languages do not use braces to denote a block. Blocks are mainly represented using indentations. And one has to be very careful in using them. Since in python if a set of statements have to be defined in a block each line of that block should have the same amount of spacing.

Asynchronous Programming

Asynchronous Programming is another concept which is slowly being used in the desktop and web applications. Have you ever experienced a situation where a web application or a desktop application is slow to respond once you request for something? I bet many of us, might have faced this situation currently or in the past.

This might be because these applications might have not adopted the Asynchronous programming approach.  Let’s take an example  of an Application which does not follow the Asynchronous Programming approach.

Suppose, we have a desktop application which obtains it’s content from the web. And for some reason during some particular day, the internet server is down or it is too slow. Due to this, the whole application will stop functioning and we will not be able to use the other aspects of the application. So, just because one resource fails to execute properly, the whole application stops working. This is not a pleasant situation in case we want the users to continue using our applications.

With Asynchronous Programming, this situation can be handled quite effectively. We can simultaneously run other other parts of the application, in case some aspects stop functioning temporarily.

Let’s take another example. Microsoft Excel is a common application which runs in the Windows Platform and there are similar applications which run on other Operating Systems as well. Many of us have used it.

Now let’s say we have created a spreadsheet regarding Contact Names and Telephone Numbers. And then suppose we want to search the Name of a person. Sometimes even though you are sure the Name is present, it would return a prompt saying that the Name is not found. In this case the search feature of Excel is currently not working. But, we would still be able to add new rows or columns and continue working with our application.

 

C# has recently added a new technique for Asynchronous Programming, to make it easier to adopt this feature.They have added two new keywords Async and Await which in a sense saves time in making the application Asynchronous.

 

 

Type Safety

This is another very important topic in the context of coding. Programming Languages can be classified based on their type safety. Basically you can have two kinds of type safety-

  1. Strongly Typed
  2. Weakly Typed

Some Languages fall into the Strongly Typed Category and some of them fall into the Weakly Typed Category.  Let’s see what to they mean-

A Programming Language is said to be strongly typed if it forces us to define a datatype for each variable. By now, you might be knowing what datatypes are. Some of the common datatypes are int, char, boolean, float etc. It basically defines the type of the data. Whether the data is a number, character or a string.

For Example-

56

This is  a whole number. And so we need to define it as an integer. In order, to use this value we have to store it in a container known as a variable.

Let’s name the variable as “apple”.

We store the value in this variable. And since the contents of the variable is a whole number, we define the variable to be an Integer.

So, we have to write something like this-

int apple=56;

Capture.PNG

We have a pink box (known as a variable) named Apple. And in that box, we store a value called 56.

 

A Strongly typed Language forces us to define the type of a variable. In this case, had we been coding in a strongly typed language, we would have written int apple=56 .

 

Whereas in a weakly typed language, we are not forced to define the type of the variable. We could have simply written apple=56; and it would have worked!

And In a weakly typed language we can mix two unrelated types. By “Unrelated” what I mean is that an Integer and a Character or an Integer and a Boolean Value. Related Types could be Integers and Decimals.

So, in a weakly typed language, we could add a number and a character and it would allow us to do it.

Example

$apple=45;

$ch=”and”;

$next=$apple.$ch;

print $next;

 

This is what we would have written in Perl, which is a weakly typed language. In Perl, the variables are prefixed with a dollar sign. So, we have a variable named apple which stores a whole number 45. And we have another variable named ch which stores a string named “and”. In the third line, we try to combine the two unrelated types. And finally, we try to print that result.

And the result is??

It would be  45and

 

So, this essentially demonstrates that weakly typed languages have no problem mixing unrelated types.

Some of the strongly typed languages are- Java, C# and a  weakly type Language would be Perl.

 

UnBoxing

Today’s topic is in complete contrast to the one I talked about yesterday. Unboxing literally means taking something out of the box. You must have done this quite often! Like unwrapping a gift, opening up a package which you placed an order for.

This is exactly how it works in programming languages. As I had explained earlier, Boxing means converting a value type to a reference type and placing it in the heap portion of the memory.

UnBoxing is just the opposite. Here, The reference type is converted to a value type and it is placed in the stack portion of the memory. The element is unwrapped from the object and it is converted to a value type.

Let’s take an example

Suppose we have a reference type like this-

object o=56;

In order to convert to a value type we have to use the casting operator, something which I would explain in my forthcoming blogs.

int i=(int)o;

So, this basically means we are trying to cast the object to an integer by using the casting operator (int).

So, now you could write-

Console.WriteLine(” “+i);  In C#

or

System.println(”  “+i);  In Java

or

Cout<<” “<<i; In C++

 

All the statements which I listed above do the same thing. They print a value. The only thing is that the syntax is different. Once you try executing any of the statements in the respective languages. The output would be the same, which is 56. And it shows that we have successfully unboxed the element from the heap.

 

 

Boxing

Today we are going to talking about a term known as Boxing. The first thing which would strike your mind once you hear this word is obviously the boxing game. But, what we are going to discuss today has got nothing to do with that.

So, Boxing in programming terminology means, converting a value type to a reference type. I had  explained what Value Type and Reference Type is in my previous blogs. The Value Type is stored in the stack portion of the memory. And once it is converted to a reference type, it is stored in the Heap Portion of the memory.

Let’s see how we can perform boxing on a Value Type. And let’s say we have a value type which is an Integer.

int i=25;

Now, in order to convert this Integer to a reference type, we would need an object.

object 0=i;

And now, the integer is wrapped in an object which can be similar to placing it in the Box and hence the name! This box is now placed in the heap portion of the memory.

 

 

Generics-2

Hi Everyone! In my previous blog I gave you a brief insight of Generics. Today, we will go a little deeper into the topic.

So, the first question which might arise in everyone’s mind is why we need Generics. Let’s see the reasons-

Need of Generics

Some of the common reasons someone would use Generics would be-

  1. It helps to reuse the code.
  2. Helps in Code Maintenance.
  3. It makes us write lesser code.
  4. It improves the efficiency of the program.

 

Let’s say if a person wants to write an algorithm to add two numbers. And assuming that guy doesn’t know generics. He would write two separate functions for adding two numbers. One would be using Integers (Whole Numbers) and the other one would be Floating point numbers (Decimals).

Something like this-

public int Add1 (int a, int b){

int c= a+b;

return c;

}

public float Add2(float a, float b){

float c=a+b;

return c;

}

 

So, the first function named “Add1” is to add two Integers (Whole Numbers) and the second function “Add2”, is to add two floating point numbers (Decimals).

Now, let’s see what would the person have written, had he known Generics

<T> Add(T a, T b){

T c=a+b;

}

 

He would have just written that, and it would have worked for both Integers and Floating Point Numbers. This reduces the number of lines of code drastically and hence improves the efficiency of the program.