create account

My Coding Quiz #55 by eniolw

View this thread on: hive.blogpeakd.comecency.com
· @eniolw ·
$12.59
My Coding Quiz #55
<div class="text-justify">

<center><h2>My Coding Quiz #55 👨‍💻🛠️🧩</h2></center>

<p>Welcome to the new installment of my series of <b>Coding Quizzes</b>, 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 <a href="/@eniolw">my blog</a> here on Hive and the <a href="/@eniolw/my-coding-quiz-1">first post</a> where I introduced it.</p>

<h3>Without further ado, here's the riddle...</h3>

<br>
<center>
<img src="https://images.ecency.com/DQmNrebfSsoYDMwCn4GExC8qq6Qo84AS3PiadgR5uXW1agK/imagen.png" alt="Quiz">
<h6>By @eniolw</h6>
</center>

<br><center><h3>What's your choice?</h3></center>

<p><b>Solution to the <a href="/@eniolw/my-coding-quiz-54">previous quiz</a>:</b> <b>[{'checkmate': True}, {'checkmate': True}]</b>. Line 1 creates a list <code>a</code> with an empty dictionary. However, by coding <code>* 2</code>, we have duplicated the contents of the list with two dictionaries, but in a way that will be relevant later.

In line 2 we do the same thing. We see that the commutative property applies without problems, but here the list with elements to replicate is empty, so that actually <code>b</code> consists of an empty list.

Line 3 is more complex: <code>a[len(b) - 1].update({'checkmate': True})</code>. We see that what we do is to update the dictionary contained in the position <code>len(b) - 1</code>, which is <b>-1</b>, since we already saw that <code>b</code> is an empty list. Therefore, when accessing <code>a[-1]</code>, what we do is to access the last position of the list <code>a</code>, which contains a dictionary.

Therefore, we update its content with the hardcoded dictionary <code>{'checkmate': True}</code>. However, when executing the last line of the script, we see that both dictionaries in list <code>a</code> have the same content and that is because when we did the duplication in line 1, <i>we made copies by reference</i>; something that is expected with mutable and complex objects such as dictionaries. In other words, both elements of the list, which are dictionaries, are the same. They both have references to the same dictionary.

This could be overlooked by an unsuspecting programmer using that notation in line 1. In short, you should know that it does not create a new dictionary, but creates as many references to the same dictionary as the number of list items you want.</p>

<hr>

<p>If you want to blog about computer science and programming content, I invite you to join <a href="/">Hive</a> and participate in its communities, such as <a href="/created/hive-196387">STEM-social</a>, <a href="/created/hive-154226">Develop Spanish</a>, <a href="/created/hive-169321">Programming & Dev</a> and others.</p>

<hr>

<center><h2>Mi Quiz de Programación #55 👨‍💻🛠️🧩</h2></center>

<p>Bienvenido a mi nueva serie de <b>Quizzes de Programación</b>, 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 <a href="/@eniolw">mi blog</a> aquí en Hive y el <a href="/@eniolw/my-coding-quiz-1">primer post</a> donde la presenté.</p>

<h3>Sin más preámbulos, he aquí el acertijo...</h3>

<br>
<center>
<img src="https://images.ecency.com/DQmev4bEjYupzdx2cpiFYa3ePFDEnLmXfYjoQDGgn7LranZ/imagen.png" alt="Quiz">
<h6>Por @eniolw</h6>
</center>

<br><center><h3>¿Cuál es tu elección?</h3></center>

<p><b>Solución al <a href="/@eniolw/my-coding-quiz-54">quiz anterior</a>:</b> <b>[{'checkmate': True}, {'checkmate': True}]</b>. La línea 1 crea una lista <code>a</code> con un diccionario vacío. Sin embargo, al codificar <code>* 2</code>, hemos duplicado el contenido de la lista con dos diccionarios, pero de una manera que será relevante más adelante.

En la línea 2 hacemos lo mismo. Vemos que la propiedad conmutativa se aplica sin problemas, pero aquí la lista con elementos a replicar está vacía, por lo que en realidad <code>b</code> consta de una lista vacía.

La línea 3 es más compleja: <code>a[len(b) - 1].update({'checkmate': True})</code>. Vemos que lo que hacemos es actualizar el diccionario contenido en la posición <code>len(b) - 1</code>, que es <b>-1</b>, ya que ya vimos que <code>b </code> es una lista vacía. Por tanto, al acceder a <code>a[-1]</code>, lo que hacemos es acceder a la última posición de la lista <code>a</code>, que contiene un diccionario.

Por lo tanto, actualizamos su contenido con el diccionario codificado <code>{'checkmate': True}</code>. Sin embargo, al ejecutar la última línea del script, vemos que ambos diccionarios en la lista <code>a</code> tienen el mismo contenido y eso se debe a que cuando hicimos la duplicación en la línea 1, <i>hicimos copias por referencia</i>; algo que se espera con objetos mutables y complejos como los diccionarios. Es decir, ambos elementos de la lista, que son diccionarios, son iguales. Ambos tienen referencias al mismo diccionario.

