create account

My Coding Quiz #48 by eniolw

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

<center><h2>My Coding Quiz #48 馃懆鈥嶐煉火煕狅笍馃З</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/DQmVKXEYVWaLWeJmL2M9sm8F1S6c35Ai9K6EzXpo725Xzec/image.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-47">previous quiz</a>:</b> <b>30</b>. Let's describe the code step by step. After importing the <code>random</code> module, we define the list <code>a</code> with numeric elements. The expression <code>15*2</code> inside the brackets is resolved or replaced by 30.</p>

<p>Then to obtain list <code>b</code> we use the <code>sample</code> method of <code>random</code>, which obtains a sublist of randomly selected elements from another given list, or in other words, a shuffle. This is the built-in pythonian solution as opposed to kind of hack Javascript offers, as we discussed in a previous installment.</p>

<p>Within this line, however, things get a bit more obscure. The expression <code>x:= a * 2</code> applies the <i>walrus operator</i> to create and use the variable <code>x</code> inline. That variable is a duplicate of the list <code>a</code>, that is, <b>[30, 10, 65, 30, 65, 10, 30, 30, 30, 10, 65, 30, 65, 65, 10, 30]</b>. As <code>x</code> comes into existence, we can access the size of that list, which is the second argument that <code>sample</code> takes, that is, the size of the sample.</p>

<p>Line 4 also looks tricky: <code>c = max(set(b), key=x.count)</code>. It is actually a form of a <i>reducer function</i>. What it actually does is to select the element of <code>b</code> that is most frequent. To do this, we first convert it to a <code>set</code> for optimisation purposes and then provide the function <code>x.count</code> to <code>key</code>, which tells the <code>max</code> function to get the frequency of each element of <code>set(b)</code> in the list <code>x</code>. As 30 is repeated 6 times in <code>x</code>, it becomes the most frequent value, which is the one selected by <code>max</code>.</p>

<p>With this code you can then get the most repeated value in a list. Remember that it is important to convert the list to be iterated into a set to avoid iterating more than necessary. With it we iterate over 3 items ([30, 10, 65]), but without it, we would have iterated over 14 items, several of which are repeated, getting the frequency of each unnecessarily.</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 #48 馃懆鈥嶐煉火煕狅笍馃З</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/DQmQzgxddsiu3BZkk6mPBwugua7Givn8gaXMNC4N85Thp3P/image.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-47">quiz anterior</a>:</b> <b>30</b>. Describamos el c贸digo paso a paso. Despu茅s de importar el m贸dulo <code>random</code>, definimos una lista <code>a</code> con elementos num茅ricos. La expresi贸n <code>15*2</code> dentro de los corchetes se resuelve o reemplaza por 30.</p>

<p>Luego, para obtener la lista <code>b</code> utilizamos el m茅todo <code>sample</code> de <code>random</code>, que obtiene una sublista de elementos seleccionados aleatoriamente de otra lista dada, o en otras palabras, una mezcla. Esta es la soluci贸n Pythoniana incorporada en lugar del tipo de hack que ofrece Javascript, como comentamos en una entrega anterior.</p>

<p>Sin embargo, dentro de esta l铆nea las cosas se vuelven un poco m谩s oscuras. La expresi贸n <code>x:= a * 2</code> aplica el <i>operador walrus</i> para crear y usar la variable <code>x</code> inline. Esa variable es un duplicado de la lista <code>a</code>, es decir, <b>[30, 10, 65, 30, 65, 10, 30, 30, 30, 10, 65, 30, 65, 65, 10, 30]</b>. Como <code>x</code> pasa a existir, podemos acceder al tama帽o de esa lista, que es el segundo argumento que toma <code>sample</code>, es decir, el tama帽o de la muestra.</p>

<p>La l铆nea 4 tambi茅n parece complicada: <code>c = max(set(b), key=x.count)</code>. En realidad, es una forma de <i>funci贸n reductora (reducer)</i>. Lo que realmente hace es seleccionar el elemento de <code>b</code> que es m谩s frecuente. Para hacer esto, primero lo convertimos a un <code>set</code> para fines de optimizaci贸n y luego proporcionamos la funci贸n <code>x.count</code> a <code>key</code>, que le indica al <code>max</code> que obtenga la frecuencia de cada elemento de <code>set(b)</code> en la lista <code>x</code>. Como 30 se repite 6 veces en <code>x</code>, se convierte en el valor m谩s frecuente, que es el seleccionado por <code>max</code>.</p>

