Categories
Code challenges Programming

30 Days of Code #5

In which I get stuck, Google it, sleep on it, and then make something new

Many software engineering memes joke about how the majority of the process is just Google-ing solutions on the internet. It’s good if you want to get a job done quickly, but it’s not always helpful. For one thing, your problem might be different in some way, and copy-pasting another person’s solution might cause you more problems than before, especially if you don’t understand what is going on in the solution. Different problems require different solutions.

The other problem with just copy-pasting a solution from the internet is that there is no long-term learning involved. If I come across a problem now, in my baby stages of coding when I am doing problems everyone else is also doing, it is almost guaranteed I will see it again at some point in my career. If I don’t learn from the experience, what’s the point?

One of the reasons I appreciate Launch School is that they teach us how to search for answers, how to investigate, and how to use documentation. Google is a tool, and tools need to be taught, as well. One of their recommendations is to try to solve the problem on your own first, to load it into your brain. Work on it for a set amount of time depending on the complexity of the problem, then look up possible solutions, using both Google and documentation.

My problem was actually quite simple, but for some reason, I just couldn’t wrap my head around it. I needed to convert a seconds integer into three values: seconds, minutes, hours. I wrote it out on paper, used irb, and could not figure out why my solution was not working. I spent a while on it, somewhere around 45 minutes, then decided that this problem was look-up-able, and not worth spending so much time on.

I first looked at the documentation, but couldn’t find what I needed. Then I Googled it, and the solution came up in the very first search result. I studied this solution for a bit, tinkering around with it in irb and trying to write it from memory, then slept on it.

Today, I not only remembered it, but used it in a different way than what the Googled solution was exactly. I then finished the problem, and compared it to other LS students’ solutions. This is also a great way to learn new ways of solving problems, and one of the reasons I love Codewars.

This problem was not hard, but it required a lot of steps. Even the best-rated Codewars solutions are pretty long, and I really like having methods that do just one thing, so I feel good about splitting mine up.

Here’s the problem: Statistics for an Athletic Association.

Here’s my solution: link.

Here’s my favorite solution from another LS student. I really like it because of how it uses divmod, which is a method I need to practice using more. Also, despite everything being in one long method, it’s still really easy to understand.

def stat(str)
  return '' if str.empty?
  times = str.split(', ').map { |time| time.split('|').map(&:to_i) }
  seconds = times.map { |h, m, s| h * 3600 + m * 60 + s }.sort
  range = seconds.last - seconds.first
  avg = (seconds.reduce(0, :+) / seconds.size)
  mid = seconds.size / 2
  mean = seconds.size.odd? ? seconds[mid] : (seconds[mid - 1, 2].reduce(:+) / 2)
  range, avg, mean = secs_to_time(range), secs_to_time(avg), secs_to_time(mean)
  "Range: #{range} Average: #{avg} Median: #{mean}"
end

def secs_to_time(secs)
  hours, secs = secs.divmod(3600)
  mins, secs = secs.divmod(60)
  format('%.2d|%.2d|%.2d', hours, mins, secs)
end