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: [3, 1]. Line 1 specifies a list containing numeric elements and is called x. The same thing happens on the next line, except that the list is empty and is identified as y.
The heart of the matter is given in lines 3 and 4, where these two lists are iterated over and expanded with new elements. Specifically, the numeric sequence given to the for loop is defined as range(1, 5, 2). This means that it will consist of numbers between 1 and 5, but incrementing at the rate of 2, which is the third argument of the range.
In this way, the sequence starts at 1, increments to 3 and then stops, since it cannot increment to 5 because 5 is the specified stop and we know that this stop value is non-inclusive. The numbers returned by this range, therefore, will be 1 and 3, which are the values that the i variable of the for loop will take.
So, it's not hard to see that the y list will be expanded with the values of the x list at indices 1 and 3, and these values are 3 and 1 respectively. Hence [3, 1] is the output produced on the final line.
I'd say the gist of the puzzle is knowing the role of incrementing the range by 2, which is not difficult if you're familiar with Python. Once again, @rafaelaquino has solved this exercise. What a great ability to read and execute code in your head!
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: [3, 1]. La l铆nea 1 especifica una lista que contiene elementos num茅ricos y se llama x. Lo mismo pasa en la l铆nea siguiente, excepto que la lista est谩 vac铆a y se indentifica como y.
El meollo del asunto se da en las l铆neas 3 y 4, donde estas dos listas son iteradas y expandidas con nuevos elementos. Espec铆ficamente, la secuencia num茅rica dada al for est谩 definida como range(1, 5, 2). Esto significa que comprender谩 los n煤meros entre 1 y 5, pero incrementando a ritmo de 2, que es el tercer argumento del range.
De esta manera, la secuencia comienza en 1, incrementa a 3 y luego se detiene, pues no puede incrementar a 5 por cuanto 5 es el tope especificado y sabemos que este tope es no inclusivo. Los n煤meros retornados por este range, por tanto, ser谩n 1 y 3, que son los valores que tomar谩 la variable i del for.
As铆, no es dif铆cil ver que la lista y ser谩 expandida con los valores de la lista x en los 铆ndices 1 y 3, y estos valores son 3 y 1 respectivamente. De all铆 que [3, 1] sea la salida producida en la l铆nea final.
Dir铆a que la esencia del acertijo est谩 en conocer el papel de incremento en 2 del range, algo nada dif铆cil si est谩s familiarizado con Python. Una vez m谩s, @rafaelaquino ha resuelto este ejercicio. 隆Qu茅 buena habilidad para leer y ejecutar c贸digo mentalmente!
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.