Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

How to Tackle a Coding Challenge

Give Feedback

00:00 In this part, you will see how Martin tackles the coding challenge, how he sorts his thoughts, and how he writes the first lines of code.

00:10 Welcome everyone to another Real Python Code Conversation. My name is Philipp. I’m one of the core content creators at Real Python. And joining me today is Martin. Hi, Martin. Hi, Philipp.

00:21 I’ve got a code challenge for you. I sent it over to you beforehand, so you could have a look at it. And the idea of this session is that you solve this little code challenge, and I ask you questions, and you walk me through the code, and hopefully our viewers learn a little bit about one thing or another.

00:42 Cool. The question I was having for you, the code challenge, is: Create a function that accepts a string. The function should return a string with each character in the original string doubled. So for example, if you put in Martin, then it should return MMaarrttiinn.

01:05 Got it.

01:08 So your first thoughts about this challenge? Like I said, you already got it beforehand, but once you got it, was there anything about this challenge unclear, or what were your first thoughts when you, when you saw it? So first thoughts, I guess always when I get a challenge, I have a short moment of being like, am I going to be able to do this? But this one is relatively straightforward, and then usually when I sit down to just piece it apart, you know, and stuff goes relatively easily. So this was what my first thought.

01:35 Maybe that’s also interesting to know that even if we’ve been coding for a bit, sometimes there’s a moment of just being like, confused about a question, but I think it’s relatively clear. You wrote it out quite clearly, and the instructions that I just reformatted so they fit nicely on my screen here, just give me a chance to basically go step by step here.

01:54 And you do also have an example there, so this is nice to see that also, if I didn’t entirely understand the instructions here, I get an example that clarifies it some more. Yeah.

02:04 I think it was interesting that you just reformatted this question, right? I mean, it’s not even a question, like a challenge, because there is no question mark in the end. The instructions, right?

02:15 Because I think just like with code in general, it sometimes helps to have it formatted in a way that makes it better readable for yourself. So I think now with the different lines, it’s kind of like three instructions how you could actually start coding.

02:30 Like the first one is Create a function that accepts a string. Like okay, so is this how, how we would start now? So actually I don’t think I would start with a function even though it’s pretty explicit in here in your instructions.

02:42 I would probably just see if I can get the code to work on a script basis.

02:46 Let me try it out. So what I’d start with first is I don’t really know where is the input going to come from. Right. I assume that it should be possible to handle input from separate sources, but I don’t want to deal with this at the moment.

02:59 So I’m just going to make an example string that I’m going to use for developing the code here. And let’s stick with this one, so we can also compare. So I have this example string, and then I would just continue on a script basis.

03:13 I want to loop here because I want to see each character in the original string. So I’m probably going to need a loop, right? So for character

03:24 in example string, or let’s call it word

03:28 it’s a little more reusable later. for character in word. So I’m going to loop over each of those characters, and then I want just to double up each of them. Now I need to save this somewhere.

03:40 So I’m going to make a new variable here that I’m going to call double.

03:45 And that just starts off as an empty string. And then in here, in my for loop, I want to then address this and fill up the variable. And so maybe that’s interesting to see, right? You know, you don’t necessarily write the first line, and you write the second line, and you write the third line. But I’m kind of following my thought process here with, okay, I need some input. And then I’m like, okay, I’ve got to do some looping, so I start with the for loop. This is not even a valid for loop right now, right? Because I have an error sitting down here because I didn’t do anything in there, right.

04:16 So nicer would be to write pass as a placeholder. Then you also see that VS Code isn’t complaining that there’s an error in there. But then at this point I was like, okay, and I’m going to need a place to hold the new string that I’m creating because strings are immutable, so I can’t change the original string.

04:33 I’m going to need a new one. And I need that one before I start doing anything in the for loop because otherwise, I would just keep overwriting the value. Yeah, that’s true.

04:43 Okay. Just back up a little bit. Like you created a new Python file, double.py, and that’s where I posted the instructions, and that’s where you are in right now. Right.

04:54 So basically the code that you’re having right now, you would just run the file and see like, if it works, if there are errors in this state. I’d open up my terminal and then I can say python—sorry I’m in this, I’m in the folder where I created the file

05:12 of double.py in this folder. And now I can say python3 double.py, and then it runs. It doesn’t bring any errors now, because I added the pass statement.

05:22 If I didn’t, then we’d end this with a SyntaxError. Yeah. I think this is sometimes at the beginning, a very good sign if a file does nothing. So sometimes at the beginning, there is an error thrown because there is a variable you are using that is undefined, or you’re importing maybe an external package that is not installed yet.

05:44 But like you just did, just running the file and not getting anything back is a good sign at the beginning. Right.

05:52 And I could do also something like put a placeholder print() in here or print each character while I’m iterating over it. So then looking at the output is a little more interesting because then you actually see that the loop is working, and it’s addressing each character separately.

06:10 Martin now is in the middle of solving the coding challenge. But before we continue, let’s recap a little bit how he tackled the coding challenge. He started by sorting his thoughts and communicated how he understands the challenge. That’s very vital because if you’re in a coding interview, for example, it’s very good that you’re on the same page like the interviewer.

06:32 So if you misunderstand the question, the coding interviewer can clarify a few things for you. You may ask questions if it wasn’t clear. In this case, Martin understood a coding challenge completely right, and he started coding.

06:46 That’s also a good thing to not think about it too much, but just get your feet wet and start coding. And also, be okay with not being perfect right away.

06:55 It’s better to start a little bit of code and get started instead of thinking of the perfect solution and trying to get there. But take one step after another.

07:06 So far, Martin coded everything plainly in the file. In the next part, he will use functions to structure the code, and he will also explain why functions are a good idea.

Become a Member to join the conversation.