My Coding Quiz #54

in #hive-1976858 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.

Sort:  

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!

Wow. This is nicee!
Coding quiz. Do you Put up questions on R language?

Thanks, do you have the answer to the quiz btw?
Maybe I also do quizzes with R. I'll take it as a suggestion!

No I don't, cause I am not familiar with python

This post has been manually curated by @bhattg from Indiaunited community. Join us on our Discord Server.

Do you know that you can earn a passive income by delegating to @indiaunited. We share more than 100 % of the curation rewards with the delegators in the form of IUC tokens. HP delegators and IUC token holders also get upto 20% additional vote weight.

Here are some handy links for delegations: 100HP, 250HP, 500HP, 1000HP.

image.png

100% of the rewards from this comment goes to the curator for their manual curation efforts. Please encourage the curator @bhattg by upvoting this comment and support the community by voting the posts made by @indiaunited.

Thank you!