some problems for an introductory python test

Chris Angelico rosuav at gmail.com
Mon Aug 9 18:00:02 EDT 2021


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.

> I think you get the idea.  I hope you can provide me with creativity.  I
> have been looking at books, but every one I look at they introduce loops
> very quickly and off they go.  Thank you!

Probably because loops are kinda important? :)

ChrisA


More information about the Python-list mailing list