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