some problems for an introductory python test

Hope Rouselle hrouselle at jevedi.com
Tue Aug 10 17:13:58 EDT 2021


Chris Angelico <rosuav at gmail.com> writes:

> On Wed, Aug 11, 2021 at 4:14 AM Hope Rouselle <hrouselle at jevedi.xotimo> wrote:
>>
>> Chris Angelico <rosuav at gmail.com> writes:
>>
>> > On Tue, Aug 10, 2021 at 7:25 AM Hope Rouselle
>> > <hrouselle at jevedi.xotimo> wrote:
>> >> I came up with the following question.  Using strings of length 5
>> >> (always), write a procedure histogram(s) that consumes a string and
>> >> produces a dictionary whose keys are each substrings (of the string) of
>> >> length 1 and their corresponding values are the number of times each
>> >> such substrings appear.  For example, histogram("aaaaa") = {"a": 5}.
>> >> Students can "loop through" the string by writing out s[0], s[1], s[2],
>> >> s[3], s[4].
>> >
>> > In other words, recreate collections.Counter? Seems decent, but you'll
>> > need to decide whether you want them to use defaultdict, use
>> > __missing__, or do it all manually.
>>
>> Yes, the course introduces very little so there is a lot of recreation
>> going on.  Hm, I don't know defaultdict and I don't know how to use
>> __missing__.  The course does introduce dict.get(), though.  If students
>> use dict.get(), then the procedure could essentially be:
>>
>> def histogram(s):
>>   d = {}
>>   d[s[0]] = d.get(s[0], 0) + 1
>>   d[s[1]] = d.get(s[1], 0) + 1
>>   d[s[2]] = d.get(s[2], 0) + 1
>>   d[s[3]] = d.get(s[3], 0) + 1
>>   d[s[4]] = d.get(s[4], 0) + 1
>>   return d
>
> There's nothing wrong with getting students to recreate things, but
> there are so many different levels on which you could do this, which
> will leave your more advanced students wondering what's legal. :) Here
> are several ways to do the same thing:

[... very impressive set of solutions...]

> It seems *very* strange to have an exercise like this without looping.

It is.  I agree.

> That seems counterproductive. 

It is.

> But if you're expecting them to not use loops, you'll want to also be
> very clear about what other features they're allowed to use - or
> alternatively, stipulate what they ARE allowed to use, eg "Use only
> indexing and the get() method".

Yes, I will do that.  I mean the course does that all the time.  They
cannot use anything that has not been introduced.  That's another
problem because the course introduces various things and students can't
quite keep everything in mind.  The suggestion to make up a list of
things is mostly ignored by nearly all of them. :-)


More information about the Python-list mailing list