Closed alex-ball closed 2 years ago
I agree here, random.choice is the easiest to read, most intuitive solution, and should be included
Olav
On Tue, Jan 4, 2022 at 12:02 PM Alex Ball @.***> wrote:
The challenge "Locating the right module http://swcarpentry.github.io/python-novice-gapminder/06-libraries/index.html#locating-the-right-module" has several solutions, but I think the most obvious one is missing.
The solutions given are:
print(bases[random.randrange(len(bases))])print(bases[random.randint(0, len(bases) - 1)]) # impliedprint(random.sample(bases, 1)[0])
with the postscript "[There are] also other functions you could use, but with more convoluted code as a result." What about this?
print(random.choice(bases))
This is, I would argue, the most keystroke-efficient solution and most intuitive to read; it certainly does not fall into the "more convoluted" category. Is the reason for missing it out because it answers exactly the use case in the challenge, and is therefore not applicable to adjacent challenges (e.g. picking more than one distinct character; doing something else with the index)?
— Reply to this email directly, view it on GitHub https://github.com/swcarpentry/python-novice-gapminder/issues/581, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABLLJBPB7TALN5KLQ3R4PVTUULHUZANCNFSM5LHFQEZA . You are receiving this because you are subscribed to this thread.Message ID: @.***>
I think the intent of the challenge was to get learners to think about generating random indexes into the string and to reinforce the conceptual models of collections + indices. That said, random.choice
is definitely the simplest / most pythonic way to solve this, and so I think we should definitely mention it, replacing the more convoluted code..
text which isn't even true 😅
Hi,
A really convoluted option would be something along the lines of:
from random import random
bases = 'ACTTGCTTGAC'
value = 0
iterator = 0
selected = ''
while value <= 0.9999:
iterator += 1
value = random()
if value > 0.9999:
selected = bases[iterator % len(bases)]
break
print(selected)
I know this is not code to be proud of, but definitively something I have seen from novices and for sure super anti-pythonic. Consider this to be an example of a very bad way of doing it.
Cheers, Jose
The challenge "Locating the right module" has several solutions, but I think the most obvious one is missing.
The solutions given are:
with the postscript "[There are] also other functions you could use, but with more convoluted code as a result." What about this?
This is, I would argue, the most keystroke-efficient solution and most intuitive to read; it certainly does not fall into the "more convoluted" category. Is the reason for missing it out because it answers exactly the use case in the challenge, and is therefore not applicable to adjacent challenges (e.g. picking more than one distinct character; doing something else with the index)?