像程序员一样思考
by Richard Reis
理查德·里斯(Richard Reis)
如何像程序员一样思考-解决问题的经验教训 (How to think like a programmer — lessons in problem solving)
If you’re interested in programming, you may well have seen this quote before:
如果您对编程感兴趣,那么您可能之前已经看过以下引用:
“Everyone in this country should learn to program a computer, because it teaches you to think.” — Steve Jobs
“这个国家的每个人都应该学习编程计算机,因为它可以教您如何思考。” —史蒂夫·乔布斯
You probably also wondered what does it mean, exactly, to think like a programmer? And how do you do it??
您可能还想知道,像程序员一样思考到底意味着什么? 以及你如何做到的?
Essentially, it’s all about a more effective way for problem solving.
本质上, 这都是解决问题的更有效方法 。
In this post, my goal is to teach you that way.
在这篇文章中,我的目标是教你这种方式。
By the end of it, you’ll know exactly what steps to take to be a better problem-solver.
到最后,您将确切地知道要成为一个更好的问题解决者应采取的步骤。
为什么这很重要? (Why is this important?)
Problem solving is the meta-skill.
解决问题是超能力。
We all have problems. Big and small. How we deal with them is sometimes, well…pretty random.
我们都有问题。 大和小。 有时,我们如何对待它们……相当随机。
Unless you have a system, this is probably how you “solve” problems (which is what I did when I started coding):
除非您有系统,否则这可能就是您“解决”问题的方式(这是我开始编码时所做的事情):
- Try a solution. 尝试解决方案。
- If that doesn’t work, try another one. 如果那不起作用,请尝试另一个。
- If that doesn’t work, repeat step 2 until you luck out. 如果那不起作用,请重复步骤2,直到运气好为止。
Look, sometimes you luck out. But that is the worst way to solve problems! And it’s a huge, huge waste of time.
看,有时候你运气不好。 但这是解决问题的最糟糕的方法! 这是一个巨大的浪费时间。
The best way involves a) having a framework and b) practicing it.
最好的方法包括a)拥有框架,b) 实践它。
“Almost all employers prioritize problem-solving skills first.
“几乎所有雇主都将解决问题的技能放在首位。
Problem-solving skills are almost unanimously the most important qualification that employers look for….more than programming languages proficiency, debugging, and system design.
解决问题的技能几乎是雇主所寻求的最重要的资质……不仅仅是编程语言的熟练程度,调试和系统设计。
Demonstrating computational thinking or the ability to break down large, complex problems is just as valuable (if not more so) than the baseline technical skills required for a job.” — Hacker Rank (2018 Developer Skills Report)
证明计算思维或解决大型复杂问题的能力与工作所需的基本技术技能同样有价值(如果不是更多的话)。” -黑客排名( 2018开发人员技能报告 )
有一个框架 (Have a framework)
To find the right framework, I followed the advice in Tim Ferriss’ book on learning, “The 4-Hour Chef”.
为了找到合适的框架,我遵循了蒂姆·弗里斯(Tim Ferriss)关于学习的书“ 4小时厨师 ”中的建议。
It led me to interview two really impressive people: C. Jordan Ball (ranked 1st or 2nd out of 65,000+ users on Coderbyte), and V. Anton Spraul (author of the book “Think Like a Programmer: An Introduction to Creative Problem Solving”).
这使我采访两位真是令人印象深刻的人: C.乔丹·鲍尔 (排名第1或2日进行65,000+用户Coderbyte )和V.安东Spraul (书“的作者认为象程序员:介绍创造性地解决问题 ”)。
I asked them the same questions, and guess what? Their answers were pretty similar!
我问了他们同样的问题,你猜怎么着? 他们的答案非常相似!
Soon, you too will know them.
很快,您也将了解它们。
Sidenote: this doesn’t mean they did everything the same way. Everyone is different. You’ll be different. But if you start with principles we all agree are good, you’ll get a lot further a lot quicker.
旁注:这并不意味着他们以相同的方式做所有事情。 每个人都不一样。 你会有所不同。 但是,如果您从原则入手,我们都同意是好的,那么您会更快地获得更多。
“The biggest mistake I see new programmers make is focusing on learning syntax instead of learning how to solve problems.” — V. Anton Spraul
“我看到新程序员犯的最大错误是专注于学习语法,而不是学习如何解决问题。” — V.安东·斯普劳
So, what should you do when you encounter a new problem?
那么,遇到新问题该怎么办?
Here are the steps:
步骤如下:
1.了解 (1. Understand)
Know exactly what is being asked. Most hard problems are hard because you don’t understand them (hence why this is the first step).
确切知道要问什么。 大多数棘手的问题之所以棘手,是因为您不理解它们(因此,这是第一步)。
How to know when you understand a problem? When you can explain it in plain English.
如何知道您何时了解问题? 当您可以用简单的英语解释它时。
Do you remember being stuck on a problem, you start explaining it, and you instantly see holes in the logic you didn’t see before?
您是否还记得被困在某个问题上,开始进行解释并立即发现以前未曾发现的逻辑漏洞?
Most programmers know this feeling.
大多数程序员都知道这种感觉。
This is why you should write down your problem, doodle a diagram, or tell someone else about it (or thing… some people use a rubber duck).
这就是为什么您应该写下问题,绘制图表或将其他事情告诉别人的原因(或其他一些人使用橡皮鸭的事情)。
“If you can’t explain something in simple terms, you don’t understand it.” — Richard Feynman
“如果您不能用简单的术语来解释,那么您将无法理解。” —理查德·费曼(Richard Feynman)
2.计划 (2. Plan)
Don’t dive right into solving without a plan (and somehow hope you can muddle your way through). Plan your solution!
不要没有计划就直接投入解决方案(以某种方式希望您能摸索到底)。 规划您的解决方案!
Nothing can help you if you can’t write down the exact steps.
如果您无法写下确切的步骤,没有任何帮助。
In programming, this means don’t start hacking straight away. Give your brain time to analyze the problem and process the information.
在编程中,这意味着不要立即开始进行黑客攻击。 给您的大脑时间分析问题并处理信息。
To get a good plan, answer this question:
为了获得良好的计划,请回答以下问题:
“Given input X, what are the steps necessary to return output Y?”
“考虑到输入X,返回输出Y需要采取什么步骤?”
Sidenote: Programmers have a great tool to help them with this… Comments!
旁注:程序员有一个很棒的工具来帮助他们。
3.划分 (3. Divide)
Pay attention. This is the most important step of all.
请注意。 这是最重要的一步。
Do not try to solve one big problem. You will cry.
不要试图解决一个大问题。 你会哭。
Instead, break it into sub-problems. These sub-problems are much easier to solve.
而是将其分解为多个子问题。 这些子问题更容易解决。
Then, solve each sub-problem one by one. Begin with the simplest. Simplest means you know the answer (or are closer to that answer).
然后,一个一个地解决每个子问题。 从最简单的开始。 最简单意味着您知道答案(或更接近答案)。
After that, simplest means this sub-problem being solved doesn’t depend on others being solved.
此后,最简单的方法意味着要解决的子问题不依赖于要解决的其他问题。
Once you solved every sub-problem, connect the dots.
解决了每个子问题后,将点连接起来。
Connecting all your “sub-solutions” will give you the solution to the original problem. Congratulations!
连接所有“子解决方案”将为您解决原始问题。 恭喜你!
This technique is a cornerstone of problem-solving. Remember it (read this step again, if you must).
该技术是解决问题的基石。 记住这一点(如果需要,请再次阅读此步骤)。
“If I could teach every beginning programmer one problem-solving skill, it would be the ‘reduce the problem technique.’
“如果我可以教每个新手程序员一种解决问题的技能,那将是'减少问题的技术'。”
For example, suppose you’re a new programmer and you’re asked to write a program that reads ten numbers and figures out which number is the third highest. For a brand-new programmer, that can be a tough assignment, even though it only requires basic programming syntax.
例如,假设您是一名新程序员,并且要求您编写一个程序,该程序读取十个数字,并弄清楚哪个数字是第三高的。 对于一个全新的程序员来说,即使只需要基本的编程语法,这也是一项艰巨的任务。
If you’re stuck, you should reduce the problem to something simpler. Instead of the third-highest number, what about finding the highest overall? Still too tough? What about finding the largest of just three numbers? Or the larger of two?
如果您遇到困难,则应将问题减少到更简单的程度。 而不是第三高的数字,如何找到最高的整体呢? 还是太强硬了? 如何找出三个数字中的最大数字呢? 还是两者中的较大者?
Reduce the problem to the point where you know how to solve it and write the solution. Then expand the problem slightly and rewrite the solution to match, and keep going until you are back where you started.” — V. Anton Spraul
将问题减少到知道如何解决并编写解决方案的程度。 然后稍微扩大问题范围,重新编写匹配的解决方案,然后继续进行下去,直到回到起点为止。” — V.安东·斯普劳
4.卡住了吗? (4. Stuck?)
By now, you’re probably sitting there thinking “Hey Richard... That’s cool and all, but what if I’m stuck and can’t even solve a sub-problem??”
现在,您可能正坐在那里,想着“嗨,理查德……这很酷,但是如果我被困住甚至不能解决子问题怎么办?”
First off, take a deep breath. Second, that’s fair.
首先,深呼吸。 第二,这很公平。
Don’t worry though, friend. This happens to everyone!
朋友,请不要担心。 这发生在每个人身上!
The difference is the best programmers/problem-solvers are more curious about bugs/errors than irritated.
不同之处在于,最好的程序员/问题解决者对错误/错误的好奇大于对错误的好奇。
In fact, here are three things to try when facing a whammy:
实际上,面对混乱时,可以尝试以下三件事:
Debug: Go step by step through your solution trying to find where you went wrong. Programmers call this debugging (in fact, this is all a debugger does).
调试:逐步解决您的解决方案,以查找错误的地方。 程序员将其称为调试 (实际上,这是调试器的全部工作)。
“The art of debugging is figuring out what you really told your program to do rather than what you thought you told it to do.”” — Andrew Singer
“调试的技巧在于弄清您真正告诉程序要执行的操作,而不是您认为要执行的操作。” –安德鲁·辛格(Andrew Singer)
Reassess: Take a step back. Look at the problem from another perspective. Is there anything that can be abstracted to a more general approach?
重新评估: 退后一步。 从另一个角度看问题。 有什么可以抽象为更通用的方法的吗?
“Sometimes we get so lost in the details of a problem that we overlook general principles that would solve the problem at a more general level. […]
“有时候,我们对问题的细节迷失了,以至于忽略了可以从更一般的层面解决问题的一般原则。 […]
The classic example of this, of course, is the summation of a long list of consecutive integers, 1 + 2 + 3 + … + n, which a very young Gauss quickly recognized was simply n(n+1)/2, thus avoiding the effort of having to do the addition.” — C. Jordan Ball
当然,这方面的经典示例是一长串连续整数的总和,即1 + 2 + 3 +…+ n,一个非常年轻的高斯很快就认出了n(n + 1)/ 2,因此避免了进行添加的工作。” — C.乔丹·鲍尔
Sidenote: Another way of reassessing is starting anew. Delete everything and begin again with fresh eyes. I’m serious. You’ll be dumbfounded at how effective this is.
旁注:重新评估的另一种方法是重新开始。 删除所有内容,然后重新开始。 我是认真的。 您将不知该如何有效。
Research: Ahh, good ol’ Google. You read that right. No matter what problem you have, someone has probably solved it. Find that person/ solution. In fact, do this even if you solved the problem! (You can learn a lot from other people’s solutions).
研究: 啊,Google不错。 您没看错。 无论您遇到什么问题,都可能有人解决了。 找到那个人/解决方案。 实际上,即使您解决了问题,也要这样做! (您可以从其他人的解决方案中学到很多东西)。
Caveat: Don’t look for a solution to the big problem. Only look for solutions to sub-problems. Why? Because unless you struggle (even a little bit), you won’t learn anything. If you don’t learn anything, you wasted your time.
警告:不要为大问题寻找解决方案。 只寻找子问题的解决方案。 为什么? 因为除非您奋斗(一点点),否则您将不会学到任何东西。 如果您什么都不学,那您就浪费了时间。
实践 (Practice)
Don’t expect to be great after just one week. If you want to be a good problem-solver, solve a lot of problems!
仅仅一周之后,别指望它会变得很棒。 如果您想成为一个好的问题解决者,请解决很多问题!
Practice. Practice. Practice. It’ll only be a matter of time before you recognize that “this problem could easily be solved with <insert concept here>.”
实践。 实践。 实践。 认识到“使用“在此处插入概念”可以轻松解决此问题”只是时间问题。
How to practice? There are options out the wazoo!
怎么练习? wazoo有多种选择!
Chess puzzles, math problems, Sudoku, Go, Monopoly, video-games, cryptokitties, bla… bla… bla….
国际象棋难题,数学问题,数独,围棋,专卖,视频游戏,加密小猫,等等……等等……等等……。
In fact, a common pattern amongst successful people is their habit of practicing “micro problem-solving.” For example, Peter Thiel plays chess, and Elon Musk plays video-games.
实际上,成功人士中的常见模式是他们练习“解决微观问题”的习惯。 例如,彼得·泰尔(Peter Thiel)下棋,埃隆·马斯克(Elon Musk)玩电子游戏。
“Byron Reeves said ‘If you want to see what business leadership may look like in three to five years, look at what’s happening in online games.’
“拜伦·里夫斯(Byron Reeves)说:“如果您想了解三到五年内业务领导者的模样,请看一下在线游戏中正在发生的事情。”
Fast-forward to today. Elon [Musk], Reid [Hoffman], Mark Zuckerberg and many others say that games have been foundational to their success in building their companies.” — Mary Meeker (2017 internet trends report)
快进到今天。 Elon [Musk],Reid [Hoffman],Mark Zuckerberg和许多其他人说,游戏是成功建立公司的基础。” — Mary Meeker( 2017年互联网趋势报告 )
Does this mean you should just play video-games? Not at all.
这是否意味着您应该只玩视频游戏? 一点也不。
But what are video-games all about? That’s right, problem-solving!
但是电子游戏到底是什么呢? 是的,解决问题!
So, what you should do is find an outlet to practice. Something that allows you to solve many micro-problems (ideally, something you enjoy).
因此,您应该做的是找到一个练习的出口。 使您能够解决许多微问题的事物(理想情况下,您喜欢的事物)。
For example, I enjoy coding challenges. Every day, I try to solve at least one challenge (usually on Coderbyte).
例如,我喜欢编码方面的挑战。 每天,我都会尝试解决至少一个难题(通常在Coderbyte上 )。
Like I said, all problems share similar patterns.
就像我说的,所有问题都有相似的模式。
结论 (Conclusion)
That’s all folks!
那是所有人!
Now, you know better what it means to “think like a programmer.”
现在,您更好地了解了“像程序员一样思考”的含义。
You also know that problem-solving is an incredible skill to cultivate (the meta-skill).
您还知道解决问题是一种不可思议的技能(超能力)。
As if that wasn’t enough, notice how you also know what to do to practice your problem-solving skills!
如果这还不够,请注意您还知道如何去练习解决问题的技能!
Phew… Pretty cool right?
ew……挺酷的吧?
Finally, I wish you encounter many problems.
最后,祝您遇到许多问题。
You read that right. At least now you know how to solve them! (also, you’ll learn that with every solution, you improve).
您没看错。 至少现在您知道如何解决它们了! (此外,您将了解到每种解决方案都可以改进)。
“Just when you think you’ve successfully navigated one obstacle, another emerges. But that’s what keeps life interesting.[…]
“只要您认为自己已经成功克服了一个障碍,就会出现另一个障碍。 但这就是让生活变得有趣的原因。[…]
Life is a process of breaking through these impediments — a series of fortified lines that we must break through.
生活是克服这些障碍的过程,这是我们必须突破的一系列防御性路线。
Each time, you’ll learn something.
每次,您都会学到一些东西。
Each time, you’ll develop strength, wisdom, and perspective.
每次,您都会发展力量,智慧和远见。
Each time, a little more of the competition falls away. Until all that is left is you: the best version of you.” — Ryan Holiday (The Obstacle is the Way)
每次,更多的竞争就会消失。 剩下的只有你:你最好的版本。” -Ryan Holiday( 《障碍就是路》 )
Now, go solve some problems!
现在,去解决一些问题!
And best of luck ?
祝你好运?
Special thanks to C. Jordan Ball and V. Anton Spraul. All the good advice here came from them.
特别感谢 C. Jordan Ball和V. Anton Spraul 。 这里所有好的建议都来自他们。
Thanks for reading! If you enjoyed it, test how many times can you hit in 5 seconds. It’s great cardio for your fingers AND will help other people see the story.
谢谢阅读! 如果喜欢,请测试您可以在5秒钟内击中多少次。 这对您的手指来说是很好的心脏锻炼,并且可以帮助其他人看到这个故事。
像程序员一样思考