Esto podría ser pasado por alto por un programador desprevenido que use esa notación en la línea 1. En resumen, debes saber que no se crea un nuevo diccionario, sino que crea tantas referencias al mismo diccionario como el número de elementos de la lista que desees.</p>

<hr>

<p>Si quieres bloguear sobre contenido informático y de programación, te invito a unirte a <a href="/">Hive</a> y participar en sus comunidades, tales como <a href="/created/hive-196387">STEM-social</a>, <a href="/created/hive-154226">Develop Spanish</a>, <a href="/created/hive-169321">Programming & Dev</a> y otras.</p>

</div>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 576 others
properties (23)
authoreniolw
permlinkmy-coding-quiz-55
categoryhive-197685
json_metadata"{"image":["https://images.ecency.com/DQmNrebfSsoYDMwCn4GExC8qq6Qo84AS3PiadgR5uXW1agK/imagen.png","https://images.ecency.com/DQmev4bEjYupzdx2cpiFYa3ePFDEnLmXfYjoQDGgn7LranZ/imagen.png"],"tags":["hive-197685","programming","coding","python","javascript","chess","quiz","neoxian","proofofbrain","waivio","spanish"],"description":"My Coding Quiz #55 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 fun way.","app":"ecency/3.1.1-vision","format":"markdown+html","image_ratios":["1.2285","1.2285"]}"
created2024-03-29 02:16:39
last_update2024-03-29 02:16:39
depth0
children12
last_payout2024-04-05 02:16:39
cashout_time1969-12-31 23:59:59
total_payout_value6.358 HBD
curator_payout_value6.233 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,622
author_reputation200,516,207,918,518
root_title"My Coding Quiz #55"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,420,928
net_rshares25,619,648,410,936
author_curate_reward""
vote details (640)
@hivebuzz ·
Congratulations @eniolw! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

