Making Computer Science simple bit by bit

If you cant explain it simply, you dont understand it well.

Computer Science is often seen as an esoteric discipline. Most of the learners approach it in a convoluted fashion. You hear complex mathematical jargon thrown around in corridors.On top of that Pop culture left no stone unturned to depict Programming as a niche skill possessed by socially anxious (awkward) individuals.

However, I cant blame them entirely. The complexity of a subject depends on a variety of things like:

*Are you exposed to similar branches of knowledge?**What do you want to achieve by studying it?**How much time & interest do you have?**How do you plan to approach the subject?*

Often learners skip the part where they honestly answer these questions.This leads to all kinds of problems.It ultimately results in their dry outlook towards a rather succulent and creative branch of knowledge.

Being a Computer Science graduate, I can assure you, things are not as difficult as they look like. Sure, they require novel thinking and good problem-solving skills. But like I said, these are skills and can be developed.

Therefore today, I am starting a series of articles that will take a programming problem and deconstruct it into intuitive puzzles.

You will see, how typical problems can be solved without any complex mumbo-jumbo if we ask ourselves the right questions and approach them systematically.

*In the coming series of essays, we will follow a consistent structure:*

**The Problem****The Strategy**ðŸ’¡**The Code ðŸŽ‰**

We will start with a detailed problem statement and gradually proceed towards the solution by building upon the *existing knowledge* and *intuition*. Towards the end, we will code that solution away.

I am a big fan of ** community learning**.When you learn together, it leads to an understanding that is beyond the realm of an isolated study session.The best way to achieve this is to share your learnings with the community that helped you grow.

I strongly feel that these essays will rekindle your curiosity and soon you will see how fun problem solving is.

The journey of

thousandcommits begins with a singleinitðŸŽŠ

*Want more of this?*

*You can connect on Twitter here:**abshekha*

In Linux, if you check the file permissions you will notice the name of the file owner along with the group which owns the file. Note that, It is

**Imagine this scenario:**

There are 3 processes operating in a system:

P1: Runs asrootP2: Creates the directories during the install.P3: It access files created by P1.

Further, P2 & P3 are the part of the same group, say *user_grp* and both run as owner *user*.

Now consider a directory ** docs** which is created by our installer process P2 and its permission looks like this:

`drwxr-x--- 5 user user_grp 4096 Jul 31 19:56 docs`

P1 gets into the directory and wants to create a file called ** readme.txt** for process P3 to read.

Please note that there is an additional constraint here, the read, write & execute access for OTHERS is prohibited.

Nevertheless, P1 goes ahead and creates the file whose permission looks like this:

` -rwxr----- 1 root root 4096 Jul 31 19:56 readme.txt`

*Can you spot the problem here?*

After carefully examining the situation, it is not difficult to judge that Process P3 will fail in reading the file *readme.txt* because it is neither the owner of file *readme.txt* nor part of the same group.

There are a lot of ways one can go about solving this access limitation. For example, you can just issue *chgrp* for the file to change its group to *user_grp* so that process P3 can access, as it will then belong to the same group. But if there are a lot of files like this in the docs directory and you want P3 to read all of them, rather than firing *chgrp* for each file, you can opt for a more elegant approach.

Let's issue this command on ** docs** directory to set

`$ chmod g+s docs`

Look at the file permission of the ** docs** directory:

`drwxr-s--- 5 user user_grp 4096 Jul 31 19:56 docs`

Notice there is an ** 's'** in place of

`-rwxr----- 1 root user_grp 4096 Jul 31 19:56 readme2.txt`

This is the magic of SGID!

When you set the SGID on a directory, all the files and directories inside it will inherit the parent directory's group.

So even though ** root** is creating the file

Linux holds deeper truths, we just have to MAN [1] up to get there!

[1] I am not trying to label courage as a masculine trait. It is just that Linux manual is called MAN pages. So a *puny* pun ðŸ˜›

If you liked this post, you can find more by:

Following me on

Connecting on

Visiting my

website: https://abhimanyutimes.com

Thanks for reading!

]]>What if I ask you to calculate the square root of a number? Depending upon your background you are likely to come up with a variety of solutions. Let me talk about that specific fraction of you who will be interested in churning out code for doing so. *[No, sqrt()]*

*Binary Search* seems to be a valid and intuitive approach, after all, we can easily discard half of the search space in each iteration. But I want you to hold that thought and glance over this small snippet below.

`double sq(double num){ double xn = num/2; while(abs((xn*xn)-num)>eps) //eps is precision { xn = (xn + num/xn)/2; } return xn;}`

Magical isn't it? When I first looked at this, I was really startled by the elegance of this solution. So what is it and why does it work?

You just witnessed the glamour of ** Newton Raphson** method.

In simple terms, it is a way to quickly find a good approximation for the roots of a real-valued function

This is our cue, square root for a number can be minimally represented by solving for the roots of **f(x) = x^2- N**, where *N* is the number we have to find the root of.

That is all cool! But how does it actually work? Let's look at the simple math behind this:-

Consider a general function:

**y = f(x)**We start by choosing an arbitrary value**x1**as our starting point[1]. For our specific case, we can choose**N/2**as our starting point and can safely discard values greater than that from the candidature of**N**.Now let's draw a tangent (Differential) to the curve at that point and mark its intersection with the x-axis. Let's call this

**x2**.

Repeat

*step 2*for point**x2**to obtain**x3**. You must have noticed that with each iteration we are nearing the roots of the quadratic equation. Slow and steady!*Depending upon the precision, you can decide upon the number of iterations required.*

Let's try to workout an elegant formula for **f(x) = x^2 - N** which will get us the compact code we saw in the beginning. Please note that we are aiming to get a relation between **N** and **(N+1)** iteration.

- For any point
**xn**, by using the equation of line we can get:**f'(x[n]) = (f(x[n+1]) - f(x[n]))/(x[n+1]- x[n])**

[*f'(x)*being the slope at point*x*]

- Putting
**0**for y coordinate at point**x[n+1]**where tangent crosses x-axis we get:**f'(x[n]) = (- f(x[n]))/(x[n+1]- x[n])**

- Rearranging the terms & putting
**f'(x) = 2x[n]**we have our final expression:**x[n+1] = (x[n]+ N/x[n])/2**

Visualising Newton Raphson method for N = 10000

[1] For the sake of simplicity, I have omitted the details about choosing the initial value which plays a crucial role in more complex functions.

*Special thanks to AMSI for facilitating illustrations*

If you liked this post, you can find more by:

Following me on

Connecting on

Visiting my

website: https://abhimanyutimes.com

Thanks for reading!

]]>