My Coding Quiz #50

in #hive-1976857 months ago

My Coding Quiz #50 👨‍💻🛠️🧩

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...




Quiz
By @eniolw


What's your choice?

Solution to the previous quiz: None. This quiz is a small nod to one of my favourite games after chess: The Legend of Zelda. First we define two sets: player1 and player2, each containing titles from famous video games of this franchise.

However, it is in line 10 where we see the heart of the matter: played = player1.intersection_update(player2). What the intersection_update method does is to select the common elements of both sets and overwrite the first one with those elements. So, if we print the content of player1 we will see that it consists of {"Majora's Mask"}, as it is the only element that both sets have in common.

Now, don't be confused by the way the two common strings were hardcoded in the script: "Majoras's Mask" (line 3) is exactly the same as 'Majora\'s Mask' (line 8), because in the second one, the backslash followed by ' is one of the many Python escape sequences and serves to use that character inside a string delimited with that same character, making the notation legal.

But why played is None then? Because contrary to what you might think, the intersection_update method does not return a new set, but just updates one of the existing sets, so that method actually returns None. This is a misleading use, but not in vain, as it is a way to make a potential error explicit.

Majoras's Mask, by the way, is my favourite of all the Zelda games I've played. How about you?


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 #50 👨‍💻🛠️🧩

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...




Quiz
Por @eniolw


¿Cuál es tu elección?

Solución al quiz anterior: None. Este quiz es un pequeño guiño a uno de mis juegos favoritos después del ajedrez: The Legend of Zelda. Primero definimos dos conjuntos: player1 y player2, cada uno de los cuales contiene títulos de videojuegos famosos de esta franquicia.

Sin embargo, es en la línea 10 donde vemos el meollo del asunto: played = player1.intersection_update(player2). Lo que hace el método intersection_update es seleccionar los elementos comunes de ambos conjuntos y sobrescribir el primero con esos elementos. Entonces, si imprimimos el contenido de player1 veremos que consta de {"Majora's Mask"}, ya que es el único elemento que ambos conjuntos tienen en común.

Ahora bien, no te dejes confundir por la forma en que las dos cadenas comunes estaban codificadas en el script: "Majoras's Mask" (línea 3) es exactamente igual que 'Majora\'s Mask' (línea 8), porque en la segunda, la barra invertida seguida de ' es una de las muchas secuencias de escape de Python y sirve para usar ese carácter dentro de una cadena delimitada con ese mismo carácter, lo que hace que la notación sea legal.

Pero ¿por qué entonces played es None? Porque, contrariamente a lo que podría pensar, el método intersection_update no devuelve un nuevo conjunto, sino que simplemente actualiza uno de los conjuntos existentes, por lo que ese método en realidad devuelve None. Este es un uso engañoso, pero no en vano, ya que es una forma de hacer explícito un posible error.

Majoras's Mask, por cierto, es mi favorito de todos los juegos de Zelda que he jugado. ¿Y tú?


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.

Sort:  

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.