A departure from David Mitchell’s fantasy genre, Black Swan Green (2006) is a semi-autobiographical novel that delves into the author’s struggle with stammering as a teenager. The novel is set in…
The aim of this challenge is to write code that can analyze code submissions. We’ll simplify things a lot to not make this too hard.
Write a function named validate
that takes code represented as a string as its only parameter.
Your function should check a few things:
the code must contain the def
keyword
the code must contain the :
symbol
the code must contain (
and )
for the parameter list
the code must not contain ()
the code must contain four spaces for indentation
the code must contain validate
the code must contain a return
statement
If all these conditions are satisfied, your code should return True
.
Here comes the twist: your solution must return True
when validating itself.
I immediately recognize that this challenge, with my skill level, is not able to be completed as as simple one-liner. For this, the first thing my mind jumps to is a ‘if’ statement chain. I run the string through multiple ‘if’ statements to check if the desired characters are present. If not, return the statement the challenge provides.
This was the first milestone of this challenge. I originally had typos between the “missing param” and “missing paren” returns, but after fixing, it still didn’t work. I double and tripled checked and the code should validate perfectly fine. I look back at the instructions and I forgot about the ‘twist’ of this challenge.
Ok. So what do we do here. I tried copy and pasting the entire code block into the function, but with quotations inside my code it messed with the variable, and I didn’t feel like scratching them all out to run it, so I just eyeballed it. I realized that every statement was a “if not” statement, except one.
This would cause a problem if I put the code to validate itself. By using this function to validate itself, it would detect the “()” inside of the function and return “missing param.” How annoying. I realized quickly I could rework how the string was written and still successfully check for “()” without having “()” directly.
My final function looked like this, which passed the verification and completed the challenge.
I didn’t learn anything new from this, but it definitely helped me think outside the box to find the problem and the solution, which is just as rewarding. Figuring out this problem felt good, as it didn’t require me to google any new ways to do something, just to find out what the problem was and find a way to efficiently fix it.
On a platform like Medium, we are exposed to a multitude of writing styles, subject matters, and formats and I thought it was time we celebrate our diversity a bit more here on The Honest…
It is known that the automotive sphere is not very fast in changes. Especially in such things as interfaces and instruments. But everything changed with the advent of Tesla Model S in 2012. The place…
Passara quarenta minutos desde que o último som de pancada fora ouvido. O porrete de Ernesto García encontrava-se ensanguentado. As peles dos animais caçados ao longo do dia acumulavam-se ao lado da…