My Coding Quiz #59 👨💻🛠️🧩
Welcome to the new installment of my series of Coding Quizzes, in which you will be able to test your knowledge and skills about programming and software development in a simple and fun way. If you want to learn more about it visit my blog here on Hive and the first post where I introduced it.
Without further ado, here's the riddle...
By @eniolw
What's your choice?
Solution to the previous quiz: <class 'str'> In line 1 we have created three variables knight
, bishop
and pawn
using unpacking notation. We see that we have assigned strings to them. It is a deliberately broken string, but we know that Python concatenates and treats it as a single string. From there we see that knight
stores 'n'
, bishop
, 'b'
and pawn
, ' '
.
In English algebraic chess notation, the N represents a knight, while the B represents a bishop. Pawns have no associated letter. Hence we have put a blank space.
Line 2 makes a selection. Variable piece
will have the value of pawn
, since it is truthy. Even though it is a blank space, it is still considered truthy. You didn't think it wasn't, did you? Or that it was compressed as an empty string? Neither is the case. In any case, if it had been None, it would not have been selected, because the or
operator looks for truthy values.
Then line 3 does something more curious. It assigns to type
the type of piece
, which is string. Why doesn't this generate an error? Because type
is not a reserved word, but a semi-reserved or soft reserved word. It can be reassigned as it happens here, but this is not recommended, of course. Something similar happens with sum
and other built-in functions and names.
At the end it is printed <class 'str'>, which is what type
is assigned, without generating exceptions.
If you want to blog about computer science and programming content, I invite you to join Hive and participate in its communities, such as STEM-social, Develop Spanish, Programming & Dev and others.
Mi Quiz de Programación #59 👨💻🛠️🧩
Bienvenido a mi nueva serie de Quizzes de Programación, en la cual podrás poner a prueba tus conocimientos y habilidades sobre programación y desarrollo de software de una manera sencilla y divertida. Si quieres aprender más sobre ella visita mi blog aquí en Hive y el primer post donde la presenté.
Sin más preámbulos, he aquí el acertijo...
Por @eniolw
¿Cuál es tu elección?
Solución al quiz anterior: <class 'str'>. En la línea 1 hemos creado tres variables knight
, bishop
y pawn
usando notación de desempaquetado. Vemos que les hemos asignado cadenas. Es una cadena deliberadamente rota, pero sabemos que Python la concatena y la trata como una sola cadena. A partir de ahí vemos que knight
almacena 'n'
, bishop
, 'b'
y pawn
, ' '
.
En la notación algebraica de ajedrez inglesa, la N representa un knight (caballo), mientras que la B representa un bishop (alfil). Los peones no tienen letra asociada. Por eso hemos puesto un espacio en blanco.
La línea 2 hace una selección. La variable piece
tendrá el valor de pawn
, ya que es veraz. Aunque sea un espacio en blanco, todavía se considera veraz. No pensaste que no lo era, ¿verdad? ¿O que estaba comprimido como una cadena vacía? Tampoco es el caso. En cualquier caso, si hubiera sido None, no se habría seleccionado, porque el operador o
busca valores veraces.
Luego la línea 3 hace algo más curioso. Asigna a type
el tipo de piece
, que es string. ¿Por qué esto no genera un error? Porque type
no es una palabra reservada, sino una semi-reservada o una palabra reservada suave. Se puede reasignar como ocurre aquí, pero esto no es recomendable, por supuesto. Algo similar sucede con sum
y otras funciones y nombres integrados.
Al final se imprime <class 'str'>, que es lo que se asigna type
, sin generar excepciones.
Si quieres bloguear sobre contenido informático y de programación, te invito a unirte a Hive y participar en sus comunidades, tales como STEM-social, Develop Spanish, Programming & Dev y otras.
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).
You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.
@tipu curate 8
Upvoted 👌 (Mana: 0/70) Liquid rewards.