<p>Con este c贸digo puedes obtener el valor m谩s repetido en una lista. Recuerda que es importante convertir la lista a iterar en un conjunto (set) para evitar iterar m谩s de lo necesario. Con 茅l iteramos sobre 3 elementos ([30, 10, 65]), pero sin 茅l, habr铆amos iterado sobre 14 elementos, varios de los cuales se repiten, obteniendo la frecuencia de cada uno innecesariamente.</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 677 others
properties (23)
authoreniolw
permlinkmy-coding-quiz-48
categoryhive-197685
json_metadata"{"image":["https://images.ecency.com/DQmVKXEYVWaLWeJmL2M9sm8F1S6c35Ai9K6EzXpo725Xzec/image.png","https://images.ecency.com/DQmQzgxddsiu3BZkk6mPBwugua7Givn8gaXMNC4N85Thp3P/image.png"],"tags":["hive-197685","programming","coding","python","javascript","neoxian","proofofbrain","waivio","creativecoin","spanish"],"description":"My Coding Quiz #48 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.0.37-vision","format":"markdown+html","image_ratios":["1.2285","1.2285"]}"
created2024-02-27 00:02:39
last_update2024-02-27 00:02:39
depth0
children6
last_payout2024-03-05 00:02:39
cashout_time1969-12-31 23:59:59
total_payout_value6.308 HBD
curator_payout_value6.155 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,507
author_reputation201,287,173,924,816
root_title"My Coding Quiz #48"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id131,611,317
net_rshares23,391,755,271,215
author_curate_reward""
vote details (741)
@indiaunited ·
Indiaunited Curation 1709004024444
This post has been manually curated by @bhattg from Indiaunited community. Join us on our [Discord Server](https://discord.gg/bGmS2tE). 

Do you know that you can earn a passive income by delegating to @indiaunited. We share more than 100 % of the curation rewards with the delegators in the form of IUC tokens. HP delegators and IUC token holders also get upto 20% additional vote weight. 

Here are some handy links for delegations: [100HP](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=indiaunited&vesting_shares=173635.64453530704%20VESTS), [250HP](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=indiaunited&vesting_shares=434089.11133826757%20VESTS), [500HP](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=indiaunited&vesting_shares=868178.2226765351%20VESTS), [1000HP](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=indiaunited&vesting_shares=1736356.4453530703%20VESTS). 

[![image.png](https://files.peakd.com/file/peakd-hive/bala41288/46eaz12N-image.png)](https://discord.gg/bGmS2tE) 

<sub>**100% of the rewards from this comment goes to the curator for their manual curation efforts. Please encourage the curator @bhattg by upvoting this comment and support the community by voting the posts made by @indiaunited.**</sub>
properties (22)
authorindiaunited
permlinkindiaunited-1709004024444
categoryhive-197685
json_metadata{"app":"hiveblog/0.1","format":"markdown","tags":["hive-197685","programming","coding","python","javascript","neoxian","proofofbrain","waivio","creativecoin","spanish"]}
created2024-02-27 03:20:24
last_update2024-02-27 03:20:24
depth1
children1
last_payout2024-03-05 03:20:24
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,329
author_reputation61,700,718,658,112
root_title"My Coding Quiz #48"
beneficiaries
0.
accountbhattg
weight10,000
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id131,614,019
net_rshares0
@eniolw ·
Thank you!
properties (22)
authoreniolw
permlinkre-indiaunited-2024227t161227979z
categoryhive-197685
json_metadata{"tags":["hive-197685","programming","coding","python","javascript","neoxian","proofofbrain","waivio","creativecoin","spanish"],"app":"ecency/3.0.37-vision","format":"markdown+html"}
created2024-02-27 20:12:30
last_update2024-02-27 20:12:30
depth2
children0
last_payout2024-03-05 20:12:30
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_reputation201,287,173,924,816
root_title"My Coding Quiz #48"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id131,632,324
net_rshares0
@serenecounsel ·
I'm definitely going to have to visit the first post to get the overview. It's a pretty interesting journey. I'm amazed at how you've been consistent with a series to 48. 

I guess I have to adopt your style of numbering my blog-isodes to keep track.

Thank you once again sir for sharing this great content.鉂わ笍
properties (22)
authorserenecounsel
permlinkre-eniolw-s9iw8q
categoryhive-197685
json_metadata{"tags":["hive-197685"],"app":"peakd/2024.1.1"}
created2024-02-27 16:24:27
last_update2024-02-27 16:24:27
depth1
children2
last_payout2024-03-05 16:24:27
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_length310
author_reputation14,208,330,171,063
root_title"My Coding Quiz #48"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id131,626,990
net_rshares0
@eniolw ·
You don't need to number them if they're not a series. My posts of this series are chained.
馃憤  
properties (23)
authoreniolw
permlinkre-serenecounsel-2024227t161431924z
categoryhive-197685
json_metadata{"tags":["hive-197685"],"app":"ecency/3.0.37-vision","format":"markdown+html"}
created2024-02-27 20:14:33
last_update2024-02-27 20:14:33
depth2
children1
last_payout2024-03-05 20:14: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_length91
author_reputation201,287,173,924,816
root_title"My Coding Quiz #48"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id131,632,373
net_rshares2,753,309,458
author_curate_reward""
vote details (1)
@serenecounsel ·
Oh okay Sir, that's well noted.

Thank you so much 鈾ワ笍
properties (22)
authorserenecounsel
permlinkre-eniolw-2024227t205527987z
categoryhive-197685
json_metadata{"type":"comment","tags":["hive-197685"],"app":"ecency/3.0.45-mobile","format":"markdown+html"}
created2024-02-27 20:56:24
last_update2024-02-27 20:56:24
depth3
children0
last_payout2024-03-05 20:56:24
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_length53
author_reputation14,208,330,171,063
root_title"My Coding Quiz #48"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id131,634,057
net_rshares0
@stemsocial ·
re-eniolw-my-coding-quiz-48-20240227t201132609z
<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-48-20240227t201132609z
categoryhive-197685
json_metadata{"app":"STEMsocial"}
created2024-02-27 20:11:33
last_update2024-02-27 20:11:33
depth1
children0
last_payout2024-03-05 20:11: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_length565
author_reputation22,460,408,920,472
root_title"My Coding Quiz #48"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id131,632,293
net_rshares0