[Tutor] Re: newbie looking for code suggestions
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Sun, 22 Sep 2002 22:20:16 -0700 (PDT)
> > Okay, so mathematically spoken, you want the size of a certain list
> > where each item is equivalent to the others by it's cross sum (sorry
> > Magnus). This is the code for the "equivalent lists":
> >
> > #v+
> > def quotient_set(seq, func):
> > """ partition seq into equivalence classes """
> > quotient_set = {}
> > for item in seq:
> > quotient_set.setdefault(repr(func(item)),[]).append(item)
> > return quotient_set
> > #v-
> >
> > >>> min_number, max_number, digit_total = 1, 999999, 21
> > >>> len(quotient_set(range(min_number, max_number+1),
> cross_sum)[str(digit_total)])
> > 39962
Wow, very cool. That quotient_set() function is appearing everywhere!
*grin* I hadn't thought about applying quotient_set() this way to solve
the problem.
> Is that a typo on that output, or is that what you actually got? I got
> 39662 when I ran it...
I'm also getting 39662 on my solution.
###
dyoo@coffeetable:~$ python bobs_puzzle.py
39662
###
> > cpu: 28.121, total: 29.832
>
> Neat. How do you get the cpu and total time to show up?
Unix comes with a 'time' command, so if we have a program called
'count_quotient_set.py' we can do:
###
$ time ./count_quotient_set.py
###
which should give us timing statistics after the program is finished.
> > Please omit the HTML part. Thanks!
>
> Sorry again. I thought it was set to plain text. Is this better?
Looks good now!
Bob, thanks for asking this problem; lots of interesting stuff is coming
out of this.