My Coding Quiz #57

in Indiaunited12 days ago (edited)

My Coding Quiz #57 👨‍💻🛠️🧩

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: <class 'set'> False. One more time we've used chess motifs to illustrate the concept of this code. This is how we created the set moves, which contains characters that represent moves in a chess game: moves = {'e4', 'c5', 'd4', 'e4', 'cxd4'}.

As we know, sets have unique elements, so the repeated element that was hardcoded ('e4') is actually eliminated during the compilation and execution of the program. So, the set moves would actually consist of {'c5', 'd4', 'e4', 'cxd4'}.

Next, we create a list numbers with a sequence of natural numbers that correspond to the elements of the set moves one by one. This is useful to be able to enumerate the moves. In chess, the movements of a game are numbered in pairs (one white move and one black move represent a single pair), but at the programming level, each move corresponds to a single number. In case you need to see it, the content of numbers is [0, 1, 2, 3].

Then we have the instruction jug = [j1, j2, j3, j4] = moves, which is curious and potentially confusing. There we make a chained assignment or mapping. Reading from left to right, we have [j1, j2, j3, j4] = moves and this is an unpacking: we are creating four variables and assigning to them items from moves in the order in which they appear; We are not creating a list.

That explains what occurs in the following part: jug = [j1, j2, j3, j4]. Here jug does not refer to any list, but directly to moves.

Finally we use print to show the output: print(type(jug), {numbers[0]} < {numbers[1]}). The first part produces the type of jug, which is a set.

The following part in the print deserves its own discussion: We are doing one of the multiple set operations. In this case, the < operator serves to check whether a set is a proper subset of another. Here {numbers[0]} < {numbers[1]} consists of {0} < {1}. Even though the value of the element of the first set is smaller than that of the second, this is not how this operation works. The truth value here is False because {0} is not a subset of {1}.

That explains why the final output is <class 'set'> False. As you can see, even though it looks like a simple script, it contains several interesting concepts and details that even take time to understand and explain.


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

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: <class 'set'> False. Nuevamente utilizamos motivos de ajedrez para ilustrar el concepto de este código. Así es como creamos el conjunto moves, que contiene caracteres que representan movimientos en un juego de ajedrez: moves = {'e4', 'c5', 'd4', 'e4', ' cxd4'}.

Como sabemos, los conjuntos tienen elementos únicos, por lo que el elemento repetido que fue codificado ('e4') en realidad se elimina durante la compilación y ejecución del programa. Entonces, el conjunto moves en realidad consistiría en {'c5', 'd4', 'e4', 'cxd4'}.

A continuación, creamos una lista numbers con una secuencia de números naturales que corresponden a los elementos del conjunto moves uno por uno. Esto es útil para poder enumerar los movimientos. En el ajedrez, los movimientos de una partida se numeran por parejas (una jugada de las blancas y otra de las negras representan un único par), pero a nivel de programación, cada jugada corresponde a un único número. En caso de que necesites verlo, el contenido de números es [0, 1, 2, 3].

Luego tenemos la instrucción jug = [j1, j2, j3, j4] = moves, que es curiosa y potencialmente confusa. Allí realizamos una asignación o mapeo encadenado. Leyendo de izquierda a derecha, tenemos [j1, j2, j3, j4] = moves y esto es un desempaquetado: estamos creando cuatro variables y asignándoles elementos de moves en el orden en que aparecen; no estamos creando una lista.

Eso explica lo que ocurre en la siguiente parte: jug = [j1, j2, j3, j4]. Aquí jug no se refiere a ninguna lista, sino directamente a movimientos.

Finalmente usamos print para mostrar el resultado: print(type(jug), {numbers[0]} < {numbers[1]}). La primera parte produce el tipo de jug, que es un set.

La siguiente parte de la impresión merece su propia discusión: Estamos realizando una de las múltiples operaciones de conjunto. En este caso, el operador < sirve para comprobar si un conjunto es un subconjunto propio de otro. Aquí {numbers[0]} < {numbers[1]} consta de {0} < {1}. Aunque el valor del elemento del primer conjunto es menor que el del segundo, esta operación no funciona así. El valor de verdad aquí es False porque {0} no es un subconjunto de {1}.

Eso explica por qué el resultado final es <class 'set'> False. Como puedes ver, aunque parece un script sencillo, contiene varios conceptos y detalles interesantes que incluso llevan tiempo comprender y explicar.


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:  

Hey! What an awesome find today! You're doing dev quizzes. I definitely need to come back here. I'm just in my feed here today finding yours.

On the quiz above I have no clou yet and look forward to your solution in #58 hopefully. Would love to have me mentioned for not missing out. Your lesson reminds me of going into regular expressions more, yet having little reason in a usual day in and day out as not being a dev yet.

Make this a great day!

!invest_vote

Hey! Thank you for stopping by! I hope you like my coding series. Are you learning coding? If you follow me, I think you should get updates about my posts in your feed.

No, I'm not learning to code. I've tried to go into it, though never had the patience to dive very deep. I'm more of an admin then a coder.

My feed is what led me here, yet that's not very reliable as I don't often can afford days like today. !LOLZ So I miss a lot that is in my feed.

Yet I now have added your account to my F.R.I.D.A.Y and look forward to more of your quizzes! 😀👍

!invest_vote

Yes, I get that the feed sometimes gets messy. Thank you!

In my past life I was a turtle.
It is slowly coming back to me.

Credit: reddit
@eniolw, I sent you an $LOLZ on behalf of anli

(2/2)

PLAY & EARN $DOOM

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

Gracias!