My Coding Quiz #51

in #hive-1976859 months ago

My Coding Quiz #51 👨‍💻🛠️🧩

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: ['ETH', 'ADA', 'SOL']. To illustrate the Python concept of this edition, we have used a data about cryptocurrencies. Well, it's just a string called cryp that lists five famous cryptocurrencies:

1/Bitcoin/BTC
2/Ethereun/ETH
3/Cardano/ADA
4/Solana/SOL
5/Hive/HIVE


We see that it follows a uniform format, which is very common and makes it easy to process. The interesting thing happens when we apply common operations to process these strings and extract their data into appropriate data structures. That's what line 2 does: f = (l.split("/")[-1] for l in cryp.split("\n")). The parenthesised syntax does not create a list by compression, but a generator.

Closely we see that we traverse each line of the string, which conceptually here are equivalent to records. Then we create fields in each record by separating the strings by "/", keeping only the last field. In other words, we are left with only the token for each cryptocurrency, so f is a generator that, if converted to a list, would reference these values: ['BTC', 'ETH', 'ADA', 'SOL', 'HIVE'].

Then, in line 3 we have x, *_, y = f. This is an unpacking: the variable x takes the first value produced by the generator, while y takes the last one. In turn, *_ takes everything in between. The floor symbol (_) is a valid identifier and is common to denote anonymous variables. Placing the asterisk next to it here is necessary so that multiple values are unpacked and not just one. It should be noted that this operation creates a list, so _ contains ['ETH', 'ADA', 'SOL'], which is printed by the final instruction.


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

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: ['ETH', 'ADA', 'SOL']. Para ilustrar el concepto de Python de esta edición, hemos utilizado datos sobre criptomonedas. Bueno, es sólo una cadena llamada cryp que enumera cinco criptomonedas famosas:

1/Bitcoin/BTC
2/Ethereun/ETH
3/Cardano/ADA
4/Solana/SOL
5/Hive/HIVE


Vemos que sigue un formato uniforme, que es muy común y facilita su procesamiento. Lo interesante sucede cuando aplicamos operaciones comunes para procesar estas cadenas y extraer sus datos en estructuras de datos apropiadas. Eso es lo que hace la línea 2: f = (l.split("/")[-1] for l in cryp.split("\n")). La sintaxis entre paréntesis no crea una lista por compresión, sino un generador.

De cerca vemos que recorremos cada línea de la cadena, que conceptualmente aquí equivalen a registros. Luego creamos campos en cada registro separando las cadenas por "/", manteniendo solo el último campo. En otras palabras, solo nos queda el token para cada criptomoneda, por lo que f es un generador que, si se convierte en una lista, haría referencia a estos valores: ['BTC', 'ETH ', 'ADA', 'SOL', 'HIVE'].

Entonces, en la línea 3 tenemos x, *_, y = f. Esto es un desempaquetado: la variable x toma el primer valor producido por el generador, mientras que y toma el último. A su vez, *_ toma todo lo que hay en el medio. El símbolo de piso (_) es un identificador válido y es común para indicar variables anónimas. Es necesario colocar el asterisco al lado aquí para que se descompriman varios valores y no solo uno. Cabe señalar que esta operación crea una lista, por lo que _ contiene ['ETH', 'ADA', 'SOL'], que se imprime en la instrucción final.


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

Congratulations @eniolw! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You have been a buzzy bee and published a post every day of the month.
You have been a buzzy bee and published a post every day of the week.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

Rebuilding HiveBuzz: The Challenges Towards Recovery