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