My Coding Quiz #54

in voilk •  4 months ago

    My Coding Quiz #54 👨‍💻🛠️🧩

    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: NameError. Line 1 creates a zip object, which is useful for traversing two iterables in parallel. In this case it would be the string "abc" and the range(1,4) which, if resolved as a list, would consist of [1, 2, 3], so both iterables are the same size.

    Line 2 creates a dictionary d using the compression notation. We see that it would consist of key/value pairs formed by each parallel element traversed with the zip object, so if we were to print d, it would consist of {'a': 1, 'b': 2, 'c': 3}.

    Line 3 should show one result, whichever is truthy of the three values joined with the or operator. Let's look at each one:

    👉 list(c) will, in fact, produce the empty list [], since zip is like a generator. This means that once it is traversed, it is consumed. We see that it was traversed on line 2, so it is already consumed at this point and produces nothing. An empty list is falsy.

    👉 d.get(1) returns None since there is no key named 1 in the d dictionary. The None value is obviously falsy.

    👉 a, on the other hand, will trigger the NameError, since it is a local variable inside the compressor block. It does not exist outside it. This explains the result obtained, which is not really an output, but an execution error.

    What do you say, were you right with your choice?


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

    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: NameError. La línea 1 crea un objeto zip, que es útil para atravesar dos iterables en paralelo. En este caso sería la cadena "abc" y el range(1,4) que, si se resuelve como una lista, consistiría en [1, 2, 3], por lo que ambos iterables tienen el mismo tamaño.

    La línea 2 crea un diccionario d usando la notación de compresión. Vemos que consistiría en pares clave/valor formados por cada elemento paralelo atravesado con el objeto zip, por lo que si imprimiésemos d, consistiría en {'a': 1, 'b': 2, 'c': 3}.

    La línea 3 debería mostrar un resultado, el que sea verdadero (truthy) de los tres valores unidos con el operador or. Veamos cada uno:

    👉 list(c) producirá, de hecho, la lista vacía [], ya que zip es como un generador. Esto significa que una vez que se atraviesa, se consume. Vemos que fue recorrido por la línea 2, por lo que ya está consumido en este punto y no produce nada. Una lista vacía es falsa (falsy).

    👉 d.get(1) devuelve None ya que no hay ninguna clave denominada 1 en el diccionario d. El valor None es obviamente falso (falsy).

    👉 a, por otro lado, desencadenará el NameError, ya que es una variable local dentro del bloque compresor. No existe fuera de él. Esto explica el resultado obtenido, que en realidad no es una salida, sino un error de ejecución.

    ¿Qué dices, acertaste con tu elección?


    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.

      Authors get paid when people like you upvote their post.
      If you enjoyed what you read here, create your account today and start earning FREE VOILK!