下一秒落寞 发表于 20161127 23:34:46
161
2
立即注册CoLaBug.com会员，免费获得投稿人的专业资料，享用更多功能，玩转个人品牌！
您需要 登录 才可以下载或查看，没有帐号？立即注册
x
Look, I am not a terrible programmer. Well, that’s not exactly true. I have just become a slightly better programmer over the years.
Now William, why do you believe this to be true, I hear you ask?
Well, I have a schedule and for the most part, I stick with it. I talked about productivity before . Now I am going to show you anecdotal proof that my advice works.
Today, I am going to demonstrate how I solved problems as a beginner and how I now solve them with two years of experience behind me. I am going to talk about two problems and I will show a before and after of my solution. You should see immediate improvement even if you are just a beginner. The second problem is something I encountered while interviewing for an internship role.
I hope this article helps you become a better programmer.
With the hindsight of years, my approach to problem solving is completely different than it was as a beginner. Does that mean my solutions are superior? No. Of course not. Someone reading this right now will have a better solution.
By the way, if that person is you, leave a comment. The whole community could benefit from your input.
Quick Disclaimer: I am aware that these particular coding problems can be Googled. If you are a person who arrived here via Google in desperate need of a solution, don’t take these as gospel because it is on a blog. I am just using these problems to prove that I now suck less.
Smallest positive number that is evenly divisible by all the numbers from 1 to 20?
The first problem I want to show you is from Project Euler . It goes like this:
“2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.”
Let’s look at how I solved this problem as a selftaught beginner.
Complete Beginner i.e The Bad Way
 i = 0
 number = false
 range = (1..20).to_a
 while number != true
 i+= 1
 if i % 2 == 0 & i % 3 == 0 & i % 5 == 0 & i % 6 == 0 & i % 7 == 0 & i % 8 == 0 & i % 9 == 0 & i % 10 == 0 & i % 11 == 0 & i % 12 == 0 & i % 13 == 0 & i % 14 == 0 & i % 15 == 0 & i % 16 == 0 & i % 17 == 0 & i % 18 == 0 & i % 19 == 0 & i % 20 == 0
 number = true
 puts i
 end
 end
复制代码 Now this solution solves the problem. But at what cost. Slow, hard to read and it demonstrates a lack of a few key principles such as:
 Being readable
 Being Modular – this script cannot be used for different inputs
 Being slow – this script is slow and is brute force
Let’s be honest, this code is just not good enough to be useful and I am ashamed to say the least.
After Some Experience i.e A Better Way
Recently I came across this problem again as my friend was doing CodeWars . With two years of experience as a programmer behind me, I helped weave this solution:
 def smallest_divisible_number(n)
 (1..n).inject(:lcm)
 end
复制代码 Now, compare before and after. This solution is faster, easier to read and is more modular. It demonstrates that I understand more about the Ruby language and it’s Object Orientated principles. Here is how it works:
 The first part (1..n) is a Ruby range which counts 1n
 Next, we call inject on e ach number.
 The final part calls the Lowest Common Multiple on each accumulative number
I arrived at this solution by doubting my past work and acknowledging that I could do better because I am still a humble beginner.
When writing this post, I discovered an even better way according to the AirBnB style guide.
 def smallest_divisible_number(n)
 (1..n).reduce(:lcm)
 end
复制代码 The reduce method is performance friendly and a more “Ruby way”* of doing things. If anyone has a better solution to this problem, I would love to read about it in the comments below.
Compress Input From aaaabbbbcccc to just a4b4c4 and count every occurrence of that letter
“ Make a string like “ aaabbdcccccf ” equal to “a3b2d1c5f1” ”
The aim of this problem is to compress a string and count the number of times each letter appears. First time I did this, I was sweating profusely because there was an interviewer hunkered over my shoulder. I still remember his coffee breath on the back of my neck sending shivers down my spine and making the hairs on my neck stand up in protest.
Ugghhh!
Notice that my answer is wrong because of the limited time you are giving in tech interviews. If I was to do this again, I would knock it out of the park.
Complete Beginner i.e The Bad Way
 def compress(input)
 input = input.split('')
 outputArr = []
 count = 1
 secondLetter = 1
 firstLetter = 0
 input.each do l
 if input[secondLetter] == input[firstLetter]
 count += 1
 secondLetter += 1
 firstLetter += 1
 else
 outputArr &amp;amp;amp;amp;lt;&amp;amp;amp;amp;lt; input[firstLetter]
 puts "#{input[firstLetter]} #{count}"
 count = 1
 secondLetter +=1
 firstLetter += 1
 end
 end
 count
 end
复制代码 After Some Experience i.e A Better Way
 input = "aabbccc"
 def compress(input)
 input.split('').map{ x "#{x}#{input.split('').count(x)}" }.uniq.join('')
 end
复制代码 Now with my second solution to problem two, you can see that it is much shorter. It took me less time. In fact, I did this in under a minute. The method is short and takes advantage of a few things:
 My understanding of problems has gotten better the more time I dedicate to programming
 My understanding of Ruby has gotten better the more I read
 Proof I understand the Ancestor of Chain concept.
As I said already, I am not the greatest Ruby programmer and this may not be the best solution. The point is to show that it is possible to learn to program and become better through commitment.
Trust the System
Recently, I took an online business course. One of the things the course author said is to trust the system. All successful students trust the system and work with it. The ones that fail, do their own thing. It is a message that sent sparks firing in my brain. One of the brilliant reasons why schools teach a large part of the population to read is because they are systems with an end goal. I have rarely met someone who can’t read. We can learn from this.
To get better we need to build our systems or follow someone else’s. You need to have a routine and not rely on motivation . You need to set time aside on your calendar, learn to obtain deep focus and build a good study habit .
Here is how my system works for learning. Bare in mind, it is always changing and evolving. Especially since I have been learning to get up earlier in the morning.
Here is a picture of my calendar:
Proof You Can Become a Better Programmer
Throughout the day, I come across things that I would like to learn. For instance, I found this great series about deployment on Youtube .
I add this to an evergrowing learning list on Evernote.
Now I simply work one resource at a time until the next one. I have also set time aside to practice on CodeWars .
You will become a better programmer in time
After some time, you will, like me, shudder in revulsion at your old code. You will inevitably know a better way because you put in the hours to learn more and get better. In fact, I am still getting better and am humbled by how much there is to still learn.
If you enjoyed this article, please feel free to share it with your friends, it is much appreciated. However, if you hated this article, please feel free to share it with your friends and show off how much smarter you are than me. This means it is a winwin. I get traffic and you demonstrate that you are smarter than some stranger on the internet.
*Ruby way means different things to different Ruby developers. Mainly it is about making your code look pretty. Unfortunately, we all know people with low standards.
P.S. There is a free course on how to get your first development job as well as a free poster with 6 of my best tips for finally getting into the industry when you sign up to my email list. 
上一篇： The Emergence Of Distributed POSIX下一篇： Base122 – A space efficient alternative to base64
