Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 5, 2022 07:50 am GMT

What Is the Difference in Thinking Model Between Programmers and Normal Persons?

Recently, one of my friends approached me. He told me that he wanted to learn programming, but he didnt 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:

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: Its 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 dont 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 dont know.

I said: Its okay. This is actually a classic algorithm problem in programming. Its normal that you wont 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, its 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, its okay. Im a little hungry. Lets buy some apples first.


Original Link: https://dev.to/bytefish/what-is-the-difference-in-thinking-model-between-programmers-and-normal-persons-4c6m

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To