Code War Python Challenge: Square Every Digit

avatar
(Edited)

image.png

Yesterday I wrote about Code Wars, one of my favorite programming challenge sites. If you want to get better as a programmer, or are learning how to program check out my introduction and get signed up.

It is completely free!

I recommend doing at least one challenge a day as a routine after you get started, it will help keep you learning and solving real world problems. I started a new account and doing the same thing myself.

Today's challenge

def square_digits(num):
    pass

As I mentioned in my Code Wars introduction, you want to break your problems into smaller problems. In fact, get as small as you can.

This challenge involves a few things.

  • Iterate digits of a number
  • Square a number
  • Concatenate result

Let's start with the first problem. As an integer is not iterable, we need to convert the input number to a string.

def square_digits(num):
    for digit in str(num):
        pass

The second problem we need to solve is how to square the digits.

def square_digits(num):
    output = ""
    for digit in str(num):
        output += str(int(digit) ** 2)

In this step we are able to solve both problem #2 and problem #3 as we are going to build a string as we iterate each digit.

We first needed a new variable to store the output as we iterate each digit.

We had to cast the digits between str and integer to make this work. First, we needed to convert digit (string) to an integer so we could use the power operator (**) and square the digit. We then had to convert it to a string again to store it in the output (string) variable. We are using the output variable to easily concatenate the output using native string functionality.

Finally we need to return the result, a simple return output wouldn't work as the challenge is expecting an integer, so we need to cast it as an integer.

def square_digits(num):
    output = ""
    for digit in str(num):
        output += str(int(digit) ** 2)
    return int(output)

Let's test our solution.

Looks good! I submitted the solution and reviewed the best answers.

The first answer is extremely similar to our solution, so I am happy that we have a good solution.

The second solution is smaller, more compact, and very clever but considerably harder to read. Being clever is not always a good thing, it's ok to be clever in your algorithms and logic, but being clever with syntax generally results in difficult to read code.

I challenge you to sign up for Code Wars, pick your favorite languages and do your own challenges.

If you like this series, let me know in the comments and I will do more.


Securely chat with me on Keybase

Why you should vote me as witness



0
0
0.000
12 comments
avatar

Code Wars is a bit more advanced than new users can do :/
It might be good when you have some experience :D

0
0
0.000
avatar

It is harder for new programmers, but certainly not impossible. It is good to step outside your comfort zone, it is the only way to grow.

A lot of these challenges require research on how to solve the sub problems.

For example a lot of new developers would try to iterate a number using a for loop and find out that doesn't work, then they would do a google search on how to iterate a number and likely find some suggestions and move on to the next problem.

You don't need a lot of experience in development to understand how to break a problem into smaller problems. Turning these smaller problems into code is something you certainly get better at with experience.

Actually writing code that isn't handed to you is the best way to grow and Code Wars provides thousands of unique problems that are not overwhelming and don't involve solving hundreds of problems like a typical real world project would. You just have to write one function. Perfect for a daily routine.

0
0
0.000
avatar

Yes, I agree on getting out of your comfort zone id good and writing your own code / working on a small project is an amazing way to learn!

But it can also be discouraging when you get a problem and you don't understand anything xD

Thats why I think it would be better with a small amount of knownledge, so you at least have an idea of what is going on.

I will 100% use Code Wars in the future!

0
0
0.000
avatar

If you get to a problem and you don't understand anything you likely haven't broken it down small enough.

There are times (and a lot of them) where I feel I am absolutely clueless how to solve a problem.

0
0
0.000
avatar

Yeah I do believe that I'm not going to be able to do all this because it is pretty much Klingon...

Congratulations to you for being able to understand it and work with it.

0
0
0.000
avatar

Thats how I feel haha xD

0
0
0.000
avatar

I am not saying you cannot learn how to do this however at my current perspective this is definitely rocket surgery.

Yep but it's okay maybe one of these days I'll try my hand at it.

0
0
0.000
avatar

Anyone can code. Just need to put the effort in.

0
0
0.000
avatar

I believe it. Just seeing that question from the perspective I have? Yeah that's a bit more esoteric than I'm able to understand.

I'm sure the difference between a 300 magnum and a 408 cheytac at 1200+ yards and difference in holdovers is equally a mystery for you. Different strokes for different folks.

But yes you are correct. Anyone can learn anything if we want to use our potential.

0
0
0.000
avatar

The real answer is you don't want to learn to code :)

0
0
0.000
avatar

Actually I have really never considered it as I have so much on my plate with Life currently.

I understand that it is definitely a talent for some people.

0
0
0.000