DEV Community

Jenny Shaw
Jenny Shaw

Posted on

If You Can Sort Your Socks, You Can Program

Programming Panic! And How to Move Past it

Most people get intimidated and frustrated by programming at some point or another.

There was one afternoon when I found one of my classmates slumped deeply into his seat, and he lamented that he'd lately been feeling less and less like a programmer. There was a coding problem he wasn't able to solve on his own, and because it wasn't the only time, he was beginning to experience classic imposter syndrome. He felt problem-solving didn't come as naturally to him as it seemed to for others, and with the memory of those already failed attempts echoing in his mind, he might've wondered if this career transition into web development was ever a good idea.

As I listened to him, I thought a bit about my own struggles with coding. Before I dove into programming, I worried that I wasn't "made for it" either. I can remember being 9 or 10 years old and my mother disappointedly shaking her head at me, wondering aloud if there was any hope or future for a child who couldn't pick up basic fractions and decimals as quickly as she expected. Through high school, I was an okay math student, and in college, I avoided the subject almost entirely. By then, I'd already accepted that I simply wasn't math-minded and wasn't ever going to be, and I decided that any sort of career path that involved science or computers wasn't going to be on my life's agenda.

And then, surprise! I started teaching myself programming last year and found myself really enjoying it. But, as I looked around at coding problems, I felt very easily frustrated and lost. Either I didn't know how to start approaching a problem, or if I did, I felt stupid after looking at other solutions because there was always someone else who wrote their algorithm in fewer, more concise lines or in a "smarter," more efficient way. I was gauging my success against a Top 100 scoreboard and setting the bar for myself unreasonably high, which made me neither more productive nor motivated. It was extremely off-putting, and similarly to how my classmate felt, I was quite close to thinking that I'd made a huge mistake pursuing this.


But then, I came across two pieces of life-changing advice that got me over this obstacle.

The first one came from some wonderful human on the internet who reminded us novice-programmers that problem-solving should be exactly what it sounds like. Solving a problem. It doesn't matter much what the solution is as long as the solution works. There is always more than one way to solve a problem, and any working solution is okay because it gets the job done. Seeing another programmer write this made me feel extremely validated and it made the goal of solving a problem much less stressful and more attainable.

The second piece of advice that considerably influenced the way I visualized and learned concepts came from reading Barbara Oakley's Mindshift where she illustrated the wonderful benefits of utilizing metaphors in learning. She emphasized that no matter what you are learning, using metaphors can "bring [a] key idea to life". With metaphors and analogies, concepts make more sense, they're more memorable, relatable, tangible, and easier to work with.

Inspired by these brilliant pieces of advice, I made a conscientious effort to change the way I approached coding and coding problems. And so, I suggested to my classmate that maybe he just needed to think about coding problems in a different context or from a different perspective, one that was more relatable and familiar to him.


A lot of people take it for granted, but it's hard to think of and explain every detailed step needed to, for example, sort numbers in an array. Sorting socks, on the other hand, is a physical, visual process and a routine that many of us are extremely familiar with. If you've made it this far in life, I'm willing to bet that sorting socks is something you know well enough that you could teach it to a child. And if that's true, you can most certainly handle typing out an algorithm for your computer to sort numbers.

I can already hear people screaming at me through their monitors, "It's not that simple! Programming and sock sorting aren't the same things!" I'm not saying that programming is as simple as sorting socks, but that using metaphors such as sock sorting can help you wrap your head around an abstract problem and give it some tangible form and clarity.

Metaphors and analogies have often been the most effective learning and teaching tools for me. When I see a coding problem that involves just numbers, I feel like I have to try harder to grasp all the information and keep track of it while trying to make things work. But if I can somehow relate to it in ways that make sense to me, trying to solve it doesn't feel like such a difficult task. It really does amazing things for establishing an understanding of the concept at hand. (And, as a bonus, it also gives me some great perspective on how I approach other problems in real life.)

Let's Sort Socks Together

Here's a problem from Leetcode that my cohort was asked to consider a few weeks ago.

Given a non-empty array of integers, every element appears twice except for one. 
Find that single one.

Input: [4,1,2,1,2]
Output: 4 

Looking at this problem, it seems super simple and obvious enough how to solve it. But if you couldn't just point your finger at the answer and instead had to write instructions to get it, how would you do it?

I would imagine that the non-empty array of integers is a basket full of socks. Every sock belongs to a pair, except for one lonely sock. An all too familiar situation.

So, let's re-write this problem to match it. The element names will change, but the problem is exactly the same.

Given a basket full of socks, every sock appears twice, except for one. 
Find that single lonely sock.

Input: ['blue', 'striped', 'yellow', 'striped', 'yellow']
Output: 'blue'

So, now that we're discussing a real-life situation we all know and understand, let's think about how to approach it.

  1. First, I'd make sure there's space around my basket to set aside each sock.
  2. I'd take a sock from the basket, take note of the color or pattern, and then see if there's a matching sock already set aside.
  3. If there is a match, I'll put it together with that matching sock.
  4. If there isn't, I'll set it aside someplace else where it can wait for its match.
  5. I'll continue the process until I run out of socks.
  6. And when there are no more socks, I'll see which sock piles are paired, and which one is lonely.

Perfect! Now that I have a game plan, I'm ready to translate this into code.

function findSingleSock(socks) {           // Mission: find single sock
  let lonelySock = "";                     // Target: the lonely sock
  let sortSpace = {};                      // Make space

  socks.forEach(sock => {                  // Go through each sock
    if (sortSpace.hasOwnProperty(sock)) {  // See if space has a sock with same property
      sortSpace[sock] += 1;                // If so, add to sock count
    } else {
      sortSpace[sock] = 1;                 // If not, create new pile 
    }
  });

  for(let pile in sortSpace){              // Go through each sorted pile
    if (sortSpace[pile] === 1) {           // If there's a pile with only 1 sockj
      lonelySock = pile;                   // Declare it the lonely sock
    }
  }

  return lonelySock;                       // Done!
}

As I said before, there are so many ways to go about solving a problem. Maybe I didn't choose the fastest or simplest way, but this is one that I thought was the most realistic. We're not worrying about fast or efficient right now. All that matters is that we found the single sock (or number), and we did a great job getting there!

Conclusion

People solve problems all the time, not only programmers. Whether it's at school, work, or home, we all encounter some kind of problem that needs solving, or at the very least a routine or process that could use improvement. If you can solve or think critically about the everyday problems in your life, you can certainly solve a coding problem. And just because a problem involves code doesn't mean it has to be complicated or the solution has to be fancy. In the end, all problems have one desirable outcome, and that's a solution that works, whatever it is that gets the job done. Efficiency, speed, and sometimes ingenuity are bonuses, and those are all skills that develop with practice.

On that note about efficiency and speed, there's one final piece of advice that I haven't gotten into much here but is still worth noting. Solving problems with other people is really the best kind of practice you can get. It's hard to think of different solutions if you don't know what options and tools are out there. If you can expose yourself to different ways of thinking, you can to get to expert level problem-solving way faster!

Good luck out there! Happy coding!

Just another sidenote! After I already started writing this blog, I fortuitously came across this book, Bad Choices, by Ali Almossawi, at the bookstore, and it explains how understanding and using algorithms help us make smarter real-life choices. And behold, sock matching is on the first chapter! Might be worth a read!

Top comments (0)