My Coding Quiz #5

avatar
(Edited)

My Coding Quiz #5 👨‍💻🛠️🧩

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


What's your choice?

Solution to the previous quiz: 14. Lines 1 and 2 define a pair of lambda functions. Lambda is a keyword in the Python language for creating short functions intended to be used in-line, which enriches the language with functional programming.

If you didn't know, the reason for naming these functions Lambda is in honor of the Lambda Calculus, which was invented by theoretical computer scientist Alonzo Church to mathematically ground the theory of computation, something Alan Turing would do as well, but using a totally different model known as Turing machines.

Let's go back to the solution. It all makes sense when these functions are called in line 3 in a slightly obscure way, which is evidently intentional: res = f1(f2(-4), 8). From here, we first resolve the call to the function f2, defined in line 2. This receives -4 and returns its square, that is, 16.

Then we solve the call to function f1, defined in line 1, which receives 16 and 8 as arguments. It executes the operation 16 - 8 / 4 which produces 14 (don't fall into the operator precedence trap, where 8 / 4 should be executed first because it is a division). An unnecessary rounding with round is applied to this value.

So 14 is the value assigned to res and printed at the end. It seems that only @zuerich managed to solve the quiz correctly this time. Congratulations!


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

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


¿Cuál es tu elección?

Solución al quiz anterior: 14. Las líneas 1 y 2 definen un par de funciones lambda. Lambda es una palabra clave en el lenguaje Python para crear funciones cortas destinadas a ser utilizadas de manera in-line, lo que enriquece el lenguaje con programación funcional.

Si no lo sabías, la razón para llamar a estas funciones Lambda es en honor al Cálculo lambda, que fue inventado por el informático teórico Alonzo Church para fundamentar matemáticamente la teoría de la computación, algo que Alan Turing haría también, pero usando un modelo totalmente diferente conocido como máquinas de Turing.

Volvamos a la solución. Todo tiene sentido cuando estas funciones se llaman en la línea 3 de una manera un poco oscura, lo cual es evidentemente intencional: res = f1(f2(-4), 8). A partir de aquí, primero resolvemos la llamada a la función f2, definida en la línea 2. Esta recibe -4 y devuelve su cuadrado, es decir, 16.

Luego resolvemos la llamada a la función f1, definida en la línea 1, que recibe 16 y 8 como argumentos. Ejecuta la operación 16 - 8 / 4 que produce 14 (no caigas en la trampa de precedencia de operadores, donde 8 / 4 se debe ejecutar primero porque es una división). Se aplica un redondeo innecesario con round a este valor.

Entonces 14 es el valor asignado a res e impreso al final. Parece que solo @zuerich logró resolver el cuestionario correctamente esta vez. ¡Felicidades!


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.



0
0
0.000
7 comments
avatar
(Edited)

I started running the code of your quizzes instead of guessing because they're harder than they look. But there's one important thing I learned about Python: while values have a type, variables don't. They're just references to a value object.

This is more logical and convenient than the variable types that other languages have, in my opinion. I didn't even know you could initialize variables with a type.

0
0
0.000
avatar

It's great that it made you think and you're learning things. That's part of the point! I'll talk more about the type issue in the next quiz.

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