<table><tr><td><img src="https://images.hive.blog/60x70/https://hivebuzz.me/@eniolw/replies.png?202403291537"></td><td>You got more than 3500 replies.<br>Your next target is to reach 3750 replies.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@eniolw) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Check out our last posts:**
<table><tr><td><a href="/hive-122221/@hivebuzz/pud-202404"><img src="https://images.hive.blog/64x128/https://i.imgur.com/805FIIt.jpg"></a></td><td><a href="/hive-122221/@hivebuzz/pud-202404">Hive Power Up Day - April 1st 2024</a></td></tr><tr><td><a href="/hive/@hivebuzz/birthday-8"><img src="https://images.hive.blog/64x128/https://files.peakd.com/file/peakd-hive/hivebuzz/48UH6maVydCEXmmaoUDSbS7oYV1nYyATQuGeLRJY3AFFunQW3RfwC1334cQRQaqMTH.png"></a></td><td><a href="/hive/@hivebuzz/birthday-8">Happy Birthday to the Hive Blockchain</a></td></tr></table>
properties (22)
authorhivebuzz
permlinknotify-eniolw-20240329t154347
categoryhive-197685
json_metadata{"image":["https://hivebuzz.me/notify.t6.png"]}
created2024-03-29 15:43:48
last_update2024-03-29 15:43:48
depth1
children0
last_payout2024-04-05 15:43:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,205
author_reputation368,331,449,213,314
root_title"My Coding Quiz #55"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,434,384
net_rshares0
@miriannalis ·
@tipu curate 8
properties (22)
authormiriannalis
permlinkre-eniolw-sb6z35
categoryhive-197685
json_metadata{"tags":["hive-197685"],"app":"peakd/2024.3.6"}
created2024-03-31 03:01:48
last_update2024-03-31 03:01:48
depth1
children1
last_payout2024-04-07 03:01:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length14
author_reputation256,937,042,354,500
root_title"My Coding Quiz #55"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,474,394
net_rshares0
@tipu ·
Sorry, please curate posts not older than 1 day.
properties (22)
authortipu
permlinkre-re-eniolw-sb6z35-20240331t201252z
categoryhive-197685
json_metadata"{"app": "beem/0.24.26"}"
created2024-03-31 20:12:51
last_update2024-03-31 20:12:51
depth2
children0
last_payout2024-04-07 20:12:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length48
author_reputation55,208,734,834,230
root_title"My Coding Quiz #55"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,491,609
net_rshares0
@palomap3 ·
![](https://images.ecency.com/DQmWB62cifnBJeAJpwPjscNg3Xjurc9SJB9VBouykyR2HWQ/ecency_curator1.gif)
properties (22)
authorpalomap3
permlinkre-eniolw-2024329t134833586z
categoryhive-197685
json_metadata{"tags":["hive-197685","programming","coding","python","javascript","chess","quiz","neoxian","proofofbrain","waivio","spanish"],"app":"ecency/3.1.1-vision","format":"markdown+html"}
created2024-03-29 12:48:33
last_update2024-03-29 12:48:33
depth1
children1
last_payout2024-04-05 12:48:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length98
author_reputation188,016,779,321,294
root_title"My Coding Quiz #55"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,430,617
net_rshares0
@eniolw ·
Thank you!
properties (22)
authoreniolw
permlinkre-palomap3-2024330t122858863z
categoryhive-197685
json_metadata{"tags":["hive-197685","programming","coding","python","javascript","chess","quiz","neoxian","proofofbrain","waivio","spanish"],"app":"ecency/3.1.1-vision","format":"markdown+html"}
created2024-03-30 16:29:00
last_update2024-03-30 16:29:00
depth2
children0
last_payout2024-04-06 16:29:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10
author_reputation200,516,207,918,518
root_title"My Coding Quiz #55"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,463,696
net_rshares0
@rafael-rdguez2 ·
Awesome. This serie is a very good idea. You can also try with something like "find the error in this code". That will be very interesting too.

I'll even check on previous episodes 😅👍
properties (22)
authorrafael-rdguez2
permlinkre-eniolw-2024329t64850624z
categoryhive-197685
json_metadata{"type":"comment","tags":["hive-197685","programming","coding","python","javascript","chess","quiz","neoxian","proofofbrain","waivio","spanish"],"app":"ecency/3.0.46-mobile","format":"markdown+html"}
created2024-03-29 10:48:45
last_update2024-03-29 10:48:45
depth1
children1
last_payout2024-04-05 10:48:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length184
author_reputation828,423,921,397
root_title"My Coding Quiz #55"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,428,017
net_rshares0
@eniolw ·
That's a good suggestion. Thank you!
properties (22)
authoreniolw
permlinkre-rafael-rdguez2-2024330t122848348z
categoryhive-197685
json_metadata{"tags":["hive-197685","programming","coding","python","javascript","chess","quiz","neoxian","proofofbrain","waivio","spanish"],"app":"ecency/3.1.1-vision","format":"markdown+html"}
created2024-03-30 16:28:48
last_update2024-03-30 16:28:48
depth2
children0
last_payout2024-04-06 16:28:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length36
author_reputation200,516,207,918,518
root_title"My Coding Quiz #55"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,463,687
net_rshares0
@rashed.ifte ·
Cool quiz. @tipu curate 8
properties (22)
authorrashed.ifte
permlinkre-eniolw-sb5xi0
categoryhive-197685
json_metadata{"tags":["hive-197685"],"app":"peakd/2024.3.6"}
created2024-03-30 13:30:12
last_update2024-03-30 13:30:12
depth1
children3
last_payout2024-04-06 13:30:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length25
author_reputation121,886,705,078,791
root_title"My Coding Quiz #55"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,459,998
net_rshares0
@eniolw ·
Thank you! BTW, the tipu command didn't work apparently 😅
properties (22)
authoreniolw
permlinkre-rashedifte-2024330t122832777z
categoryhive-197685
json_metadata{"tags":["hive-197685"],"app":"ecency/3.1.1-vision","format":"markdown+html"}
created2024-03-30 16:28:33
last_update2024-03-30 16:28:33
depth2
children1
last_payout2024-04-06 16:28:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length57
author_reputation200,516,207,918,518
root_title"My Coding Quiz #55"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,463,682
net_rshares0
@rashed.ifte ·
Yeah, I noticed. Maybe the bot is having issues.
properties (22)
authorrashed.ifte
permlinkre-eniolw-sb67kw
categoryhive-197685
json_metadata{"tags":["hive-197685"],"app":"peakd/2024.3.6"}
created2024-03-30 17:07:54
last_update2024-03-30 17:07:54
depth3
children0
last_payout2024-04-06 17:07:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length48
author_reputation121,886,705,078,791
root_title"My Coding Quiz #55"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,464,347
net_rshares0
@tipu ·
Sorry, please curate posts not older than 1 day.
properties (22)
authortipu
permlinkre-re-eniolw-sb5xi0-20240331t192549z
categoryhive-197685
json_metadata"{"app": "beem/0.24.26"}"
created2024-03-31 19:25:48
last_update2024-03-31 19:25:48
depth2
children0
last_payout2024-04-07 19:25:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length48
author_reputation55,208,734,834,230
root_title"My Coding Quiz #55"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,490,718
net_rshares0
@stemsocial ·
re-eniolw-my-coding-quiz-55-20240329t235713975z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> 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.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-eniolw-my-coding-quiz-55-20240329t235713975z
categoryhive-197685
json_metadata{"app":"STEMsocial"}
created2024-03-29 23:57:12
last_update2024-03-29 23:57:12
depth1
children0
last_payout2024-04-05 23:57:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,460,334,324,555
root_title"My Coding Quiz #55"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,445,676
net_rshares0