Once upon a time there was a silly programmer, his wife gave him a task: “go buy 3 jin of oranges, buy the biggest, sweetest and cheapest navel orange.”


Normal people go to the store and buy fruit, but programmers don’t. Programmer heart way: “big, good distinguish, sweet, bad distinguish. Fruit shop owners don’t let every orange sample.” So ask Siri, Siri said: “generally orange bottom has umbilical ring, is the mother orange, the mother orange is sweeter.” In this way, through research, the technical problem of sweetness discrimination was solved.


Then the programmers started plotting where to buy. Siri found four fruit stores in the neighborhood. The programmer said to himself, “Each of these four stores could sell oranges, and they could all be the cheapest. If you go to shop A to buy something, and shop C is the cheapest, you will lose your conscientiousness and may be scolded when you come back. The prudent thing to do would be to shop around, look at each house and write down the price, then choose the one with the lowest price. But which one should you go to first? In theory, you should go to the store away from home first and go to the store farthest from home last.”



According to the rough estimation, the four stores are ranked by the distance from home, and the order is ABCD. Programmers should go to store A first. But the estimation is not rigorous after all, so the programmer please baidu map, on the map one by one mark home and 4 stores of GPS coordinates, and then the fruit store GPS coordinates respectively calculate the distance from home. Finally, it is concluded that the order is CABD (because store C is the direct distance, the ordering logic of store distance is omitted).



So the programmer goes to store C, gets A result, then goes to store ABD separately, and finally gets store A returns infinity. Store A may be out of navel oranges or out of stock. Of the other three stores, store B is the cheapest. The programmer decided to go to Store B to buy it. At this time the programmer’s wife called, said “call you to buy an orange how to go so long!” The programmer said, “Target locked, mission completed.”


When the programmer arrived at store B, he did not hesitate to start the logic of choosing navel oranges:



Half an hour later, the programmer returned home with the fruit. The wife scold him: “how to buy fruit compare to plant fruit tree all slow!” Programmers began to reflect on the process of buying fruit is there a lot of room for optimization?


On the second orange purchase, the programmer combined the purge data source and bubble sort loops:



Due to the reduced time complexity, and omitted the first technical research, system architecture time, this time only 20 minutes, but his wife is still too slow: “others buy fruit a few minutes, you buy fruit 20 minutes!”


For the third time, instead of using a one-hand bubble sort, the programmer used a two-hand quicksort, comparing left hand to right and right hand to left (the algorithm is too complicated to be shown as a pseudo-code). This time it took 15 minutes, but my wife was still not satisfied.


The fourth time to buy fruit, the programmer optimized the inquiry logic into the store. Before a few times to buy fruit, the programmer to the boss’s wechat, this set out before the first 4 bosses sent a group of micro information: “boss, today navel orange still have no, how much money a catty?”


The task was completed in 5 minutes instead of the previous single thread walking task. The wife is satisfied.


Programmer sighed: “program optimization is really endless ah! However, it seems that the current logic can be further optimized. For example, the first choice of fruit purchase is basically B store, and next time you can directly go to B store. I sent wechat inquiries to four bosses on the way to the store, but the answer is still B store. If not, there’s still time to adjust your route before you get in. It’s a multi-threaded process of execution and inquiry.”


Can you see the difficulty in programming from the story of the programmer buying oranges?


29 December 2018


There was once a silly programmer. His wife gave him a task. He had to do it four times before he was satisfied