# My Thought Process while I write a piece of code?

There are three reasons why I’m writing this article down.

- It is to give myself some
**room to think clearly**about my mindset and approach to solving problems. - To better
**introspect**how my brain breaks down programming problems and to reflect on how to make the whole process more effective. - To
**communicate**to anyone interested in wanting to know about my thought process when I write a piece of code.

Programming aka coding can be sometimes overwhelming if we don’t have the right mindset or if our expectations are **not realistic**. Some when they begin to code are under the impression that they need to know everything from top to bottom, I mean the syntax, the libraries, the sub-libraries and functions. While who do have some experience with writing code have realistic expectations when they begin to write a piece of code such that ** Yes, I’ll be making silly mistakes.** I may forget the most basic of functions. I even may have to search online,

**how to write a class.**

I do have come to a point where the latter seems inevitable that one has to accept if one wishes to become good at writing programs.

*The next important question is when you are presented with a problem how do you approach it?*

Everyone has a unique style of breaking down the problem. I’m in a constant cycle of learning hence vulnerable to making mistakes. Also, open to more room for improvement. When I’m presented with a problem I break it down into subproblems. Then I try to work on them individually and if required I may further break them down. In the end, I’ll combine these sub solutions to form a single solution to the whole problem. While attempting to break through the problem my thoughts would be more focused on the logic behind the solution than the libraries that I may use.

Often when there is no restriction in the question over what not to use. The first thing where my brain goes is do I already have a function in any library that can make my life easier? For instance, if I were asked to arrange the elements in a list in ascending order. My immediate response will be to use **sort()**. **The whole idea behind programming is problem-solving**, and I strongly advocate the idea of ** why to reinvent the wheel**. If a programming language as Python ( which I mostly use to program) gives you an upper hand in making problem-solving easier by giving you access to thousands of useful libraries and functions, why not benefit from it?.

On the other hand, some approach problem solving without the help of any pre-built function or library. It has its own perks but to be honest when working on real-world problems it just doesn’t make any sense.

I’ll present before you some basic programming questions that I received in an online course as assignments and how I approached the solution.

**Let’s begin with the most basic of problems:**

*Given a list of integers and a value k, print the number in the list that appears exactly k times. (It is guaranteed that there’s exactly one integer that appears k times).*

*Input Format:*

*The first line of the input contains a list of integers*

*The second line of the input contains a value k*

*Output Format:*

*Display the number that appears exactly k times*

*Example:*

*Input:*

*1 2 2 3 3 3*

*3*

*Output:*

*3*

**Solution:**

When I encountered this problem I haven’t coded for months The first thing that came to my mind was how to take in multiple inputs in a single line. Somewhere in the back of my mind, I knew that I have done this before but couldn’t recall it at the moment. So I did what everyone would do.

Opened a new tab typed in how to take in multiple inputs in python?

As soon as I saw the first result I immediately recalled how to do it. It was using the **map()** function. It went as follows:

Now I have the list of integers stored in a list of data. The next step was to take the second line of input which is the **value of k**.

The next thing that comes to my mind is how can I count the repetition of numbers in the list and after that how can I compare the elements uniquely with the count of a repetition.

**How to count the repetitions in a list?**

I recalled using a function, **count()** but to be sure I googled the documentation of **count()**.

The next step is to remove the repetitions in the list so that I can count the repetition of each element in that main list **data ***uniquely*. For that, I converted the whole list into a ** set**. A property of set is that it doesn’t allow repetition.

Now I can take elements from without_rep and compare the repetition to the **value of k**. Making the final solution as follows:

Similarly, let’s look at this second problem statement. **Given a list of n integers, print a new list such that every element in the new list represents the cumulative sum of all the elements until that position.**

**Input Format:**

Single line of input contains a list of space separated integers

**Output Format:**

Print the cumulative list

**Example:**

**Input:**

1 2 3 4 5

**Output:**

1 3 6 10 15

Breaking it into subproblems as follows:

- Taking in input.
- Breaking the array into subarray.
- Restoring the sub-array after processing it into a final array.
- Returning Output

