今日:250| 主题:58096
收藏本版 (1)

[其他] Proof You Can Become a Better Programmer

下一秒落寞 投递于 2016-11-27 23:34:46
179 2
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 self-taught beginner.
  Complete Beginner i.e The Bad Way

  1. i = 0

  2. number = false

  3. range = (1..20).to_a

  4. while number != true

  5.   i+= 1

  6.   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

  7.   number = true

  8.   puts i

  9. end

  10. 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:
  1. def smallest_divisible_number(n)
  2.   (1..n).inject(:lcm)
  3. 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 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.
  1. def smallest_divisible_number(n)
  2.   (1..n).reduce(:lcm)
  3. 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.
   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

  1. def compress(input)
  2.   input = input.split('')
  3.   outputArr = []
  4.   count = 1
  5.   secondLetter = 1
  6.   firstLetter = 0
  7.   input.each do |l|
  8.     if input[secondLetter] == input[firstLetter]
  9.       count += 1
  10.       secondLetter += 1
  11.       firstLetter += 1
  12.     else
  13.       outputArr << input[firstLetter]
  14.       puts "#{input[firstLetter]} #{count}"
  15.       count = 1
  16.       secondLetter +=1
  17.       firstLetter += 1
  18.     end
  19.   end
  20. count
  21. end
After Some Experience i.e A Better Way

  1. input = "aabbccc"

  2. def compress(input)
  3.   input.split('').map{ |x| "#{x}#{input.split('').count(x)}" }.uniq.join('')
  4. 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

Proof You Can Become a Better Programmer-1-技术控-碧桂园,exactly,深圳人,岗亭,栏杆

   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.

上一篇:The Emergence Of Distributed POSIX
下一篇:Base-122 – A space efficient alternative to base-64
名字123 投递于 2016-12-5 16:31:32
回复 支持 反对

使用道具 举报

银格登泷 投递于 2016-12-16 02:20:59
回复 支持 反对

使用道具 举报


手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表