create account

My Coding Quiz #39 by eniolw

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

<center><h2>My Coding Quiz #39 馃懆鈥嶐煉火煕狅笍馃З</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/DQmeWuiJCx6toRUs6qqsadm1uzJUJvU6uebV2CjWC1Gm7LH/quiz_img_en.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-38">previous quiz</a>:</b> <b>['Tue', 'Thu']</b>. We start by defining a function <code>getWeekDay</code> that expects to receive a number. We see that it returns one of the options contained in the dictionary, which is the name of the day of the week depending on the key number. To prevent errors due to non-existent key, we have used the <code>get</code> method with a default value to return.</p>

<p>This way of programming using a dictionary to choose options is very popular in Python due to the absence of a <i>switch case</i> clause in it, although Python 3.10 finally already incorporates something similar called <i>match case</i>.</p>

<p>Then in the main program we create a list <code>schedule</code> using list comprehension. Some programmers disagree with this way of denoting or using list comprehensions, but there is no definitive agreement or convention on this. Let's take a closer look:</p>

<pre><code>schedule = [
    n for d in range(1,7) 
    if d % 2 == 0 and 
       (n := getWeekDay(d)) and 
       not n.startswith('S')
]</code></pre>

<p>The expression <code>n for d in range(1,7)</code> indicates that the list will consist of the values of <code>n</code> in an iteration from 1 to 6 (7 is excluded). We see that <code>d % 2 == 0</code> indicates that only the even values are selected which could be <b>2</b>,<b>4</b> and <b>6</b>.</p>

<p>With these values, we obtain their alphabetical code with the instruction <code>(n := getWeekDay(d))</code>. Here we have used the <i>walrus operator</i> which is an innovation of Python 3.8. Therefore, if you run this code in an earlier Python, it will give you a syntax error. This operator is very handy and powerful. What it does is to <i>create and assign variables in the middle of expressions</i>. In our case, we created the variable <code>n</code> and assigned it what <code>getWeekDay</code> returns. The variable <code>n</code> comes into existence from then on and we see that it is renewed at each iteration.</p>

<p>The last condition <code>not n.startswith('S')</code> indicates that the alphabetic code for the day of the week must not start with <code>S</code>. Days 2, 4 and 6 correspond to <b>'Tue'</b>, <b>'Thu'</b> and <b>'Sat'</b>, but 'Sat' is discarded because of what we said. Therefore, the <code>schedule</code> list contains only <b>'Tue'</b> and <b>'Thu'</b>.</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 #39 馃懆鈥嶐煉火煕狅笍馃З</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/DQmeHf4MwXHhzEU2WiKTUfb2aewiWreuzXnsFikigRHArw8/quiz_img_es.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-38">quiz anterior</a>:</b> <b>['Tue', 'Thu']</b>. Comenzamos definiendo una funci贸n <code>getWeekDay</code> que espera recibir un n煤mero. Vemos que nos devuelve una de las opciones contenidas en el diccionario, la cual es el nombre del d铆a de la semana dependiendo del n煤mero de clave. Para evitar errores debido a una clave inexistente, hemos utilizado el m茅todo <code>get</code> con un valor predeterminado para devolver.</p>

<p>Esta forma de programar usando un diccionario para elegir opciones es muy popular en Python debido a la ausencia de una cl谩usula <i>switch case</i> en 茅l, aunque Python 3.10 finalmente ya incorpora algo similar llamado <i>match case</i>.</p>

<p>Luego, en el programa principal creamos una lista <code>schedule</code> usando la comprensi贸n de listas. Algunos programadores no est谩n de acuerdo con esta forma de denotar o utilizar listas por comprensi贸n, pero no existe un acuerdo o convenci贸n definitivo al respecto. Echemos un vistazo m谩s de cerca:</p>

<pre><code>schedule = [
    n for d in range(1,7) 
    if d % 2 == 0 and 
       (n := getWeekDay(d)) and 
       not n.startswith('S')
]</code></pre>

<p>La expresi贸n <code>n for d in range(1,7)</code> indica que la lista estar谩 formada por los valores de <code>n</code> en una iteraci贸n del 1 al 6 (7 es excluido). Vemos que <code>d % 2 == 0</code> indica que solo se seleccionan los valores pares que podr铆an ser <b>2</b>,<b>4</b> y <b>6</b>.</p>

<p>Con estos valores obtenemos su c贸digo alfab茅tico con la instrucci贸n <code>(n := getWeekDay(d))</code>. Aqu铆 hemos utilizado el <i>Operador Walrus</i> que es una innovaci贸n de Python 3.8. Por lo tanto, si ejecutas este c贸digo en una versi贸n anterior de Python, obtendr谩s un error de sintaxis. Este operador es muy pr谩ctico y potente. Lo que hace es <i>crear y asignar variables en medio de expresiones</i>. En nuestro caso, creamos la variable <code>n</code> y le asignamos lo que devuelve <code>getWeekDay</code>. La variable <code>n</code> entra en existencia a partir de ese momento y vemos que se renueva en cada iteraci贸n.</p>

<p>La 煤ltima condici贸n <code>not n.startswith('S')</code> indica que el c贸digo alfab茅tico para el d铆a de la semana no debe comenzar con <code>S</code>. Los d铆as 2, 4 y 6 corresponden a <b>'Tue'</b>, <b>'Thu'</b> y <b>'Sat'</b>, pero se descarta 'Sat' por lo que dijimos. Por lo tanto, la lista <code>schedule</code> contiene s贸lo <b>'Tue'</b> y <b>'Thu'</b>.</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 582 others
properties (23)
authoreniolw
permlinkmy-coding-quiz-39
categoryhive-169321
json_metadata"{"image":["https://images.ecency.com/DQmeWuiJCx6toRUs6qqsadm1uzJUJvU6uebV2CjWC1Gm7LH/quiz_img_en.png","https://images.ecency.com/DQmeHf4MwXHhzEU2WiKTUfb2aewiWreuzXnsFikigRHArw8/quiz_img_es.png"],"tags":["hive-169321","coding","programming","python","development","neoxian","creativecoin","proofofbrain","waivio","spanish"],"description":"My Coding Quiz #39 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.0112","1.0112"]}"
created2023-12-08 17:12:00
last_update2023-12-08 17:12:00
depth0
children1
last_payout2023-12-15 17:12:00
cashout_time1969-12-31 23:59:59
total_payout_value4.652 HBD
curator_payout_value4.537 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,005
author_reputation253,328,400,128,603
root_title"My Coding Quiz #39"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,512,839
net_rshares19,758,240,062,523
author_curate_reward""
vote details (646)
@stemsocial ·
re-eniolw-my-coding-quiz-39-20231209t024248216z
<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-39-20231209t024248216z
categoryhive-169321
json_metadata{"app":"STEMsocial"}
created2023-12-09 02:42:48
last_update2023-12-09 02:42:48
depth1
children0
last_payout2023-12-16 02:42: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_length565
author_reputation22,903,676,462,363
root_title"My Coding Quiz #39"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,522,725
net_rshares0