**Taking the input: **It’s pretty much the same as before data=list(map(………..)

**Breaking the array into subarray: **As we have to calculate the cumulative sum of each element in the main array data. We can look at it as follow. Each element in the final result is the sum of all the elements preceding it. So the first step will go as follows:

Now we have a new_data as a 2d array each containing the elements which if summed up will result in the cumulative list of elements.

**Restoring the sub-array after processing it into a final array:**

**The output of the final result:**

Now let’s look at another problem.** Given a N X N square matrix, determine if it is a Symmetric Matrix.**

**Input Format:**

The first line of the input contains an integer N which represents the number of rows and the number of columns.

Next N lines represent the elements of the matrix.

**Output Format:**

Print Yes or No

**Example:**

**Input:**

3

1 -2 3

-2 3 1

3 1 2

**Output:**

Yes

I broke down this problem into the following subproblems:

- Taking in the input
- Generating a transpose matrix
- comparing the original matrix and the transposed matrix
- Output the final result

The input approach is always the same as before. **The tricky part was Generating a Transpose Matrix**

What is a Transpose Matrix?

It simply is just interchanging the rows and columns. If after doing so the original and transpose are the same then it’s a symmetric matrix.

For this, I need to find a way to pick the first element of each row and make it into a list and append it into another list. Let’s break it further down. The first step is to take each element of each row. For that, I have to iterate through all the rows. Therefore I need to use a loop.

for j in data:

Now after I have access to each row individually I also have to extract the first element of each row. Then followed by extraction of the second element of each row and so on and so forth. Now I can see another iteration in choosing the position of the element to be picked from each row therefore I’ll use another loop.

for i in range(N):

Now I have to combine all these extracted elements into a list for which I have the following line.

*temp is acting as a temporary variable to store the extracted elements as list.*

Now I have to store each list into another list so as to form the transpose Matrix. For that, I have

data_transpose.append(temp)

Now the code somewhat looks like this

The final step is quite simple where I have to compare the original and the transposed matrix which goes something like this.

At the end the final solution looks like the following.

Let us look at another problem. **Given an integer input ’n’, print a palindromic triangle of n lines as shown in the example.**

**Input Format:**

The input contains a number n (n < 10)

**Output Format:**

Print n lines corresponding to the number triangle

**Example:**

**Input:**

5

**Output:**

1

121

12321

1234321

123454321

When I look at this my brain breaks it into the following subproblems. The first and last is pretty much the same all the time.

- Taking the input
- Doing the Palindrome of a number.
- Doing Palindrome of all the values.
- Displaying the output

The input is a single number so a simple ** int(input())** would do the job. The next step is doing palindrome. After observing the output sample it seems the palindrome corresponding to each value is different. So I need to have a loop and iterate a function, say

**in it.**

*makePalindromOf(value)*1

121

12321

1234321

123454321

When we look at the triangle we find that each line begins from 1 to the a given value(**say value) **then reverse and ending back at 1. This

**is always the central value in any line and the peak of the series that begins with 1, furthermore then going in descending order a and ending as well.**

*value*The first step will be to generate a list from 1 to value and printing it such that the next output is also in the same line:

temp1=list(range(1,value))

print(*temp1,sep=’’,end=’’)

The next step is to print the central value which is ** value **followed by reversing the first sequence of 1 to value and printing it in the same line.

The final function will look as follows:

Now after we combine Input, Palindrome and the output we have the final solution which looks as follows:

It’s worthwhile mentioning it here that I’m writing this article in multiple shifts. I have allocated 15 mins per day for the past few days when I sit down and continue to write from where I had left the day before. That is why my thought flow may not be consistent throughout as each day I sit down with a fresh perspective and insight for the codes that I have written.

Most of the problems that I have discussed so far are the solutions that I have come up with to the coding assignments and are approved and marked correct by the moderators of the NPTEL portal.

I hope I was able to communicate well my thought process while I program. I would love to hear back and receive some feedback about it. Any suggestion or insights will be of great. I hope this article lands in front of some great programmers for some better insights.