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: stem. First we create a string base that has several unintelligible characters, but that doesn't matter. Line 2 converts them into a list using the powerful Python list comprehension mechanism. That list in turn is transformed into a set in line 3 using the set constructor. A set is, simply put, an unordered collection of unique elements.
In our case, the set a will be formed by all the characters in base, noting that as the character 'h' is duplicated in base, it will not be duplicated in a, since sets do not have duplicates by definition.
Then, line 4 presents the difference operation that applies to two sets. We see that it applies to a and b. The way to read that instruction is which elements are in a that are not in b. We have that:
a = {'m', 'c', 'e', 'n', 'a', 'h', 't', 's', 'i'}
And
b = {'c', 'n', 'h', 'a', 'i'}
The difference a - b is the set:
c = {'s', 'm', 't', 'e'}
The object returned by difference is a new set. In the final line, the set c is converted to a list and then converted to a string using join. What is printed is an anagram of 'stem', that is, a word with the same letters that stem has.
Enthusiast @gabrielr29 came up with the right answer. 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.
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: stem. Primero creamos una cadena base que tiene varios caracteres ininteligibles, pero eso no importa. La l铆nea 2 los convierte en una lista utilizando el poderoso mecanismo de comprensi贸n de listas de Python. Esa lista a su vez se transforma en un conjunto en la l铆nea 3 usando el constructor set. Un conjunto es, en pocas palabras, una colecci贸n desordenada de elementos 煤nicos.
En nuestro caso, el conjunto a estar谩 formado por todos los caracteres de base, teniendo en cuenta que como el car谩cter 'h' es duplicado en base, no se duplicar谩 en a, ya que los conjuntos no tienen duplicados por definici贸n.
Luego, la l铆nea 4 presenta la operaci贸n de diferencia que se aplica a dos conjuntos. Vemos que se aplica a a y b. La forma de leer esa instrucci贸n es qu茅 elementos est谩n en a y no en b. Tenemos que:
a = {'m', 'c', 'e', 'n', 'a', 'h', 't', 's', 'i'}
Y
b = {'c', 'n', 'h', 'a', 'i'}
La diferencia a - b es el conjunto:
c = {'s', 'm', 't', 'e'}
El objeto devuelto por difference es un conjunto nuevo. En la l铆nea final, el conjunto c se convierte en una lista y luego se convierte en una cadena usando join. Lo que est谩 impreso es un anagrama de 'stem', es decir, una palabra con las mismas letras que tiene stem.
El entusiasta @gabrielr29 dio con la respuesta correcta. Felicitaciones.
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.