What Is the Difference in Thinking Model Between Programmers and Normal Persons?
Tags: programming, javascript, career, beginners
Published: 2022-01-05 07:50:16

Recently, one of my friends approached me. He told me that he wanted to learn programming, but he didn’t know if he was suitable for this profession. So he asked me a question: Is there any difference between programmers and normal persons in thinking model?

I know that he has never learned programming before, and it is quite difficult to introduce programming thinking to people who have no coding experience. Um, after thinking about it for a while, I plan to explain the problem in another way.

I asked him: If you were to buy two pounds of apples now, what would you do?

He said: Go directly to the fruit shop to buy it.

I said: If we want to express the process of buying apples programmatically, it might be like this.

---

First of all, we must clarify our needs, for example:

- I plan to buy about two pounds of apples

- The price I can accept is less than 1.5$ per pound

Then we can design the following process:

- Query surrounding fruit shops to get a shop list;

- Visit the fruit shops in the list one by one, and perform the following operations:

  - Go to the fruit shop;

  - If the fruit shop is not open, end the current process and then visit the next fruit shop;

  - If there is no apple left in the fruit shop, end the current process and then visit the next fruit shop;

  - If the price of the apples is higher than US$1.5 per pound, then:

     - Ask the shop owner whether he is willing to lower the price;
     - If the shop owner does not agree, end the current process and then visit the next fruit shop
  
  - Take a bag;
  
  - Start picking apples;
  
  - Continue the following operations until the weight of the apples in the bag is greater than two pounds:

     - Pick an apple from the pile of apples;

     - Put the apple in the bag;

  - Calculate the total price of apples in the bag;

  - Total price = weight of apples in the bag multiply unit price of apples;

  - Pay money;
  
  - Leave the shop;

  - Skip the remaining fruit shops in the list;

- Take apples home;


Programmers need to consider problems in a rigorous and accurate way.

- In the above process, we need to record the surrounding fruit shops, so we need to define the variable **friutShops**.

- We need to visit different fruit shops in turn, which is called **traversal**.

- Then we need to judge the boundary conditions, for example, what if the fruit shop does not open? What if the price of Apple exceeds my expectations?

- When we are picking apples, we need to keep putting apples in the bag until it exceeds two pounds. This is called **looping**.

These are the most basic steps when a programmer considers a problem.

If we use pseudo-code to represent this process, it may be like this:

```js
Query surrounding fruit shops to get fruitShops;
for(fruitShop in fruitShops){
  if(fruitShop.isOpen == false){
    break;
  }
  if(fruitShop.apple.price > 1.5){
    Ask the shop owner whether he is willing to lower the price;
    if(the shop owner does not agree){
      break;
    } else {
      fruitShop.apple.price = newPrice;
    }
  }

  Take a bag;
  Start picking apples;
  while(the weight of the apples in the bag is less than two pounds){
    Pick an apple from the pile of apples;
    Put the apple in the bag;
  }
  Calculate the total price of apples in the bag;
  Total price = (weight of apples in the bag) * (unit price of apples);
  Pay money;
  Leave the shop;
  return;
}
Take apples home;
```

---

After listening to my description, my friend said: Um, you explained well, it seems simple.

Then, I went on to say: The process is really not complicated, but in the real development, we have to consider many things. For example, in the above case, as long as we find a shop where the price of apples is less than $1.5 per pound, we will buy apples immediately. But if now we want to find the fruit shop with the lowest price for apples. What should we do?

My friend said: It’s too simple. Go to every fruit shop and ask for the price. Then you can find the fruit shop with the lowest price.

I said: But we are very lazy and don’t want to walk too much. Now there are 10 fruit shops around us, and they are distributed in different locations. If we want to walk as little as possible while traversing these 10 fruit shops, how can we arrange the order of visits to minimize the total path?

After thinking about it for a long time, my friend replied: Well, this question sounds complicated, I don’t know.

I said: It’s okay. This is actually a classic algorithm problem in programming. It’s normal that you won’t think of the answer for a while. When a programmer writes code, he must not only solve the problem correctly, but also solve the problem as efficiently as possible. In the development process, we are solving similar problems one by one.

Then I continue: And when we choose apples, we all hope to buy bigger and redder apples. Now you need to select N apples from the pile of apples. And you need to make sure that they are the best apples and that the sum of their masses is just over 2 pounds. How do you choose?

My friend: Well, it’s still a bit difficult.

I said: Some people will first sort the apple piles according to the quality, and then choose the best apples, but how to sort the apple piles quickly is another problem.

My friend: Okay, stop talking about it, my mind is a little dizzy. It seems that I am still not suitable as a programmer.

I said: Haha, it’s okay. I’m a little hungry. Let’s buy some apples first. 😁😁