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: 1. This is a bit tricky in that we are dealing with an array of string elements, not numeric elements, which is what is declared in line 1. Then in line 2 we create a b identifier that references the identifier a, so a points to the same memory address as b. However, in line 3, b changes reference and now points to a new object, but doesn't overwrite what it previously pointed to. The references issue could be considered a red herring, although if you had any doubts when analyzing this, it means you should probably brush up on it!
Now this is the tricky part, because the object returned in line 3 is an ordered array of strings. This is: ['-0', '-1', '1', '2']. String ordering is lexicographical, so it does not behave the same as numeric ordering. To understand this, imagine that you are asked to sort this array ["Shanon", "Turing", "Church"]. Surely you will know how to order it by comparing each position of a string with its equivalent in the others (this one is rather easy).
By applying this to the original ['2', '1', '-1', '-0'] array, we'll figure out the trick. It is important to know that the character '-' is less than the characters '1' and '2', so it is sorted first. Then, to break a tie between '-0' and '-1', the second position of both strings is compared. It turns out that '0' is less than '1', so it is sorted first. Hence, ['-0', '-1', '1', '2'] is the final result of the lexicographical ordering.
Finally, in line 4 the conversions are made to integer values and we have: 2 + -1, which is 2 - 1 which is 1. This one managed to trick all the participants this time!
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 even Hive Learners.
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: 1. Este es un poco enga帽oso por cuanto estamos lidiando con un arreglo de cadenas de caracteres, no de n煤meros, que es lo que se declara en la l铆nea 1. Luego, en la l铆nea 2 se crea un indentificador b que hace referencia al identificador a, de modo que a apunta a la misma direcci贸n de memoria que b. Sin embargo, en la l铆nea 3, b cambia de referencia y ahora apunta a un nuevo objeto, m谩s no sobreescribe lo que anteriormente apuntaba. El asunto de las referencias podr铆a ser considerado una pista falsa, aunque si tuviste dudas, significa que probablemente deber铆as repasar este asunto!
Ahora bien, esta es la parte complicada, pues el objeto retornado en la l铆nea 3 es un arreglo de cadenas de caracteres ordenado. Este es: ['-0', '-1', '1', '2']. El ordenamiento de caracteres es lexicogr谩fico, por lo que no se comporta igual que el ordenamiento num茅rico. Para entender esto, imagina que se pide ordenar este arreglo ["Shanon", "Turing", "Church"]. Seguro sabr谩s ordenarlo comparando cada posici贸n de una cadena de caracteres con su equivalente en las dem谩s (en realidad este ejemplo es muy f谩cil).
Aplicando esto al arreglo original ['2', '1', '-1', '-0'], descubriremos el truco. Importa saber que el caracter '-' es menor que los caracteres '1' y '2', por lo que se ordena de primero. Luego, para desempatar entre '-0' y '-1', se compara la segunda posici贸n de ambas cadenas. Resulta que '0' es menor que '1', por lo que se ordena de primero. De all铆 que ['-0', '-1', '1', '2'] sea el resultado final del ordenamiento lexicogr谩fico.
Finalmente, en la l铆nea 4 se hacen las conversiones a valores enteros y se tiene: 2 + -1, que es 2 - 1 que es 1. 脡ste ha conseguido enga帽ar a todos los participantes esta vez.
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 e incluso Hive Learners.