My Coding Quiz #15

Words
739
Reading
4 min
Listen
Play
3y

My Coding Quiz #15 馃懆鈥嶐煉火煕狅笍馃З

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@eniolw


What's your choice?

Solution to the previous quiz: 1. Line 1 creates the variables a and b using the unpacking notation, something very well known and practical in Python that resembles to the destructuring of Javascript.

Then we modify the value of b in line 2 in a slightly tricky way, but it serves to show how we can use the - operator to change the sign of the value of numeric variables. The expression is equivalent to b = (-1) - (-3), which is b = -1 + 3, which is b = 2 .

Line 3 presents the most interesting and least known aspect of the exercise. In Python, the ^ operator is not the exponentiation operator; that would be ** in Python. If that was what you had in mind, then you would have chosen 9 as the outcome, but that was a red herring.

Instead, ^ is a bitwise exclusive-OR operator, equivalent to the XOR function. In a nutshell, it compares each bit of both operands and returns one (1) if only one of the two compared bits is 1 or returns zero (0) otherwise.

However, we see that the numbers supplied to the ^ operator in the given code are base-decimal integers, not binary, so Python converts them to binary in order to XOR them. The result of the 3 ^ 2 operation is therefore 1, which is the value assigned to c and printed at the final line.

This exercise involved hibadurahmon@hibadurahmon, splash-of-angs63@splash-of-angs63 and rafaelaquino@rafaelaquino, with splash-of-angs63@splash-of-angs63 providing an explanation of why the output was obtained. Very good!


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, Hive Learners and others.


Mi Quiz de Programaci贸n #15 馃懆鈥嶐煉火煕狅笍馃З

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@eniolw


驴Cu谩l es tu elecci贸n?

Soluci贸n al quiz anterior: 1. Las l铆nea 1 crea las variables a y b usando la notaci贸n de desempaquetado de secuencias, algo muy bien conocido y pr谩ctico en Python y que se asemeja al destructuring de Javascript.

Luego modificamos el valor de b en la l铆nea 2 de una manera un poco truquera, pero que sirve para poner en evidencia c贸mo podemos usar el operador - para cambiar el signo del valor de las variables num茅ricas. La expresi贸n es equivalente a b = (-1) - (-3), lo cual es b = -1 + 3, lo cual es b = 2.

La l铆nea 3 presenta el aspecto m谩s interesante y menos conocido del ejercicio. En Python, el operador ^ no es el de exponenciaci贸n; ese ser铆a ** en Python. Si eso era lo que ten铆as en mente, entonces habr铆as escogido el resultado 9, pero era una pista falsa.

En su lugar, ^ es un operador bitwise de 脫 exclusiva, equivalente a la funci贸n XOR. En palabras puntuales, esta compara cada bit de ambos operandos y devuelve uno (1) si solo uno de los dos bits comparados es 1 o devuelve cero (0) en caso contrario.

Sin embargo, vemos que los n煤meros provistos al operador ^ en el c贸digo dado son enteros de base decimal, no binarios, por lo Python los convierte a binarios para poder aplicarles la funci贸n XOR. El resultado de la operaci贸n 3 ^ 2, por tanto, es 1, que es valor asignado a c e impreso en la l铆nea final.

En este ejercicio participaron hibadurahmon@hibadurahmon, splash-of-angs63@splash-of-angs63 y rafaelaquino@rafaelaquino, con splash-of-angs63@splash-of-angs63 proveyendo una explicaci贸n del porqu茅 la salida obtenida. 隆Muy bien!


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, Hive Learners y otras.

My Coding Quiz #15 | Ecency