技术控

今日:29| 主题:52645

[其他] Proof You Can Become a Better Programmer

149 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.
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.
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 self-taught beginner.
Complete Beginner i.e The Bad Way

[code]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[/code]   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 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:
[code]def smallest_divisible_number(n)
(1..n).inject(:lcm)
end[/code]  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 1-n
• 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.
[code]def smallest_divisible_number(n)
(1..n).reduce(:lcm)
end[/code]   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

[code]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[/code]  After Some Experience i.e A Better Way

[code]input = "aabbccc"

def compress(input)
input.split('').map{ |x| "#{x}#{input.split('').count(x)}" }.uniq.join('')
end[/code]  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:

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 ever-growing 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 win-win. 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.

 态度决定一切，不错！

 房价越来越高,所以,好男人越来越少……

• 丁磊的救赎、马化腾的神坛 豪赌和欺诈交织

摘要：一个IP定生死，在手游行业，无论是大 [...]

• 2016年全球服装上市公司市值百强榜

截止2016年12月31日，在全球177家纺织服装上市公 [...]

• 亚马逊无人机专利图曝光：路灯当充电器

【亿邦动力网讯】2月23日消息，亿邦动力网最新获 [...]

• 深扒！创业七年没得一分股份无奈净身出户 C

一个坚持专制与忽悠的创始人、一个永远相信 [...]

• 万达换了三届CEO都没搞成新零售 阿里与百联

2016年中国零售行业平均增长率只有1%，而电商整 [...]

• 顺丰今日敲钟上市 千亿市值背后的十个小故

近段时间，快递行业的话题接连不断。 　　在 [...]

• 钢铁电商成潮流?互联网驱动钢铁产业变革

第二次工业革命蒸汽机和电力的使用开启了工业化 [...]

• 小米松果非“纯正血统” 它救得了雷军吗？

下周二，小米将发布自主处理器品牌，松果。 2月2 [...]

© 2001-2017 Comsenz Inc. Design: Dean. DiscuzFans.