My Coding Quiz #60

avatar

My Coding Quiz #60 👨‍💻🛠️🧩

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: ['pin', 'fork', 'skewer', []] We start by creating two lists tactics1 and tactics2 containing strings with the names of famous chess tactical themes.

Then, line 3 creates a new list t by importing the contents of tactics1 using the asterisk operator (*) and a reference to the whole list tactics2. Both operations are different.

To add more confusion or curiosity, at the end of that line is appended [:], which slices the contents of the newly created list from end to end and this is what is assigned to t. What is so special about this? It is to create a copy, but we will see its real effect later.

Line 4 applies the del operator to delete the entire contents of tactics1 and tactics2. This happens because we have used the slice notation again ([:]). If we had not done this, del would have deleted both identifiers, making their use impossible from that point in the code. Here instead, we just remove the contents of the lists, preserving the identifiers.

Finally, the last line prints the contents of t, which is ['pin', 'fork', 'skewer', []]. This is because the items 'pin', 'fork', 'skewer' were copied as new items from tactics1 in line 3, but the list [] is empty because tactics2 was just referenced. Even though we included a slice operator at the end ([:]), this does not create nested or deep copies; it made no difference. That explains why the list is empty, because it was deleted by del, since we always had the same list referenced everywhere in the script.


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

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: ['pin', 'fork', 'skewer', []]. Comenzamos creando dos listas tactics1 y tactics2 que contienen cadenas con los nombres de temas tácticos de ajedrez famosos.

Luego, la línea 3 crea una nueva lista t importando el contenido de tactics1 usando el operador de asterisco (*) y una referencia a toda la lista tactics2. Ambas operaciones son diferentes.

Para agregar más confusión o curiosidad, al final de esa línea se agrega [:], que rebana el contenido de la lista recién creada de un extremo a otro y esto es lo que se asigna a t. ¿Qué tiene de especial esto? Es para crear una copia, pero veremos su efecto real más adelante.

La línea 4 aplica el operador del para eliminar todo el contenido de tactics1 y tactics2. Esto sucede porque hemos usado la notación de rebanada nuevamente ([:]). Si no hubiéramos hecho esto, del habría eliminado ambos identificadores, imposibilitando su uso a partir de ese punto del código. Aquí, en cambio, simplemente eliminamos el contenido de las listas, preservando los identificadores.

Finalmente, la última línea imprime el contenido de t, que es ['pin', 'fork', 'skewer', []]. Esto se debe a que los elementos 'pin', 'fork', 'skewer' se copiaron como elementos nuevos de tactics1 en la línea 3, pero la lista [] está vacío porque solo se hizo referencia a tactics2. Aunque incluimos un operador de rebanada al final de esa línea ([:]), esto no crea copias anidadas o profundas; no hizo ninguna diferencia. Eso explica por qué la lista está vacía, porque fue eliminada por del, ya que siempre tuvimos referencia a la misma lista en todas partes del script.


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.



0
0
0.000
7 comments
avatar

Congratulations @eniolw! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You have been a buzzy bee and published a post every day of the week.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

0
0
0.000
avatar

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. 
 

0
0
0.000